/** * 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; }
/** * Parses a RFC 2616 content negotiation header field by evaluating the Quality * Values and splitting the options into an array list, ordered by user preference. * * @param string $rawValues The raw Accept* Header field value * @return array The parsed list of field values, ordered by user preference */ public static function parseContentNegotiationQualityValues($rawValues) { $acceptedTypes = array_map(function ($acceptType) { $typeAndQuality = preg_split('/;\\s*q=/', $acceptType); return [$typeAndQuality[0], isset($typeAndQuality[1]) ? (double) $typeAndQuality[1] : '']; }, preg_split('/,\\s*/', $rawValues)); $flattenedAcceptedTypes = []; $valuesWithoutQualityValue = [[], [], [], []]; foreach ($acceptedTypes as $typeAndQuality) { if ($typeAndQuality[1] === '') { $parsedType = MediaTypes::parseMediaType($typeAndQuality[0]); if ($parsedType['type'] === '*') { $valuesWithoutQualityValue[3][$typeAndQuality[0]] = true; } elseif ($parsedType['subtype'] === '*') { $valuesWithoutQualityValue[2][$typeAndQuality[0]] = true; } elseif ($parsedType['parameters'] === []) { $valuesWithoutQualityValue[1][$typeAndQuality[0]] = true; } else { $valuesWithoutQualityValue[0][$typeAndQuality[0]] = true; } } else { $flattenedAcceptedTypes[$typeAndQuality[0]] = $typeAndQuality[1]; } } $valuesWithoutQualityValue = array_merge(array_keys($valuesWithoutQualityValue[0]), array_keys($valuesWithoutQualityValue[1]), array_keys($valuesWithoutQualityValue[2]), array_keys($valuesWithoutQualityValue[3])); arsort($flattenedAcceptedTypes); $parsedValues = array_merge($valuesWithoutQualityValue, array_keys($flattenedAcceptedTypes)); return $parsedValues; }
/** * Update the resource on an asset. * * @param AssetInterface $asset * @param PersistentResource $resource * @param array $options * @throws InvalidArgumentValueException * @return void */ public function updateAssetResourceAction(AssetInterface $asset, PersistentResource $resource, array $options = []) { $sourceMediaType = MediaTypes::parseMediaType($asset->getMediaType()); $replacementMediaType = MediaTypes::parseMediaType($resource->getMediaType()); // Prevent replacement of image, audio and video by a different mimetype because of possible rendering issues. if (in_array($sourceMediaType['type'], ['image', 'audio', 'video']) && $sourceMediaType['type'] !== $replacementMediaType['type']) { $this->addFlashMessage('Resources of type "%s" can only be replaced by a similar resource. Got type "%s"', '', Message::SEVERITY_WARNING, [$sourceMediaType['type'], $resource->getMediaType()], 1462308179); $this->redirect('index'); } parent::updateAssetResourceAction($asset, $resource, $options); }
/** * @test * @dataProvider mediaTypesAndParsedPieces */ public function parseMediaTypeReturnsAssociativeArrayWithIndividualPartsOfTheMediaType($mediaType, $expectedPieces) { $request = $this->getAccessibleMock(Request::class, ['dummy'], [], '', false); $actualPieces = MediaTypes::parseMediaType($mediaType); $this->assertSame($expectedPieces, $actualPieces); }
/** * Parses a RFC 2616 Media Type and returns its parts in an associative array. * @see MediaTypes::parseMediaType() * * @param string $rawMediaType The raw media type, for example "application/json; charset=UTF-8" * @return array An associative array with parsed information * @deprecated since Flow 2.1. Use \Neos\Utility\MediaTypes::parseMediaType() instead */ public static function parseMediaType($rawMediaType) { return MediaTypes::parseMediaType($rawMediaType); }