/** * @param $format * * @return array|mixed */ private function decode($format) { $data = $this->decoder->decode((string) $this->apiContext->getResponse()->getBody(), $format); if ($format === 'xml') { $data = !empty($data) ? is_int(key($data['entry'])) ? $data['entry'] : [$data['entry']] : []; } return $data; }
public function decode($data, $format) { $context = []; if ($format === 'json') { // the JSON schema validator need an object hierarchy $context['json_decode_associative'] = false; } $decoded = $this->decoder->decode($data, $format, $context); if ($format === 'xml') { // the JSON schema validator need an object hierarchy $decoded = json_decode(json_encode($decoded)); } return $decoded; }
/** * @param $format * * @return array|mixed */ private function decode($format) { $data = $this->decoder->decode((string) $this->apiContext->getResponse()->getBody(), $format); if ($format === 'json' && isset($data['_embedded']['items'])) { $data = $data['_embedded']['items']; } if ($format === 'xml') { if (!empty($data) && isset($data['entry'])) { if (is_int(key($data['entry']))) { $data = $data['entry']; } elseif (isset($data['entry']['@rel'])) { $data = array_pop($data['entry']); } else { $data = [$data['entry']]; } } else { $data = []; } } return $data; }
/** * Decodes the request body * * @param Request $request The request * * @return void */ protected function decodeBody(Request $request) { if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH', 'DELETE'])) { $contentType = $request->headers->get('Content-Type', ''); $format = $this->formatNegotiator->getFormat($contentType); if (!$this->contentDecoder->supportsDecoding($format)) { return; } $content = $request->getContent(); if (!empty($content)) { try { $data = $this->contentDecoder->decode($content, $format); } catch (Exception $exception) { $data = null; } if (is_array($data)) { $request->request->replace($data); } else { $message = sprintf('Invalid %s message received', $format); throw new BadRequestHttpException($message); } } } }
/** * @param string $format * @param EncoderInterface|DecoderInterface $encoder * @param int $iterations * @param mixed $data * * @return self */ public static function create($format, $encoder, $iterations, $data) { $bench = new self(); /*Raw****************************************************************************/ $bench->format = $format; list($keysLength, $dataLength) = $bench->measure($data); $bench->dataTime = $bench->measureTime($iterations, array($bench, 'measure'), array($data)); $bench->dataLength = $keysLength + $dataLength; /*Encoded************************************************************************/ $encoded = $encoder->encode($data, $format); $bench->encodedLength = strlen($encoded); $bench->encodingTime = $bench->measureTime($iterations, array($encoder, 'encode'), array($data, $format)); $bench->decodingTime = $bench->measureTime($iterations, array($encoder, 'decode'), array($encoded, $format)); /*GZip***************************************************************************/ $gzipEncoded = gzencode($encoded); $bench->gzipEncodedLength = strlen($gzipEncoded); $bench->gzipEncodingTime = $bench->encodingTime + $bench->measureTime($iterations, 'gzencode', array($encoded)); $bench->gzipDecodingTime = $bench->decodingTime + $bench->measureTime($iterations, 'gzdecode', array($gzipEncoded)); /*BZip***************************************************************************/ $bzipEncoded = bzcompress($encoded); $bench->bzipEncodedLength = strlen($bzipEncoded); $bench->bzipEncodingTime = $bench->encodingTime + $bench->measureTime($iterations, 'bzcompress', array($encoded)); $bench->bzipDecodingTime = $bench->decodingTime + $bench->measureTime($iterations, 'bzdecompress', array($bzipEncoded)); return $bench; }
/** * @param string $format format name * @param DecoderInterface $decoder */ public function setDecoder($format, DecoderInterface $decoder) { $this->decoders[$format] = $decoder; if ($decoder instanceof SerializerAwareInterface) { $decoder->setSerializer($this); } }
/** * {@inheritdoc} */ public function decode($data, array $context = []) { return $this->serializer->decode($data, JsonEncoder::FORMAT, $context); }