hshn/serializer-extra-bundle

This bundle provides some extra features for serialization

v0.4.1 2015-01-15 12:16 UTC

This package is not auto-updated.

Last update: 2020-08-03 06:50:52 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

This bundle provides some extra features for serialization.

Exporting authorities of objects

# app/config.yml
hshn_serializer_extra:
    authority:
        classes:
            AcmeBundle\Entity\Blog:
                attributes: OWNER
/** @var $serializer JMS\Serializer\Serializer */
$json = $serializer->serialize($blog, 'json');

The access authorities provided by AuthorizationCheckerInterface::isGranted() will be exported to the attribute '_authority' when an object was serialized.

{
    "key": "value",
    "_authority": {
        "OWNER": true
    }
}

Overriding the attribute name

# app/config.yml
hshn_serializer_extra:
    authority:
        export_to: "my_authority"
{
    "key": "value",
    "my_authority": {
        "OWNER": true
    }
}

Restrict exporting the authorities by depth

# app/config.yml
hshn_serializer_extra:
    authority:
        classes:
            AcmeBundle\Entity\Blog:
                attributes: [OWNER]
                max_depth: 0 # default -1 (unlimited)
class Blog
{
}

class User
{
    /**
     * @var Blog
     */
    private $blog;
}

$serializer->serialize($blog, 'json'); // will export the blog authorities (depth 0)
$serializer->serialize($user, 'json'); // will NOT export the blog authorities (depth 1)

Export files as URLs

This feature require VichUploaderBundle

# app/config.yml
hshn_serializer_extra:
    vich_uploader:
        classes:
            AcmeBundle\Entity\Blog:
                files:
                    - { property: picture }
                    - { property: picture, export_to: image }
/** @var $serializer JMS\Serializer\Serializer */
$json = $serializer->serialize($blog, 'json');

Generated URLs will be exported when serializing an object.

{
    "picture": "/url/to/picture",
    "image": "/url/to/picture"
}

Export images as URLs

This feature require LiipImagineBundle

Adding a filter name specification to a file configuration.

# app/config.yml
hshn_serializer_extra:
    vich_uploader:
        classes:
            AcmeBundle\Entity\Blog:
                files:
                    - { property: picture }
                    - { property: picture, export_to: image, filter: thumbnail }
{
    "picture": "/url/to/picture",
    "image": "/url/to/thumbnail/picture"
}