/** * 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; }
/** * 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 array(); } $result = array(); switch ($mediaTypeParts['subtype']) { case 'json': case 'x-json': case 'javascript': case 'x-javascript': $result = json_decode($requestBody, TRUE); if ($result === NULL) { return array(); } break; case 'xml': try { $xmlElement = new \SimpleXMLElement(urldecode($requestBody), LIBXML_NOERROR); } catch (\Exception $e) { return array(); } $result = Arrays::convertObjectToArray($xmlElement); break; case 'x-www-form-urlencoded': default: parse_str($requestBody, $result); break; } return $result; }
/** * @test * @dataProvider mediaTypesAndParsedPieces */ public function parseMediaTypeReturnsAssociativeArrayWithIndividualPartsOfTheMediaType($mediaType, $expectedPieces) { $request = $this->getAccessibleMock(\TYPO3\Flow\Http\Request::class, array('dummy'), array(), '', FALSE); $actualPieces = MediaTypes::parseMediaType($mediaType); $this->assertSame($expectedPieces, $actualPieces); }
/** * Update the resource on an asset. * * @param AssetInterface $asset * @param FlowResource $resource * @param array $options * @throws InvalidArgumentValueException * @return void */ public function updateAssetResourceAction(AssetInterface $asset, FlowResource $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); }
/** * Parses a RFC 2616 Media Type and returns its parts in an associative array. * @see \TYPO3\Flow\Utility\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 \TYPO3\Flow\Utility\MediaTypes::parseMediaType() instead */ public static function parseMediaType($rawMediaType) { return MediaTypes::parseMediaType($rawMediaType); }