示例#1
0
 /**
  * @throws \WoohooLabs\Yin\JsonApi\Exception\QueryParamUnrecognized
  */
 public function validateQueryParams()
 {
     foreach ($this->getQueryParams() as $queryParamName => $queryParamValue) {
         if (preg_match("/^([a-z]+)\$/", $queryParamName) && in_array($queryParamName, ["fields", "include", "sort", "page", "filter"]) === false) {
             throw $this->exceptionFactory->createQueryParamUnrecognizedException($this, $queryParamName);
         }
     }
 }
示例#2
0
 /**
  * @param mixed $domainObject
  * @param array $data
  * @param \WoohooLabs\Yin\JsonApi\Exception\ExceptionFactoryInterface $exceptionFactory
  * @return mixed
  * @throws \Exception
  */
 protected function hydrateIdForUpdate($domainObject, $data, ExceptionFactoryInterface $exceptionFactory)
 {
     if (empty($data["id"])) {
         throw $exceptionFactory->createResourceIdMissingException();
     }
     $result = $this->setId($domainObject, $data["id"]);
     if ($result) {
         $domainObject = $result;
     }
     return $domainObject;
 }
示例#3
0
 /**
  * Hydrates the domain object from the creating request.
  *
  * The domain object's attributes and relationships are hydrated
  * according to the JSON API specification.
  *
  * @param \WoohooLabs\Yin\JsonApi\Request\RequestInterface $request
  * @param \WoohooLabs\Yin\JsonApi\Exception\ExceptionFactoryInterface $exceptionFactory
  * @param mixed $domainObject
  * @return mixed
  * @throws \Exception
  */
 public function hydrateForCreate(RequestInterface $request, ExceptionFactoryInterface $exceptionFactory, $domainObject)
 {
     $data = $request->getResource();
     if ($data === null) {
         throw $exceptionFactory->createDataMemberMissingException($request);
     }
     $this->validateType($data, $exceptionFactory);
     $domainObject = $this->hydrateIdForCreate($domainObject, $data, $request, $exceptionFactory);
     $domainObject = $this->hydrateAttributes($domainObject, $data);
     $domainObject = $this->hydrateRelationships($domainObject, $data, $exceptionFactory);
     return $domainObject;
 }
示例#4
0
 /**
  * @param array $array
  * @param ExceptionFactoryInterface $exceptionFactory
  * @return $this
  * @throw \Exception
  */
 public static function fromArray(array $array, ExceptionFactoryInterface $exceptionFactory)
 {
     if (isset($array["type"]) === false) {
         throw $exceptionFactory->createResourceIdentifierTypeMissing($array);
     }
     if (isset($array["id"]) === false) {
         throw $exceptionFactory->createResourceIdentifierIdMissing($array);
     }
     $resourceIdentifier = new self();
     $resourceIdentifier->setType($array["type"]);
     $resourceIdentifier->setId($array["id"]);
     if (isset($array["meta"]) === true) {
         $resourceIdentifier->setMeta($array["meta"]);
     }
     return $resourceIdentifier;
 }
示例#5
0
 /**
  * @param string $relationshipName
  * @param \Closure $hydrator
  * @param mixed $domainObject
  * @param ToOneRelationship|ToManyRelationship $relationshipObject
  * @param array $data
  * @param \WoohooLabs\Yin\JsonApi\Exception\ExceptionFactoryInterface $exceptionFactory
  * @return mixed
  * @throws \WoohooLabs\Yin\JsonApi\Exception\RelationshipTypeInappropriate
  * @throws \Exception
  */
 protected function getRelationshipHydratorResult($relationshipName, \Closure $hydrator, $domainObject, $relationshipObject, array $data, ExceptionFactoryInterface $exceptionFactory)
 {
     // Checking if the current and expected relationship types match
     $relationshipType = $this->getRelationshipType($relationshipObject);
     $expectedRelationshipType = $this->getRelationshipType($this->getArgumentTypeHintFromClosure($hydrator));
     if ($expectedRelationshipType !== null && $relationshipType !== $expectedRelationshipType) {
         throw $exceptionFactory->createRelationshipTypeInappropriateException($relationshipName, $relationshipType, $expectedRelationshipType);
     }
     // Returning if the hydrator returns the hydrated domain object
     $value = $hydrator($domainObject, $relationshipObject, $data);
     if ($value) {
         return $value;
     }
     // Returning the domain object which was mutated but not returned by the hydrator
     return $domainObject;
 }
示例#6
0
文件: JsonApi.php 项目: garethwi/yin
 /**
  * Disables sorting.
  *
  * If the current request contains sorting criteria, it throws a SortingNotSupported exception.
  *
  * @throws \WoohooLabs\Yin\JsonApi\Exception\SortingUnsupported
  */
 public function disableSorting()
 {
     if ($this->request->getQueryParam("sort") !== null) {
         throw $this->exceptionFactory->createSortingUnsupportedException($this->request);
     }
 }