PHP code example of exorg / data-coder

1. Go to this page and download the library: Download exorg/data-coder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

exorg / data-coder example snippets




use ExOrg\DataCoder\Coder\Json\Data\Encoder;

$data = [
    "firstName" => "John",
    "lastName" => "Smith",
    "address" => [
        "streetAddress" => "21 2nd Street",
        "city" => "New York",
        "state" => "NY",
        "postalCode" => "10021-3100",
    ],
];

$encoder = new Encoder();
$result = $encoder->encodeData($data);

print($result);

use ExOrg\DataCoder\Coder\Yaml\Data\Decoder;

$data = '
firstName: John
lastName: Smith
address:
  streetAddress: 21 2nd Street
  city: New York
  state: NY
  postalCode: 10021-3100
';

$decoder = new Decoder();
$result = $decoder->decodeData($data);

print_r($result);

use ExOrg\DataCoder\Coder\Data\Encoder;

$data = [
    "firstName" => "John",
    "lastName" => "Smith",
    "address" => [
        "streetAddress" => "21 2nd Street",
        "city" => "New York",
        "state" => "NY",
        "postalCode" => "10021-3100",
    ],
];

$encoder = new Encoder();
$encoder->setDataFormat('yaml');
$result = $encoder->encodeData($data);

print($result);

use ExOrg\DataCoder\Coder\Data\Decoder;

$data = '
{
    "firstName": "John",
    "lastName": "Smith",
    "isAlive": true,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": "10021-3100"
    }
}
';

$decoder = new Decoder();
$decoder->setDataFormat('json');
$result = $decoder->decodeData($data);

print_r($result);

use ExOrg\DataCoder\Coder\Datafile\Encoder;

$data = [
    "firstName" => "John",
    "lastName" => "Smith",
    "address" => [
        "streetAddress" => "21 2nd Street",
        "city" => "New York",
        "state" => "NY",
        "postalCode" => "10021-3100",
    ],
];

$datafilePath = 'data.json';

$encoder = new Encoder();
$encoder->encodeFile($data, $datafilePath);

print file_get_contents($datafilePath);

use ExOrg\DataCoder\Coder\Datafile\Decoder;

$datafilePath = 'data.yaml';

print file_get_contents($datafilePath);

$decoder = new Decoder();
$data = $decoder->decodeFile($datafilePath);

print_r($data);