supportsDecoding example

public function testEncodeEmptyArrayObject()
    {
        $value = new \ArrayObject();
        $this->assertEquals("\n", $this->encoder->encode($value, 'csv'));

        $value = ['foo' => new \ArrayObject()];
        $this->assertEquals("\n\n", $this->encoder->encode($value, 'csv'));
    }

    public function testSupportsDecoding()
    {
        $this->assertTrue($this->encoder->supportsDecoding('csv'));
        $this->assertFalse($this->encoder->supportsDecoding('foo'));
    }

    public function testDecodeAsSingle()
    {
        $expected = ['foo' => 'a', 'bar' => 'b'];

        $this->assertEquals($expected$this->encoder->decode(<<<'CSV' foo,bar a,b CSV
            ,


    public function testSupportsDecoding()
    {
        $this->decoder1
            ->method('decode')
            ->willReturn('result1');
        $this->decoder2
            ->method('decode')
            ->willReturn('result2');

        $this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_1));
        $this->assertEquals('result1', $this->chainDecoder->decode('', self::FORMAT_1, []));

        $this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_2));
        $this->assertEquals('result2', $this->chainDecoder->decode('', self::FORMAT_2, []));

        $this->assertFalse($this->chainDecoder->supportsDecoding(self::FORMAT_3));

        $this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_3, ['foo' => 'bar']));
        $this->assertEquals('result1', $this->chainDecoder->decode('', self::FORMAT_3, ['foo' => 'bar']));

        $this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_3, ['foo' => 'bar2']));
        


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

        $data = $this->decode($data$format$context);

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

    public function normalize(mixed $data, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
    {
        // If a normalizer supports the given data, use it


        return $encoded;
    }

    public function supportsDecoding(string $format, array $context = []): bool
    {
        if (!$this->encoder instanceof DecoderInterface) {
            return false;
        }

        return $this->encoder->supportsDecoding($format$context);
    }

    public function setSerializer(SerializerInterface $serializer): void
    {
        if (!$this->encoder instanceof SerializerAwareInterface) {
            return;
        }

        $this->encoder->setSerializer($serializer);
    }

    
private function getDecoder(string $format, array $context): DecoderInterface
    {
        if (isset($this->decoderByFormat[$format])
            && isset($this->decoders[$this->decoderByFormat[$format]])
        ) {
            return $this->decoders[$this->decoderByFormat[$format]];
        }

        $cache = true;
        foreach ($this->decoders as $i => $decoder) {
            $cache = $cache && !$decoder instanceof ContextAwareDecoderInterface;
            if ($decoder->supportsDecoding($format$context)) {
                if ($cache) {
                    $this->decoderByFormat[$format] = $i;
                }

                return $decoder;
            }
        }

        throw new RuntimeException(sprintf('No decoder found for format "%s".', $format));
    }
}

        $encoder = new YamlEncoder();

        $this->assertEquals('foo', $encoder->decode('foo', 'yaml'));
        $this->assertEquals(['foo' => 1]$encoder->decode('{ foo: 1 }', 'yaml'));
    }

    public function testSupportsDecoding()
    {
        $encoder = new YamlEncoder();

        $this->assertTrue($encoder->supportsDecoding('yaml'));
        $this->assertTrue($encoder->supportsDecoding('yml'));
        $this->assertFalse($encoder->supportsDecoding('json'));
    }

    public function testIndentation()
    {
        $encoder = new YamlEncoder(null, null, [YamlEncoder::YAML_INLINE => 100, YamlEncoder::YAML_INDENTATION => 7]);

        $expected = <<<'END' foo: bar: baz
private function getDecoder(string $format, array $context): DecoderInterface
    {
        if (isset($this->decoderByFormat[$format])
            && isset($this->decoders[$this->decoderByFormat[$format]])
        ) {
            return $this->decoders[$this->decoderByFormat[$format]];
        }

        $cache = true;
        foreach ($this->decoders as $i => $decoder) {
            $cache = $cache && !$decoder instanceof ContextAwareDecoderInterface;
            if ($decoder->supportsDecoding($format$context)) {
                if ($cache) {
                    $this->decoderByFormat[$format] = $i;
                }

                return $decoder;
            }
        }

        throw new RuntimeException(sprintf('No decoder found for format "%s".', $format));
    }
}

  public function testSupportsEncoding() {
    $this->assertTrue($this->encoder->supportsEncoding('xml'));
    $this->assertFalse($this->encoder->supportsEncoding('json'));
  }

  /** * Tests the supportsDecoding() method. */
  public function testSupportsDecoding() {
    $this->assertTrue($this->encoder->supportsDecoding('xml'));
    $this->assertFalse($this->encoder->supportsDecoding('json'));
  }

  /** * Tests the encode() method. */
  public function testEncode() {
    $this->baseEncoder->expects($this->once())
      ->method('encode')
      ->with($this->testArray, 'test', [])
      ->willReturn('test');

    
class JsonDecodeTest extends TestCase
{
    private JsonDecode $decode;

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

    public function testSupportsDecoding()
    {
        $this->assertTrue($this->decode->supportsDecoding(JsonEncoder::FORMAT));
        $this->assertFalse($this->decode->supportsDecoding('foobar'));
    }

    /** * @dataProvider decodeProvider */
    public function testDecode($toDecode$expected$context)
    {
        $this->assertEquals(
            $expected,
            $this->decode->decode($toDecode, JsonEncoder::FORMAT, $context)
        );

        return $this->serializer->supportsDenormalization($data$type$format$context);
    }

    public function supportsEncoding(string $format, array $context = []): bool
    {
        return $this->serializer->supportsEncoding($format$context);
    }

    public function supportsDecoding(string $format, array $context = []): bool
    {
        return $this->serializer->supportsDecoding($format$context);
    }

    /** * Proxies all method calls to the original serializer. */
    public function __call(string $method, array $arguments): mixed
    {
        return $this->serializer->{$method}(...$arguments);
    }

    private function getCaller(string $method, string $interface): array
    {

        $encoder = $this->createMock(EncoderInterface::class);
        $encoder->method('supportsEncoding')->willReturn(true);

        $decoder = $this->createMock(DecoderInterface::class);
        $decoder->method('supportsDecoding')->willReturn(true);

        $traceableEncoder = new TraceableEncoder($encodernew SerializerDataCollector());
        $traceableDecoder = new TraceableEncoder($decodernew SerializerDataCollector());

        $this->assertTrue($traceableEncoder->supportsEncoding('data'));
        $this->assertTrue($traceableDecoder->supportsDecoding('data'));
        $this->assertFalse($traceableEncoder->supportsDecoding('data'));
        $this->assertFalse($traceableDecoder->supportsEncoding('data'));
    }
}


        return $encoded;
    }

    public function supportsDecoding(string $format, array $context = []): bool
    {
        if (!$this->encoder instanceof DecoderInterface) {
            return false;
        }

        return $this->encoder->supportsDecoding($format$context);
    }

    public function setSerializer(SerializerInterface $serializer): void
    {
        if (!$this->encoder instanceof SerializerAwareInterface) {
            return;
        }

        $this->encoder->setSerializer($serializer);
    }

    
class JsonApiDecoderTest extends TestCase
{
    private JsonApiDecoder $decoder;

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

    public function testSupportFormat(): void
    {
        static::assertTrue($this->decoder->supportsDecoding('jsonapi'));
        static::assertFalse($this->decoder->supportsDecoding('JSONAPI'));
        static::assertFalse($this->decoder->supportsDecoding('yml'));
    }

    public static function emptyInputProvider(): array
    {
        return [
            [null],
            ['string'],
            [1],
            [false],
            [

        return $this->serializer->supportsDenormalization($data$type$format$context);
    }

    public function supportsEncoding(string $format, array $context = []): bool
    {
        return $this->serializer->supportsEncoding($format$context);
    }

    public function supportsDecoding(string $format, array $context = []): bool
    {
        return $this->serializer->supportsDecoding($format$context);
    }

    /** * Proxies all method calls to the original serializer. */
    public function __call(string $method, array $arguments): mixed
    {
        return $this->serializer->{$method}(...$arguments);
    }

    private function getCaller(string $method, string $interface): array
    {


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

        $data = $this->decode($data$format$context);

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

    public function normalize(mixed $data, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
    {
        // If a normalizer supports the given data, use it
Home | Imprint | This part of the site doesn't use cookies.