needsNormalization example


        $this->encoder = new ChainEncoder($realEncoders);
        $this->decoder = new ChainDecoder($decoders);
    }

    final public function serialize(mixed $data, string $format, array $context = []): string
    {
        if (!$this->supportsEncoding($format$context)) {
            throw new UnsupportedFormatException(sprintf('Serialization for the format "%s" is not supported.', $format));
        }

        if ($this->encoder->needsNormalization($format$context)) {
            $data = $this->normalize($data$format$context);
        }

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

    final public function deserialize(mixed $data, string $type, string $format, array $context = []): mixed
    {
        if (!$this->supportsDecoding($format$context)) {
            throw new UnsupportedFormatException(sprintf('Deserialization for the format "%s" is not supported.', $format));
        }

        

        $this->encoder = new ChainEncoder($realEncoders);
        $this->decoder = new ChainDecoder($decoders);
    }

    final public function serialize(mixed $data, string $format, array $context = []): string
    {
        if (!$this->supportsEncoding($format$context)) {
            throw new UnsupportedFormatException(sprintf('Serialization for the format "%s" is not supported.', $format));
        }

        if ($this->encoder->needsNormalization($format$context)) {
            $data = $this->normalize($data$format$context);
        }

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

    final public function deserialize(mixed $data, string $type, string $format, array $context = []): mixed
    {
        if (!$this->supportsDecoding($format$context)) {
            throw new UnsupportedFormatException(sprintf('Deserialization for the format "%s" is not supported.', $format));
        }

        
return true;
    }

    /** * Checks whether the normalization is needed for the given format. */
    public function needsNormalization(string $format, array $context = []): bool
    {
        $encoder = $this->getEncoder($format$context);

        if ($encoder instanceof TraceableEncoder) {
            return $encoder->needsNormalization();
        }

        if (!$encoder instanceof NormalizationAwareInterface) {
            return true;
        }

        if ($encoder instanceof self) {
            return $encoder->needsNormalization($format$context);
        }

        return false;
    }
$this->assertSame('foo:123', $this->chainEncoder->encode(['foo' => 123], self::FORMAT_2));
    }

    public function testEncodeUnsupportedFormat()
    {
        $this->expectException(RuntimeException::class);
        $this->chainEncoder->encode(['foo' => 123], self::FORMAT_3);
    }

    public function testNeedsNormalizationBasic()
    {
        $this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_1));
        $this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_2));
    }

    public function testNeedsNormalizationNormalizationAware()
    {
        $encoder = new NormalizationAwareEncoder();
        $sut = new ChainEncoder([$encoder]);

        $this->assertFalse($sut->needsNormalization(self::FORMAT_1));
    }

    
return true;
    }

    /** * Checks whether the normalization is needed for the given format. */
    public function needsNormalization(string $format, array $context = []): bool
    {
        $encoder = $this->getEncoder($format$context);

        if ($encoder instanceof TraceableEncoder) {
            return $encoder->needsNormalization();
        }

        if (!$encoder instanceof NormalizationAwareInterface) {
            return true;
        }

        if ($encoder instanceof self) {
            return $encoder->needsNormalization($format$context);
        }

        return false;
    }
Home | Imprint | This part of the site doesn't use cookies.