PHP code example of psx / data

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
}



$payload = Payload::create($in, 'application/xml');

$configuration->getReaderFactory()->addReader(new Acme\Reader(), 32);

$payload = Payload::create(
    (string) $request->getBody(),
    $request->getHeaderLine('Content-Type')
);

$payload = Payload::create(
    $model,
    $request->getHeaderLine('Accept')
);

$configuration->getWriterFactory()->addWriter(new Acme\Writer(), 64);

#[Required(['title'])]
class News
{
    #[Pattern('[A-z]')]
    private ?string $title = null;

     #[MinLength(3)]
     #[MaxLength(255)]
    private ?string $text = null;

    #[Enum(['active', 'deleted'])]
    private ?string $status = null;

    #[Minimum(0)]
    #[Maximum(5)]
    private ?int $rating = null;
}

$payload = Payload::xml($data);
$payload->setTransformer(new XmlValidator('path/to/schema.xsd'));

$model = $processor->read(News::class, $payload);