php - Symfony/serializer 在对象中标准化对象

标签 php symfony serialization

大家好,我没有找到关于递归归一化对象的回复

    class User
    {
        public $email;
        public $userId;
        public $userName;
        /**
         * @var CustomAttributes
         */
        public $customAttributes;
    }
    class CustomAttributes
    {
        public $someStuff;
        public $someStuffHere;
    }

我只是想通过 symfony 组件的 normalize() 将其转换为数组 snake_case

$normalizer = new PropertyNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
$user_normalize = $normalizer->normalize($user);

但是我有这个错误

In AbstractObjectNormalizer.php line 129: Cannot normalize attribute "customAttributes" because the injected serializer is not a normalizer

谢谢你的帮助

最佳答案

这是因为你忘记添加 $serializer = new Serializer([$normalizer]) 位。请参阅下面的工作示例。

实现

use App\User;
use App\CustomAttributes;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;

class SnakeCaseUtility
{
    public function convert(): array
    {
        $classMetadataFactory = null;
        $nameConverter = new CamelCaseToSnakeCaseNameConverter();

        $normalizer = new PropertyNormalizer($classMetadataFactory, $nameConverter);
        $serializer = new Serializer([$normalizer]);

        $normalizedUser = $serializer->normalize($this->getUser(), 'json');

        return $normalizedUser;
    }

    private function getUser(): User
    {
        $customAttributes = new CustomAttributes();
        $customAttributes->someStuff = 'Some stuff';
        $customAttributes->someStuffHere = 'Some stuff here';

        $user = new User();
        $user->userId = 123;
        $user->userName = 'Hello World';
        $user->email = 'hello@world.com';
        $user->customAttributes = $customAttributes;

        return $user;
    }
}

测试

use App\SnakeCaseUtility;
use PHPUnit\Framework\TestCase;

class SnakeCaseUtilityTest extends TestCase
{
    public function testSnakeCase(): void
    {
        $expected = [
            'email' => 'hello@world.com',
            'user_id' => 123,
            'user_name' => 'Hello World',
            'custom_attributes' => [
                'some_stuff' => 'Some stuff',
                'some_stuff_here' => 'Some stuff here',
            ]
        ];

        $this->assertSame($expected, (new SnakeCaseUtility())->convert());
    }
}

结果

$ vendor/bin/phpunit --filter SnakeCaseUtilityTest tests/SnakeCaseUtilityTest.php 
PHPUnit 7.5.1 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 83 ms, Memory: 4.00MB

OK (1 test, 1 assertion)

关于php - Symfony/serializer 在对象中标准化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54987264/

相关文章:

php - MySQL 和 PHP 中的线程消息传递问题

twig - 使用表单实体字段类型(Symfony3)在 Twig 模板中使用自定义 html 包装复选框

c++ - boost 序列化: How does pointer tracking work?

java - Gson序列化方法没有被调用

java - 序列化一个静态类

PHP、MySQL 登录跳过应执行的代码部分

php - Zend Framework 2 - 如何对自己的 session 服务进行单元测试?

php - 从 PHP 到 Python 住宿

php - $query->andWhere() 中的 Foreach 语句

php - 如何用 Twig 渲染字符串/数据库中的内容?