supportsNormalization example

public function setFallbackNormalizer(NormalizerInterface $normalizer) {
    $this->fallbackNormalizer = $normalizer;
  }

  /** * {@inheritdoc} */
  public function normalize($data$format = NULL, array $context = []): array|string|int|float|bool|\ArrayObject|NULL {
    if ($this->selfSupportsNormalization($data$format$context)) {
      return parent::normalize($data$format$context);
    }
    if ($this->fallbackNormalizer->supportsNormalization($data$format$context)) {
      return $this->fallbackNormalizer->normalize($data$format$context);
    }
    return parent::normalize($data$format$context);
  }

  /** * {@inheritdoc} */
  public function denormalize($data$type$format = NULL, array $context = []): mixed {
    if ($this->selfSupportsDenormalization($data$type$format$context)) {
      return parent::denormalize($data$type$format$context);
    }
/** * Parse the data and convert it to DOMElements. * * @throws NotEncodableValueException */
    private function buildXml(\DOMNode $parentNode, mixed $data, string $format, array $context, string $xmlRootNodeName = null): bool
    {
        $append = true;
        $removeEmptyTags = $context[self::REMOVE_EMPTY_TAGS] ?? $this->defaultContext[self::REMOVE_EMPTY_TAGS] ?? false;
        $encoderIgnoredNodeTypes = $context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES];

        if (\is_array($data) || ($data instanceof \Traversable && (null === $this->serializer || !$this->serializer->supportsNormalization($data$format)))) {
            foreach ($data as $key => $data) {
                // Ah this is the magic @ attribute types.                 if (str_starts_with($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
                    if (!\is_scalar($data)) {
                        $data = $this->serializer->normalize($data$format$context);
                    }
                    if (\is_bool($data)) {
                        $data = (int) $data;
                    }
                    $parentNode->setAttribute($attributeName$data);
                } elseif ('#' === $key) {
                    
'%RP%yY%mM%dD', '+P10Y2M3D', '+P10Y2M3DT0H'],
            ['%RP%yY%mM%dD', '+P10Y2M3D', 'P10Y2M3DT0H'],
            ['%rP%yY%mM%dD', '-P10Y2M3D', '-P10Y2M3DT0H'],
            ['%rP%yY%mM%dD', 'P10Y2M3D', 'P10Y2M3DT0H'],
        ];

        return $data;
    }

    public function testSupportsNormalization()
    {
        $this->assertTrue($this->normalizer->supportsNormalization(new \DateInterval('P00Y00M00DT00H00M00S')));
        $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
    }

    public function testNormalize()
    {
        $this->assertEquals('P0Y0M0DT0H0M0S', $this->normalizer->normalize(new \DateInterval('PT0S')));
    }

    /** * @dataProvider dataProviderISO */
    
$this->form->method('getErrors')
            ->willReturn(new FormErrorIterator($this->form, [
                new FormError('a', 'b', ['c', 'd'], 5, 'f'),
                new FormError(1, 2, [3, 4], 5, 6),
            ])
            );
    }

    public function testSupportsNormalizationWithWrongClass()
    {
        $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
    }

    public function testSupportsNormalizationWithNotSubmittedForm()
    {
        $form = $this->createMock(FormInterface::class);
        $this->assertFalse($this->normalizer->supportsNormalization($form));
    }

    public function testSupportsNormalizationWithValidForm()
    {
        $this->assertTrue($this->normalizer->supportsNormalization($this->form));
    }
protected function setUp(): void {
    parent::setUp();

    $this->normalizer = new ComplexDataNormalizer();
  }

  /** * @covers ::supportsNormalization */
  public function testSupportsNormalization() {
    $complex_data = $this->prophesize(ComplexDataInterface::class)->reveal();
    $this->assertTrue($this->normalizer->supportsNormalization($complex_data));
    // Also test that an object not implementing ComplexDataInterface fails.     $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
  }

  /** * Tests normalizing complex data. * * @covers ::normalize */
  public function testNormalizeComplexData() {
    $serializer_prophecy = $this->prophesize(Serializer::class);

    
class DateTimeNormalizerTest extends TestCase
{
    private DateTimeNormalizer $normalizer;

    protected function setUp(): void
    {
        $this->normalizer = new DateTimeNormalizer();
    }

    public function testSupportsNormalization()
    {
        $this->assertTrue($this->normalizer->supportsNormalization(new \DateTime()));
        $this->assertTrue($this->normalizer->supportsNormalization(new \DateTimeImmutable()));
        $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
    }

    public function testNormalize()
    {
        $this->assertEquals('2016-01-01T00:00:00+00:00', $this->normalizer->normalize(new \DateTime('2016/01/01', new \DateTimeZone('UTC'))));
        $this->assertEquals('2016-01-01T00:00:00+00:00', $this->normalizer->normalize(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC'))));
    }

    public function testNormalizeUsingFormatPassedInContext()
    {


        return $normalized;
    }

    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
    {
        if (!$this->normalizer instanceof NormalizerInterface) {
            return false;
        }

        return $this->normalizer->supportsNormalization($data$format$context);
    }

    public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
    {
        if (!$this->normalizer instanceof DenormalizerInterface) {
            throw new \BadMethodCallException(sprintf('The "%s()" method cannot be called as nested normalizer doesn\'t implements "%s".', __METHOD__, DenormalizerInterface::class));
        }

        $startTime = microtime(true);
        $denormalized = $this->normalizer->denormalize($data$type$format$context);
        $time = microtime(true) - $startTime;

        
$this->normalizer = new ListNormalizer();

    $this->list = new ItemList(new DataDefinition());
    $this->list->setValue($this->expectedListValues);
  }

  /** * Tests the supportsNormalization() method. */
  public function testSupportsNormalization() {
    $this->assertTrue($this->normalizer->supportsNormalization($this->list));
    $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
  }

  /** * Tests the normalize() method. */
  public function testNormalize() {
    $serializer = $this->prophesize(Serializer::class);
    $serializer->normalize($this->typedData, 'json', ['mu' => 'nu'])
      ->shouldBeCalledTimes(3)
      ->willReturn('test');

    
foreach ($this->normalizers as $k => $normalizer) {
                if (!$normalizer instanceof NormalizerInterface) {
                    continue;
                }

                if (!method_exists($normalizer, 'getSupportedTypes')) {
                    trigger_deprecation('symfony/serializer', '6.3', '"%s" should implement "NormalizerInterface::getSupportedTypes(?string $format): array".', $normalizer::class);

                    if (!$normalizer instanceof CacheableSupportsMethodInterface || !$normalizer->hasCacheableSupportsMethod()) {
                        $this->normalizerCache[$format][$type][$k] = false;
                    } elseif ($normalizer->supportsNormalization($data$format$context)) {
                        $this->normalizerCache[$format][$type][$k] = true;
                        break;
                    }

                    continue;
                }

                $supportedTypes = $normalizer->getSupportedTypes($format);

                foreach ($supportedTypes as $supportedType => $isCacheable) {
                    if (\in_array($supportedType['*', 'object'], true)
                        
protected function setUp(): void {
    parent::setUp();

    $this->normalizer = new PrimitiveDataNormalizer();
  }

  /** * @covers ::supportsNormalization * @dataProvider dataProviderPrimitiveData */
  public function testSupportsNormalization($primitive_data$expected) {
    $this->assertTrue($this->normalizer->supportsNormalization($primitive_data));
  }

  /** * @covers ::supportsNormalization */
  public function testSupportsNormalizationFail() {
    // Test that an object not implementing PrimitiveInterface fails.     $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
  }

  /** * @covers ::normalize * @dataProvider dataProviderPrimitiveData */
$this->normalizer = new DataUriNormalizer();
    }

    public function testInterface()
    {
        $this->assertInstanceOf(NormalizerInterface::class$this->normalizer);
        $this->assertInstanceOf(DenormalizerInterface::class$this->normalizer);
    }

    public function testSupportNormalization()
    {
        $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
        $this->assertTrue($this->normalizer->supportsNormalization(new \SplFileObject('data:,Hello%2C%20World!')));
    }

    /** * @requires extension fileinfo */
    public function testNormalizeHttpFoundationFile()
    {
        $file = new File(__DIR__.'/../Fixtures/test.gif');

        $this->assertSame(self::TEST_GIF_DATA, $this->normalizer->normalize($file));
    }
parent::setUp();

    $this->normalizer = new NullNormalizer($this->interface);
  }

  /** * @covers ::__construct * @covers ::supportsNormalization */
  public function testSupportsNormalization() {
    $mock = $this->createMock('Drupal\Core\TypedData\TypedDataInterface');
    $this->assertTrue($this->normalizer->supportsNormalization($mock));
    // Also test that an object not implementing TypedDataInterface fails.     $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
  }

  /** * @covers ::normalize */
  public function testNormalize() {
    $mock = $this->createMock('Drupal\Core\TypedData\TypedDataInterface');
    $this->assertNull($this->normalizer->normalize($mock));
  }

}
public function testDenormalizeNonExistingAttribute()
    {
        $this->assertEquals(
            new ObjectDummy(),
            $this->normalizer->denormalize(['non_existing' => true], ObjectDummy::class)
        );
    }

    public function testNoTraversableSupport()
    {
        $this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
    }

    public function testNormalizeStatic()
    {
        $this->assertEquals(['foo' => 'K']$this->normalizer->normalize(new ObjectWithStaticPropertiesAndMethods()));
    }

    public function testNormalizeUpperCaseAttributes()
    {
        $this->assertEquals(['Foo' => 'Foo', 'Bar' => 'BarBar']$this->normalizer->normalize(new ObjectWithUpperCaseAttributeNames()));
    }

    
$config_factory->get('system.date')
      ->willReturn($system_date_config->reveal());

    $this->normalizer = new DateTimeNormalizer($config_factory->reveal());
    $this->data = $this->prophesize(DateTimeInterface::class);
  }

  /** * @covers ::supportsNormalization */
  public function testSupportsNormalization() {
    $this->assertTrue($this->normalizer->supportsNormalization($this->data->reveal()));

    $datetimeiso8601 = $this->prophesize(DateTimeIso8601::class);
    $this->assertTrue($this->normalizer->supportsNormalization($datetimeiso8601->reveal()));

    $integer = $this->prophesize(IntegerData::class);
    $this->assertFalse($this->normalizer->supportsNormalization($integer->reveal()));
  }

  /** * @covers ::supportsDenormalization */
  
protected function setUp(): void {
    parent::setUp();

    $this->normalizer = new TimestampItemNormalizer();
  }

  /** * @covers ::supportsNormalization */
  public function testSupportsNormalization() {
    $timestamp_item = $this->createTimestampItemProphecy();
    $this->assertTrue($this->normalizer->supportsNormalization($timestamp_item->reveal()));

    $entity_ref_item = $this->prophesize(EntityReferenceItem::class);
    $this->assertFalse($this->normalizer->supportsNormalization($entity_ref_item->reveal()));
  }

  /** * @covers ::supportsDenormalization */
  public function testSupportsDenormalization() {
    $timestamp_item = $this->createTimestampItemProphecy();
    $this->assertTrue($this->normalizer->supportsDenormalization($timestamp_item->reveal(), TimestampItem::class));

    
Home | Imprint | This part of the site doesn't use cookies.