PHP code example of realshadow / serializers
1. Go to this page and download the library: Download realshadow/serializers 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/ */
realshadow / serializers example snippets
array(
'products' => array(
array(
'brand' => 'Samsung',
'model' => 'Galaxy',
'price' => 999
),
array(
'brand' => 'HTC',
'model' => 'One',
'price' => null
)
)
);
$array = array(
Serializers\Encoders\Xml::ATTRIBUTES => array(
'xmlns' => 'http://cfh.sk/izmluvnik/xsell',
Serializers\Encoders\Xml::NS => array(
array(
'name' => 'xmlns:xsi',
'content' => 'http://www.w3.org/2001/XMLSchema-instance'
),
array(
'name' => 'xmlns:xsd',
'content' => 'http://www.w3.org/2001/XMLSchema'
)
)
),
'products' => array(
array(
'brand' => 'Samsung',
'model' => 'Galaxy',
'price' => 999
),
array(
'brand' => 'HTC',
'model' => 'One',
'price' => null
)
)
);
$xml = Serializers\Encode::toXml('root', $array, array(
'singularize_words' => true,
'nil_on_null' => true
));
print $xml->withHeaders();
// using the same XML that we got in serialization
$output = Serializers\Decode::xml($xml->load(), array('singularize_words' => true));
print_r($output->toArray());
$json = <<<EOT
{
"foo" : "bar",
"small" : "123456",
"large" : 200000000000009093302,
"text" : "Example ratio 1000000000000000:1",
"date" : "/Date(1425556377427+0100)/"
}
EOT;
$s = Serializers\Decode::json($json);
print_r($s->toObject());
// transform said json to xml and output it
print Serializers\Decode::json($json)->toXml('root')->withHeaders();
// events
$json = Serializers\Decode::json($json)->on(Serializers\Events::JSON_MSDATE_MATCH, function($date) {
// matches returned from preg_replace_callback
list(, $timestamp,,) = $date;
return date('Y-m-d H:i:s', $timestamp);
});
$json = Serializers\Encode::toJson(array(
'foo' => 'bar',
'foodate' => date('d.m.Y H:i:s')
))->onSerialize(function($data) {
$data['foodate'] = Serializers\Encoders\Json::toMSDate($data['foodate']);
return $data;
});
print $json->withHeaders();
$jsonp = Serializers\Encode::toJsonp('_foo.bar', array(
'foo' => 'bar',
'bar' => 'foo'
));
$jsonp->allowCors('*', array('GET', 'POST'));
print $jsonp->withHeaders();
$json = '_foo.bar({"foo":"bar","bar":"foo"})';
$data = Serializers\Decode::jsonp($json);
print_r($data->toObject());
// transform said json to xml with callback name as root element and output it
print Serializers\Decode::jsonp($json)->toXml()->withHeaders();
$yaml = \Serializers\Encode::toYaml($array);
print_r($yaml->load());
// or
$yaml->toFile('config.yml');
$yaml = Serializers\Decode::yaml(file_get_contents('config.yml'));
print_r($yaml->toObject());
// transform said json to xml and output it
print Serializers\Decode::yaml(file_get_contents('config.yml'))->toXml('yaml')->withHeaders();
$array = array(
'a' => 'd',
'b' => array('test' => 'c'),
'database' => array(
'default' => array(
'name' => 'db',
'host' => 'master.db',
'ip' => 'dd',
)
),
'array' => array('a', '1', 3),
);
$encode = Serializers\Encode::toIni($array);
$encode->toFile('config.ini');
$ini = Serializers\Encode::toIni($array);
print_r($ini->load());
xml
<products>
<product>
<brand>Samsung</brand>
<model>Galaxy</model>
<price>999</price>
</product>
<product>
<brand>HTC</brand>
<model>One</model>
<price xsi:nil="true"></price>
</product>
</products>
xml
<products>
<brand>Samsung</brand>
<model>Galaxy</model>
<price>999</price>
</products>
<products>
<brand>HTC</brand>
<model>One</model>
<price xsi:nil="true"></price>
</products>