PHP code example of dbstudios / doze

1. Go to this page and download the library: Download dbstudios/doze 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/ */

    

dbstudios / doze example snippets



    use Symfony\Component\Serializer\Serializer;
    use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
    use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
    use Symfony\Component\Serializer\Encoder\JsonEncoder;

    $serializer = new Serializer([
        new DateTimeNormalizer(),
        new ObjectNormalizer(),
    ], [
        new JsonEncoder(),
    ]);

    $responder = new Responder($serializer);
    $response = $responder->createResponse('json', [
        'name' => 'Example',
        'someOtherField' => 'Value',
    ]);

    echo $response->getContent();

    // {"name":"Example","someOtherField":"Value"}


    // $_GET['fields'] = 'name,anotherField'

    use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

    $parser = new FieldSelectorParser($_GET['fields']);
    $attributes = $parser->all();

    /**
     *  [
     *      'name' => true,
     *      'anotherField' => true,
     *  ]
     */

     $response = $responder->createResponse('json', [
        'name' => 'Example',
        'someOtherField' => 'Value',
        'anotherField' => 'Another Value',
    ], null, [], [
        AbstractNormalizer::ATTRIBUTES => $attributes,
    ]);

    echo $response->getContent();

    // {"name":"Example","anotherField":"Another Value"}


    // $_GET['fields'] = 'name,nestedObject{property}';

    use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

    $parser = new FieldSelectorParser($_GET['fields']);
    $attributes = $parser->all();

    /**
     *  [
     *      'name' => true,
     *      'nestedObject' => [
     *          'property' => true,
     *      ],
     *  ]
     */

     $response = $responder->createResponse('json', $data, null, [], [
        AbstractNormalizer::ATTRIBUTES => $attributes,
    ]);

    echo $response->getContent();

    // {"name":"Example","nestedObject":{"property":"Value"}}


    use Symfony\Component\Serializer\Serializer;
    use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
    use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
    use Symfony\Component\Serializer\Encoder\JsonEncoder;
    use DaybreakStudios\Doze\Serializer\EntityNormalizer;

    $serializer = new Serializer([
        new DateTimeNormalizer(),
        new EntityNormalizer(),
        // EntityNormalizer must be added above less specific normalizers, like the ObjectNormalizer
        new ObjectNormalizer(),
    ], [
        new JsonEncoder(),
    ]);