1. Go to this page and download the library: Download psx/data 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/ */
psx / data example snippets
// create processor
$processor = new Processor(Configuration::createDefault());
// example json data which we want to parse in our model
$in = <<<JSON
{
"id": 1,
"title": "Lorem ipsum",
"author": {
"id": 1,
"name": "Foo",
"email": "[email protected]",
},
"comments": [{
"id": 1,
"author": {
"id": 1,
"name": "Foo",
"email": "[email protected]",
},
"text": "Lorem ipsum"
},{
"id": 2,
"author": {
"id": 1,
"name": "Foo",
"email": "[email protected]",
},
"text": "Lorem ipsum"
}],
"date": "2016-03-28T22:40:00Z"
}
JSON;
// reads the json data into a custom model class
$model = $processor->read(News::class, Payload::json($in));
// the model can be used to get or set data
$model->getAuthor()->getName();
$model->getComments()[0]->getText();
// writes the model back to json
$out = $processor->write(Payload::json($model));
// model classes
class News
{
private ?int $id = null;
private ?string $title = null;
protected ?Author $author = null;
/**
* @var array<Comment>|null
*/
private ?array $comments = null;
#[Format('date-time')]
private ?string $date;
// getter/setter implementations removed for readability
}
class Author
{
private ?int $id = null;
private ?string $name = null;
private ?string $email = null;
// getter/setter implementations removed for readability
}
class Comment
{
private ?int $id = null;
private ?Author $author = null;
private ?string $text = null;
// getter/setter implementations removed for readability
}