collectDecoding example

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

        $startTime = microtime(true);
        $encoded = $this->encoder->decode($data$format$context);
        $time = microtime(true) - $startTime;

        if ($traceId = ($context[TraceableSerializer::DEBUG_TRACE_ID] ?? null)) {
            $this->dataCollector->collectDecoding($traceId$this->encoder::class$time);
        }

        return $encoded;
    }

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

        
$dataCollector = new SerializerDataCollector();

        $caller = ['name' => 'Foo.php', 'file' => 'src/Foo.php', 'line' => 123];
        $dataCollector->collectEncode('traceIdOne', 'data', 'format', ['foo' => 'bar'], 20.0, $caller);
        $dataCollector->collectDecode('traceIdTwo', 'data', 'format', ['foo' => 'bar'], 20.0, $caller);

        $dataCollector->collectEncoding('traceIdOne', JsonEncoder::class, 1.0);
        $dataCollector->collectEncoding('traceIdOne', JsonEncoder::class, 2.0);
        $dataCollector->collectEncoding('traceIdOne', CsvEncoder::class, 5.0);
        $dataCollector->collectEncoding('traceIdOne', CsvEncoder::class, 10.0);

        $dataCollector->collectDecoding('traceIdTwo', JsonEncoder::class, 1.0);
        $dataCollector->collectDecoding('traceIdTwo', JsonEncoder::class, 2.0);
        $dataCollector->collectDecoding('traceIdTwo', CsvEncoder::class, 5.0);
        $dataCollector->collectDecoding('traceIdTwo', CsvEncoder::class, 10.0);

        $dataCollector->lateCollect();
        $collectedData = $dataCollector->getData();

        $this->assertSame(10.0, $collectedData['encode'][0]['encoder']['time']);
        $this->assertSame('CsvEncoder', $collectedData['encode'][0]['encoder']['class']);
        $this->assertArrayHasKey('file', $collectedData['encode'][0]['encoder']);
        $this->assertArrayHasKey('line', $collectedData['encode'][0]['encoder']);

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

        $startTime = microtime(true);
        $encoded = $this->encoder->decode($data$format$context);
        $time = microtime(true) - $startTime;

        if ($traceId = ($context[TraceableSerializer::DEBUG_TRACE_ID] ?? null)) {
            $this->dataCollector->collectDecoding($traceId$this->encoder::class$time);
        }

        return $encoded;
    }

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

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