getDenormalizationContexts example

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;
    }
}
$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[] */
Assert::assertEquals([
            '*' => [
                'foo' => 'value',
                'bar' => 'value',
                'nested' => ['nested_key' => 'nested_value'],
                'array' => ['first', 'second'],
            ],
            'a' => ['bar' => 'value_for_group_a'],
        ]$attributes['foo']->getNormalizationContexts());
        Assert::assertSame(
            $attributes['foo']->getNormalizationContexts(),
            $attributes['foo']->getDenormalizationContexts()
        );

        Assert::assertEquals([
            'a' => $c = ['format' => 'd/m/Y'],
            'b' => $c,
        ]$attributes['bar']->getNormalizationContexts());
        Assert::assertEquals([
            'a' => $c = ['format' => 'm-d-Y H:i'],
            'b' => $c,
        ]$attributes['bar']->getDenormalizationContexts());

        
 $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());
    }

    public function testGetContextsForGroups()
    {
        $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']$metadata->getNormalizationContextForGroups([]));
        
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;
    }
}
$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.