decode() public method

Decodes a string into PHP data
public decode ( scalar $data, string $format ) : mixed
$data scalar Data to decode
$format string Format name
return mixed
Beispiel #1
0
 /**
  * @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;
 }
Beispiel #3
0
 /**
  * @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);
             }
         }
     }
 }
Beispiel #5
0
 /**
  * {@inheritdoc}
  */
 public function decode($data, array $context = [])
 {
     return $this->serializer->decode($data, JsonEncoder::FORMAT, $context);
 }