hshn/serializer-extra-bundle

此包为序列化提供了一些额外功能

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

This package is not auto-updated.

Last update: 2024-09-24 03:18:19 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

此包为序列化提供了一些额外功能。

对象导出的权限

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

当对象被序列化时,由AuthorizationCheckerInterface::isGranted()提供的访问权限将被导出到属性'_authority'。

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

重写属性名

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

限制按深度导出权限

# 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)

将文件导出为URL

此功能需要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');

在序列化对象时导出生成的URL。

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

将图片导出为URL

此功能需要LiipImagineBundle

在文件配置中添加过滤器名称规范。

# 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"
}