【问题标题】:Symfony/serializer Normalize Object in ObjectSymfony/serializer 规范化对象中的对象
【发布时间】:2019-07-26 00:04:14
【问题描述】:

大家好,需要一点帮助我没有找到关于递归规范化对象的回复

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

但我有这个错误

在 AbstractObjectNormalizer.php 第 129 行: 无法规范化属性“customAttributes”,因为注入 序列化器不是规范化器

感谢您的帮助

【问题讨论】:

    标签: php symfony serialization


    【解决方案1】:

    这是因为您忘记添加$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)
    

    【讨论】:

    • 非常感谢您的帮助。创建一个序列化器来规范化而不仅仅是一个 Normalizer 非常奇怪,但没关系:)
    猜你喜欢
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 2017-05-02
    • 2019-10-03
    • 2011-10-24
    • 2015-09-20
    • 2017-01-27
    相关资源
    最近更新 更多