Esempio n. 1
0
 /**
  * @test
  */
 public function convertObjectToArrayConvertsNestedObjectsToArray()
 {
     $object = new \stdClass();
     $object->a = 'v';
     $object->b = new \stdClass();
     $object->b->c = 'w';
     $object->d = ['i' => 'foo', 'j' => 12, 'k' => true, 'l' => new \stdClass()];
     $array = Arrays::convertObjectToArray($object);
     $expected = ['a' => 'v', 'b' => ['c' => 'w'], 'd' => ['i' => 'foo', 'j' => 12, 'k' => true, 'l' => []]];
     $this->assertEquals($expected, $array);
 }
 /**
  * Converts the given request body according to the specified media type
  * Override this method in your custom TypeConverter to support additional media types
  *
  * @param string $requestBody the raw request body
  * @param string $mediaType the configured media type (for example "application/json")
  * @return array
  * @api
  */
 protected function convertMediaType($requestBody, $mediaType)
 {
     $mediaTypeParts = MediaTypes::parseMediaType($mediaType);
     if (!isset($mediaTypeParts['subtype']) || $mediaTypeParts['subtype'] === '') {
         return [];
     }
     $result = [];
     switch ($mediaTypeParts['subtype']) {
         case 'json':
         case 'x-json':
         case 'javascript':
         case 'x-javascript':
             $result = json_decode($requestBody, true);
             if ($result === null) {
                 return [];
             }
             break;
         case 'xml':
             $entityLoaderValue = libxml_disable_entity_loader(true);
             try {
                 $xmlElement = new \SimpleXMLElement(urldecode($requestBody), LIBXML_NOERROR);
                 libxml_disable_entity_loader($entityLoaderValue);
             } catch (\Exception $exception) {
                 libxml_disable_entity_loader($entityLoaderValue);
                 return [];
             }
             $result = Arrays::convertObjectToArray($xmlElement);
             break;
         case 'x-www-form-urlencoded':
         default:
             parse_str($requestBody, $result);
             break;
     }
     return $result;
 }