/** * Parse provided request. * * @param \eZ\Publish\Core\REST\Common\Message $message * * @return mixed */ public function parse(Message $message) { if (!isset($message->headers['Content-Type'])) { throw new Exceptions\Parser('Missing Content-Type header in message.'); } $mediaTypeParts = explode(';', $message->headers['Content-Type']); $contentTypeParts = explode('+', $mediaTypeParts[0]); if (count($contentTypeParts) !== 2) { // TODO expose default format $contentTypeParts[1] = 'xml'; } $media = $contentTypeParts[0]; $format = $contentTypeParts[1]; if (!isset($this->handlers[$format])) { throw new Exceptions\Parser("Unknown format specification: '{$format}'."); } $rawArray = $this->handlers[$format]->convert($message->body); // Only 1 XML root node $rootNodeArray = reset($rawArray); // @todo: This needs to be refactored in order to make the called URL // available to parsers in the server in a sane way if (isset($message->headers['Url'])) { $rootNodeArray['__url'] = $message->headers['Url']; } if (isset($message->headers['__publish'])) { $rootNodeArray['__publish'] = $message->headers['__publish']; } return $this->parsingDispatcher->parse($rootNodeArray, $media); }
/** * Parses the given $objectElement, if it contains embedded data * * @param array $objectElement * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @return mixed */ public function parseObjectElement(array $objectElement, ParsingDispatcher $parsingDispatcher) { if ($this->isEmbeddedObject($objectElement)) { $parsingDispatcher->parse($objectElement, $objectElement['_media-type']); } return $objectElement['_href']; }
/** * Parse input structure * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @todo Error handling * * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup[] */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { $groups = array(); foreach ($data['ObjectStateGroup'] as $rawGroupData) { $groups[] = $parsingDispatcher->parse($rawGroupData, $rawGroupData['_media-type']); } return $groups; }
/** * Parse input structure. * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @todo Error handling * * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState[] */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { $states = array(); foreach ($data['ObjectState'] as $rawStateData) { $states[] = $parsingDispatcher->parse($rawStateData, $rawStateData['_media-type']); } return $states; }
/** * Parse input structure * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @todo Error handling * * @return \eZ\Publish\API\Repository\Values\User\RoleAssignment[] */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { $roleAssignments = array(); foreach ($data['RoleAssignment'] as $rawRoleAssignmentData) { $roleAssignments[] = $parsingDispatcher->parse($rawRoleAssignmentData, $rawRoleAssignmentData['_media-type']); } return $roleAssignments; }
/** * Parse input structure. * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @todo Error handling * * @return \eZ\Publish\API\Repository\Values\Content\Section[] */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { $sections = array(); foreach ($data['Section'] as $rawSectionData) { $sections[] = $parsingDispatcher->parse($rawSectionData, $rawSectionData['_media-type']); } return $sections; }
/** * Parse input structure. * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @todo Error handling * * @return \eZ\Publish\API\Repository\Values\Content\Section[] */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { $contentTypeGroups = array(); foreach ($data['ContentTypeGroup'] as $rawContentTypeGroupData) { $contentTypeGroups[] = $parsingDispatcher->parse($rawContentTypeGroupData, $rawContentTypeGroupData['_media-type']); } return $contentTypeGroups; }
/** * Parses input structure to a RestViewInput struct. * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser * * @return \eZ\Publish\Core\REST\Server\Values\RestViewInput */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { $valueData = []; if (!array_key_exists('value', $data) || !is_array($data['value'])) { throw new Exceptions\Parser("Missing or invalid data property in SearchHit"); } $value = $data['value']; if (array_key_exists('Content', $value)) { $valueData['valueObject'] = $parsingDispatcher->parse($value['Content'], $value['Content']['_media-type']); } if (array_key_exists('Location', $value)) { $valueData['valueObject'] = $parsingDispatcher->parse($value['Location'], $value['Location']['_media-type']); } $valueData['score'] = $data['_score']; $valueData['index'] = $data['_index']; return new SearchHitValue($valueData); }
public function testParseVersion() { $parserVersionOne = $this->createParserMock(); $parserVersionTwo = $this->createParserMock(); $dispatcher = new Common\Input\ParsingDispatcher(['text/html' => $parserVersionOne, 'text/html; version=2' => $parserVersionTwo]); $parserVersionOne->expects($this->never())->method('parse'); $parserVersionTwo->expects($this->once())->method('parse'); $dispatcher->parse(array(42), 'text/html; version=2'); }
/** * Dispatches parsing of a criterion name + data to its own parser * @param string $criterionName * @param mixed $criterionData * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser * * @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion */ public function dispatchCriterion($criterionName, $criterionData, ParsingDispatcher $parsingDispatcher) { $mediaType = $this->getCriterionMediaType($criterionName); try { return $parsingDispatcher->parse(array($criterionName => $criterionData), $mediaType); } catch (Exceptions\Parser $e) { throw new Exceptions\Parser("Invalid Criterion id <{$criterionName}> in <AND>", 0, $e); } }
/** * Parse input structure. * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @todo Error handling * * @return \eZ\Publish\API\Repository\Values\User\Policy[] */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { $policies = array(); if (array_key_exists('Policy', $data) && is_array($data['Policy'])) { foreach ($data['Policy'] as $rawPolicyData) { $policies[] = $parsingDispatcher->parse($rawPolicyData, $rawPolicyData['_media-type']); } } return $policies; }
/** * Parse input structure * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @return \eZ\Publish\API\Repository\Values\User\RoleAssignment * @todo Error handling * @todo Use dependency injection system for Role Limitation lookup */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { $roleLimitation = null; if (array_key_exists('limitation', $data)) { $limitation = $parsingDispatcher->parse($data['limitation'], $data['limitation']['_media-type']); switch ($limitation->getIdentifier()) { case APILimitation::SECTION: $roleLimitation = new \eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation(); break; case APILimitation::SUBTREE: $roleLimitation = new \eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation(); break; default: throw new \eZ\Publish\Core\Base\Exceptions\NotFoundException('RoleLimitation', $limitation->getIdentifier()); } $roleLimitation->limitationValues = $limitation->limitationValues; } return new Client\Values\User\RoleAssignment(array('role' => $parsingDispatcher->parse($data['Role'], $data['Role']['_media-type']), 'limitation' => $roleLimitation)); }
/** * Parse input structure * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @todo Error handling * * @return \eZ\Publish\API\Repository\Values\User\Policy */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { $limitations = array(); if (array_key_exists('limitations', $data)) { foreach ($data['limitations']['limitation'] as $limitation) { $limitations[] = $parsingDispatcher->parse($limitation, $limitation['_media-type']); } } return new Client\Values\User\Policy(array('id' => $data['id'], 'module' => $data['module'], 'function' => $data['function'], 'limitations' => $limitations)); }
/** * Parses input structure to a RestViewInput struct. * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser * * @return \eZ\Publish\Core\REST\Server\Values\RestViewInput */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { $viewData = []; // identifier if (!array_key_exists('identifier', $data)) { throw new Exceptions\Parser('Missing identifier attribute for ViewInput.'); } $viewData['identifier'] = $data['identifier']; // query if (array_key_exists('ContentQuery', $data) && is_array($data['ContentQuery'])) { $viewData['query'] = $parsingDispatcher->parse($data['Query'], 'application/vnd.ez.api.internal.ContentQuery'); } elseif (array_key_exists('LocationQuery', $data) && is_array($data['LocationQuery'])) { $viewData['query'] = $parsingDispatcher->parse($data['LocationQuery'], 'application/vnd.ez.api.internal.LocationQuery'); } elseif (array_key_exists('Query', $data) && is_array($data['Query'])) { $viewData['query'] = $parsingDispatcher->parse($data['Query'], 'application/vnd.ez.api.internal.ContentQuery'); } else { throw new Exceptions\Parser('Missing or invalid LocationQuery or ContentQuery attribute for View.'); } // results $viewData['result'] = $parsingDispatcher->parse($data['Result'], $data['Result']['_media-type']); return new ViewValue($viewData); }
/** * Parses input structure to a RestViewInput struct. * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser * * @return \eZ\Publish\Core\REST\Server\Values\RestViewInput */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { $restViewInput = new RestViewInput(); // identifier if (!array_key_exists('identifier', $data)) { throw new Exceptions\Parser('Missing <identifier> attribute for <ViewInput>.'); } $restViewInput->identifier = $data['identifier']; // query if (!array_key_exists('Query', $data) || !is_array($data['Query'])) { throw new Exceptions\Parser('Missing <Query> attribute for <ViewInput>.'); } $restViewInput->query = $parsingDispatcher->parse($data['Query'], 'application/vnd.ez.api.internal.ContentQuery'); return $restViewInput; }
/** * Parses input structure to a RestViewInput struct. * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser * * @return \eZ\Publish\Core\REST\Server\Values\RestViewInput */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { $valueData = []; if (!array_key_exists('searchHits', $data) || !is_array($data['searchHits'])) { throw new Exceptions\Parser("Missing or invalid SearchHits in ViewResult data"); } if (!array_key_exists('searchHit', $data['searchHits']) || !is_array($data['searchHits']['searchHit'])) { throw new Exceptions\Parser("Missing or invalid searchHits.searchHit in ViewResult data"); } $valueData['searchHits'] = []; foreach ($data['searchHits']['searchHit'] as $searchHit) { $valueData['searchHits'][] = $parsingDispatcher->parse($searchHit, $searchHit['_media-type']); } return new ViewResultValue($valueData); }
/** * Parses input structure to a RestViewInput struct. * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser * * @return \eZ\Publish\Core\REST\Server\Values\RestViewInput */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { $restViewInput = new RestViewInput(); // identifier if (!array_key_exists('identifier', $data)) { throw new Exceptions\Parser('Missing <identifier> attribute for <ViewInput>.'); } $restViewInput->identifier = $data['identifier']; // query if (array_key_exists('ContentQuery', $data) && is_array($data['ContentQuery'])) { $queryData = $data['ContentQuery']; $queryMediaType = 'application/vnd.ez.api.internal.ContentQuery'; } if (array_key_exists('LocationQuery', $data) && is_array($data['LocationQuery'])) { $queryData = $data['LocationQuery']; $queryMediaType = 'application/vnd.ez.api.internal.LocationQuery'; } if (!isset($queryMediaType) || !isset($queryData)) { throw new Exceptions\Parser('Missing <ContentQuery> or <LocationQuery> attribute for <ViewInput>.'); } $restViewInput->query = $parsingDispatcher->parse($queryData, $queryMediaType); return $restViewInput; }
/** * Parse input structure. * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @return \eZ\Publish\Core\REST\Server\Values\RoleAssignment */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { if (!array_key_exists('Role', $data)) { throw new Exceptions\Parser("Missing 'Role' element for RoleAssignInput."); } if (!is_array($data['Role']) || !array_key_exists('_href', $data['Role'])) { throw new Exceptions\Parser("Invalid 'Role' element for RoleAssignInput."); } try { $roleId = $this->requestParser->parseHref($data['Role']['_href'], 'roleId'); } catch (Exceptions\InvalidArgumentException $e) { throw new Exceptions\Parser('Invalid format for <Role> reference in <RoleAssignInput>.'); } // @todo XSD says that limitation is mandatory, but roles can be assigned without limitations $limitation = null; if (array_key_exists('limitation', $data) && is_array($data['limitation'])) { if (!array_key_exists('_identifier', $data['limitation'])) { throw new Exceptions\Parser("Missing '_identifier' attribute for Limitation."); } $limitation = $parsingDispatcher->parse($data['limitation'], 'application/vnd.ez.api.internal.limitation.' . $data['limitation']['_identifier']); } return new RoleAssignment($roleId, $limitation); }
/** * Dispatches parsing of a sort clause name + direction to its own parser. * * @param string $sortClauseName * @param string $direction * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser * * @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion */ public function dispatchSortClause($sortClauseName, $direction, ParsingDispatcher $parsingDispatcher) { $mediaType = $this->getSortClauseMediaType($sortClauseName); return $parsingDispatcher->parse(array($sortClauseName => $direction), $mediaType); }
/** * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser */ public function testParseMissingContentType() { $dispatcher = new Common\Input\ParsingDispatcher(); $dispatcher->parse(array(), 'text/unknown'); }
/** * Parse input structure * * @param array $data * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher * * @todo Error handling * * @return \eZ\Publish\API\Repository\Values\Content\Location */ public function parse(array $data, ParsingDispatcher $parsingDispatcher) { $content = $parsingDispatcher->parse($data['Content'], 'Content'); return new Values\Content\Location(array('contentInfo' => $content instanceof APIContent ? $content->getVersionInfo()->getContentInfo() : null, 'id' => $data['_href'], 'priority' => (int) $data['priority'], 'hidden' => $data['hidden'] === 'true' ? true : false, 'invisible' => $data['invisible'] === 'true' ? true : false, 'remoteId' => $data['remoteId'], 'parentLocationId' => $data['ParentLocation']['_href'], 'pathString' => $data['pathString'], 'depth' => (int) $data['depth'], 'sortField' => $this->parserTools->parseDefaultSortField($data['sortField']), 'sortOrder' => $this->parserTools->parseDefaultSortOrder($data['sortOrder']))); }