Jsonfy Class

The Jsonfy class is used to handle a two level dimensionsional array or json string. It may convert a json string to an array, convert an array to json, modify an existing json or array data , fetch or remove data from a json string. The methods available are as follows:

The following keywords should be noted:

keywords
    
    $data : refers to a json string or array data variable

    $array : refers to sample array used - ['name' => 'foo', 'class' => 'bar'];

    $json  : refers to sample json used - {"name": "foo", "class": "bar"};

    data   : specifies that an argument accepts a json string or array data 
    
                                

Initializing Jsonfy class
The jsonfy class can be easily initialized as shown below.

Sample: Initializing Input
  $jsonfy  = new Jsonfy;
                                        

newData
The newData method is used to set a new json string or array data

Syntax: newData
  $jsonfy->newData(data); // check keywords for data
                                        
We shall be looking at a series of examples below.
Example: setting files
  $jsonfy->newData($json); // check keywords for $json

  $jsonfy->newData($array); // check keywords for $array
                                

add
The add() method is used to add a key and its value into a json or array data. It involves a series of formats. We shall be looking at few examples:

Syntax
    $jsonfy->add(null); 
    $jsonfy->add(name); 


    $jsonfy->add(null, null); 
    $jsonfy->add(null, value); 
    $jsonfy->add(name, value);


    $jsonfy->add(null, null, null); 
    $jsonfy->add(name, key, value); 
    $jsonfy->add(null, key, value); 
    $jsonfy->add(name, null, value); 
    
    where: 

     name  : name of a given index of an associative array 
     value : value of a given index of an associative or 2-level multidimentional array 
     key   : subkey of a 2-level multidimentional array
     null  : numbered index (e.g 0, 1, 2 ...) or empty string

    Note: This may look comprehensive but a series of examples will provide guidance 
    
                                        
Examples
    Note:: all case lines below are assumed to be the first line

    //case 1 - one argument
    sample: $jsonfy->add('');                 //['0'=>'']
    sample: $jsonfy->add('foo');              //['foo'=>'']

    //case 2 - two arguments
    sample: $jsonfy->add('', '');             //['0'=>'']
    sample: $jsonfy->add('', 'bar');          //['0'=>'bar']
    sample: $jsonfy->add('foo', 'bar');       //['foo'=>'bar']

    //case 3 - three arguments
    sample: $jsonfy->add('', '', '');         //['0'=>['0'=>'']]
    sample: $jsonfy->add('foo', 'bar', 'me'); //['foo'=>['bar'=>'me']]
    sample: $jsonfy->add('', 'bar', 'me');    //['0'=>['bar'=>'me']]
    sample: $jsonfy->add('', 'me', '');       //['0'=>['me'=>'']]
    sample: $jsonfy->add('', '', 'me');       //['0'=>['0'=>'me']]
                                        
In the above, all empty strings (i.e '') resolves to numbers except in few occassions. Kindly note the following
  1. When one argument is supplied, it becomes the index key. If the argument is null, then numbers are used starting from 0.
  2. When two null arguments are supplied without a third, the first argument resolves to numbered index while the second resolves to empty value
  3. When two null arguments are supplied with a third, the first two arguments resolves to numbered index while the third resolves to value
  4. When numbers are used, the jsonfy class increases index numbers. This means that if add('') is used twice, the first will have the index of zero 0 while the the second will assume an index of one 1
datakey
The datakey() method returns the first key of a specified value for an associative array.

Syntax: datakey
    $jsonfy->datakey(key); 

    
    where: 

     key : name of a given index of an associative array
    
                                        
Examples
    Note:: very line below is assumed to be the first list

    $jsonfy->newData(['user'=> 'foo', 'class' => 'bar']); or {"name": "foo", "class": "bar"}

    var_dump( $jsonfy->datakey('foo') ); // user
    var_dump( $jsonfy->datakey('bar') ); // class
                                        

update
The update() method works similarly as the add() method, taking the same count of parameters needed for a corresponding update

Examples
    //example 1
    $jsonfy->add('name', 'foo');                     //['name'=>'foo']
    $jsonfy->update('name', 'voo');                  //['name'=>'voo']

    //example 2
    $jsonfy->add('', 'foo');                         //['0'=>'foo']
    $jsonfy->update($jsonfy->datakey('foo'), 'bar'); //['0'=>'bar']

    //example 3
    $jsonfy->add('user','foo','bar');                //['user'=>['foo'=>'bar']]
    $jsonfy->update('user', 'foo', 'me');            //['user'=>['foo'=>'me']]
                                        

delete
The delete() method deletes a value from existing jsonfy data It removes either the main key of a 2-level multidimentional array or the subkey of a multidimentional array.

Examples
    //test data
    $jsonfy->add('user','foo','bar'); //['user'=>['foo'=>'bar']]
    
    //example 1
    $jsonfy->delete('user', 'foo');   //['user'=>'']

    //example 2
    $jsonfy->delete('user');          //[]
                                        

read
The read method reads data from a supplied jsonfy data.

Syntax: read
    $jsonfy->read(key); 
    
    where:
        
     key: main array index key.
    
                                        
Examples
    //example 1
    $jsonfy->add('user','foo','bar');   //['user'=>['foo'=>'bar']]
    var_dump( $jsonfy->read('user') );  //['foo'=>'bar']


    //example 2
    $jsonfy->add('user','foo');         //['user'=>'foo']
    var_dump( $jsonfy->read('user') );  //foo

    Note: when an index key does not exist, it returns a boolean of false
                                        

data
This data method returns the entire stored data. This can be in form of array or json string.

Syntax: data
    $jsonfy->data(type); 
    
    where: 
    
     type - options [null|json|source|count] 

       null - returns array of data stored 
       json - returns json string of data stored 
       count - returns the total count of data 
       source - returns the source data supplied from newData method 
    
                                        
Example: returning a data
    $jsonfy->newData(['foo' => 'bar']); 

    var_dump($jsonfy->data('source')); // ['foo' => 'bar']

    var_dump($jsonfy->data());         // ['foo' => 'bar']

    var_dump($jsonfy->data('json'));   // {"foo": "bar"}

    var_dump($jsonfy->data('count'));  // 1