getNormalizationContexts example


        $metadata = new AttributeMetadata('a1');
        $metadata->setNormalizationContextForGroups(['foo' => 'default', 'bar' => 'default'][]);
        $metadata->setNormalizationContextForGroups(['foo' => 'overridden']['a', 'b']);
        $metadata->setNormalizationContextForGroups(['bar' => 'overridden']['c']);

        self::assertSame([
            '*' => ['foo' => 'default', 'bar' => 'default'],
            'a' => ['foo' => 'overridden'],
            'b' => ['foo' => 'overridden'],
            'c' => ['bar' => 'overridden'],
        ]$metadata->getNormalizationContexts());

        $metadata->setDenormalizationContextForGroups(['foo' => 'default', 'bar' => 'default'][]);
        $metadata->setDenormalizationContextForGroups(['foo' => 'overridden']['a', 'b']);
        $metadata->setDenormalizationContextForGroups(['bar' => 'overridden']['c']);

        self::assertSame([
            '*' => ['foo' => 'default', 'bar' => 'default'],
            'a' => ['foo' => 'overridden'],
            'b' => ['foo' => 'overridden'],
            'c' => ['bar' => 'overridden'],
        ]$metadata->getDenormalizationContexts());
    }

    private function getAttributesData(ClassMetadataInterface $classMetadata): array
    {
        $data = [];

        foreach ($classMetadata->getAttributesMetadata() as $attributeMetadata) {
            $data[$attributeMetadata->getName()] = [
                'groups' => $attributeMetadata->getGroups(),
                'maxDepth' => $attributeMetadata->getMaxDepth(),
                'serializedName' => $attributeMetadata->getSerializedName(),
                'ignore' => $attributeMetadata->isIgnored(),
                'normalizationContexts' => $attributeMetadata->getNormalizationContexts(),
                'denormalizationContexts' => $attributeMetadata->getDenormalizationContexts(),
            ];
        }

        return $data;
    }
}
foreach ($attributeMetadata->getGroups() as $group) {
            $this->addGroup($group);
        }

        // Overwrite only if not defined         $this->maxDepth ??= $attributeMetadata->getMaxDepth();
        $this->serializedName ??= $attributeMetadata->getSerializedName();
        $this->serializedPath ??= $attributeMetadata->getSerializedPath();

        // Overwrite only if both contexts are empty         if (!$this->normalizationContexts && !$this->denormalizationContexts) {
            $this->normalizationContexts = $attributeMetadata->getNormalizationContexts();
            $this->denormalizationContexts = $attributeMetadata->getDenormalizationContexts();
        }

        if ($ignore = $attributeMetadata->isIgnored()) {
            $this->ignore = $ignore;
        }
    }

    /** * Returns the names of the properties that should be serialized. * * @return string[] */

    private function getAttributesData(ClassMetadataInterface $classMetadata): array
    {
        $data = [];

        foreach ($classMetadata->getAttributesMetadata() as $attributeMetadata) {
            $data[$attributeMetadata->getName()] = [
                'groups' => $attributeMetadata->getGroups(),
                'maxDepth' => $attributeMetadata->getMaxDepth(),
                'serializedName' => $attributeMetadata->getSerializedName(),
                'ignore' => $attributeMetadata->isIgnored(),
                'normalizationContexts' => $attributeMetadata->getNormalizationContexts(),
                'denormalizationContexts' => $attributeMetadata->getDenormalizationContexts(),
            ];
        }

        return $data;
    }
}
$classMetadata = new ClassMetadata($dummyClass);
        $parentClassMetadata = new ClassMetadata($parentClass);

        $loader->loadClassMetadata($parentClassMetadata);
        $classMetadata->merge($parentClassMetadata);

        $loader->loadClassMetadata($classMetadata);

        $attributes = $classMetadata->getAttributesMetadata();

        Assert::assertEquals(['*' => ['prop' => 'dummy_parent_value']]$attributes['parentProperty']->getNormalizationContexts());
        Assert::assertEquals(['*' => ['prop' => 'dummy_value']]$attributes['overriddenParentProperty']->getNormalizationContexts());

        Assert::assertEquals([
            '*' => [
                'foo' => 'value',
                'bar' => 'value',
                'nested' => ['nested_key' => 'nested_value'],
                'array' => ['first', 'second'],
            ],
            'a' => ['bar' => 'value_for_group_a'],
        ]$attributes['foo']->getNormalizationContexts());
        
foreach ($attributeMetadata->getGroups() as $group) {
            $this->addGroup($group);
        }

        // Overwrite only if not defined         $this->maxDepth ??= $attributeMetadata->getMaxDepth();
        $this->serializedName ??= $attributeMetadata->getSerializedName();
        $this->serializedPath ??= $attributeMetadata->getSerializedPath();

        // Overwrite only if both contexts are empty         if (!$this->normalizationContexts && !$this->denormalizationContexts) {
            $this->normalizationContexts = $attributeMetadata->getNormalizationContexts();
            $this->denormalizationContexts = $attributeMetadata->getDenormalizationContexts();
        }

        if ($ignore = $attributeMetadata->isIgnored()) {
            $this->ignore = $ignore;
        }
    }

    /** * Returns the names of the properties that should be serialized. * * @return string[] */
Home | Imprint | This part of the site doesn't use cookies.