Example #1
0
 /**
  * @inheritdoc
  */
 public function withRelationshipRelatedLink($resource, $relationshipName, $meta = null, $treatAsHref = false)
 {
     $parentSubLink = $this->container->getSchema($resource)->getSelfSubLink($resource);
     $selfHref = $parentSubLink->getSubHref() . '/' . $relationshipName;
     $links = [DocumentInterface::KEYWORD_RELATED => $this->factory->createLink($selfHref, $meta, $treatAsHref)];
     return $this->withLinks($links);
 }
Example #2
0
 /**
  * @param object     $resource
  * @param string     $key
  * @param string     $prefix
  * @param string     $name
  * @param null|mixed $meta
  * @param bool       $treatAsHref
  *
  * @return EncoderInterface
  */
 private function getRelationshipLink($resource, $key, $prefix, $name, $meta = null, $treatAsHref = false)
 {
     $parentSubLink = $this->container->getSchema($resource)->getSelfSubLink($resource);
     $selfHref = $parentSubLink->getSubHref() . $prefix . '/' . $name;
     $links = [$key => $this->factory->createLink($selfHref, $meta, $treatAsHref)];
     return $this->withLinks($links);
 }
Example #3
0
 /**
  * @inheritdoc
  */
 public function analyze($data)
 {
     $isCollection = true;
     $schema = null;
     $traversableData = null;
     $firstItem = null;
     if (is_array($data) === true) {
         /** @var array $data */
         $isEmpty = empty($data);
         $traversableData = $data;
         if ($isEmpty === false) {
             $firstItem = reset($data);
         }
     } elseif ($data instanceof Iterator) {
         /** @var Iterator $data */
         $data->rewind();
         $isEmpty = $data->valid() === false;
         if ($isEmpty === false) {
             $firstItem = $data->current();
             $traversableData = $data;
         } else {
             $traversableData = [];
         }
     } elseif (is_object($data) === true) {
         /** @var object $data */
         $isEmpty = $data === null;
         $isCollection = false;
         $firstItem = $data;
         $traversableData = [$data];
     } elseif ($data === null) {
         $isCollection = false;
         $isEmpty = true;
     } else {
         throw new InvalidArgumentException('data');
     }
     if ($firstItem !== null) {
         $schema = $this->container->getSchema($firstItem);
     }
     return [$isEmpty, $isCollection, $schema, $traversableData];
 }
Example #4
0
 /**
  * @param array|object|null                $data
  * @param EncodingParametersInterface|null $parameters
  *
  * @return EncodingParametersInterface
  */
 private function getEncodingParameters($data, EncodingParametersInterface $parameters = null)
 {
     if (empty($data) === true && $parameters === null) {
         return $this->parametersFactory->createEncodingParameters();
     } elseif ($parameters !== null && $parameters->getIncludePaths() !== null) {
         return $parameters;
     } else {
         $schema = $this->container->getSchema(is_array($data) ? $data[0] : $data);
         $includePaths = $schema->getIncludePaths();
         $fieldSets = $parameters === null ? null : $parameters->getFieldSets();
         return $this->parametersFactory->createEncodingParameters($includePaths, $fieldSets);
     }
 }
Example #5
0
 /**
  * @param array|object|null $data
  *
  * @return Iterator
  */
 private function parseData($data)
 {
     $curFrame = $this->stack->end();
     if (empty($data) === true) {
         (yield $this->createReplyForEmptyData($data));
     } else {
         if (is_array($data) === true) {
             $isOriginallyArrayed = true;
             $schema = $this->container->getSchema($data[0]);
         } else {
             $isOriginallyArrayed = false;
             $schema = $this->container->getSchema($data);
             $data = [$data];
         }
         // duplicated are allowed in data however they shouldn't be in includes
         $isDupAllowed = $curFrame->getLevel() < 2;
         $fieldSet = $this->getFieldSet($schema->getResourceType());
         foreach ($data as $resource) {
             $resourceObject = $schema->createResourceObject($resource, $isOriginallyArrayed, $fieldSet);
             $isCircular = $this->checkCircular($resourceObject);
             $this->stack->setCurrentResource($resourceObject);
             (yield $this->createReplyResourceStarted());
             if ($isCircular === true && $isDupAllowed === false || $this->shouldParseRelationships($resourceObject, $isCircular) === false) {
                 continue;
             }
             foreach ($schema->getRelationshipObjectIterator($resource) as $relationship) {
                 /** @var RelationshipObjectInterface $relationship */
                 $nextFrame = $this->stack->push();
                 $nextFrame->setRelationship($relationship);
                 try {
                     foreach ($this->parseData($relationship->getData()) as $parseResult) {
                         (yield $parseResult);
                     }
                 } finally {
                     $this->stack->pop();
                 }
             }
             (yield $this->createReplyResourceCompleted());
         }
     }
 }
 /**
  * @inheritdoc
  */
 public function getSchema($resourceObject)
 {
     return $this->getSchemaAdapter($this->container->getSchema($resourceObject));
 }
Example #7
0
 /**
  * @param mixed                       $resource
  * @param StackFrameReadOnlyInterface $frame
  *
  * @return SchemaProviderInterface
  */
 private function getSchema($resource, StackFrameReadOnlyInterface $frame)
 {
     try {
         $schema = $this->container->getSchema($resource);
     } catch (InvalidArgumentException $exception) {
         $message = T::t('Schema is not registered for a resource at path \'%s\'.', [$frame->getPath()]);
         throw new InvalidArgumentException($message, 0, $exception);
     }
     return $schema;
 }
Example #8
0
 /**
  * @param mixed $resource
  *
  * @return SchemaProviderInterface
  */
 private function getSchema($resource)
 {
     return $this->container->getSchema($resource);
 }
Example #9
0
 /**
  * @param object                                                             $resource
  * @param array<string,\Neomerx\JsonApi\Contracts\Schema\LinkInterface>|null $links
  * @param mixed                                                              $meta
  *
  * @return Response
  */
 protected function getCreatedResponse($resource, $links = null, $meta = null)
 {
     $parameters = $this->getParameters();
     $encoder = $this->codecMatcher->getEncoder();
     $outputMediaType = $this->codecMatcher->getEncoderRegisteredMatchedType();
     $content = $encoder->encode($resource, $links, $meta, $parameters);
     $urlPrefix = $encoder->getEncoderOptions() === null ? null : $encoder->getEncoderOptions()->getUrlPrefix();
     $location = $urlPrefix . $this->schemaContainer->getSchema($resource)->getSelfSubLink($resource)->getSubHref();
     return $this->responses->getCreatedResponse($location, $outputMediaType, $content, $this->supportedExtensions);
 }
Example #10
0
 /**
  * @inheritdoc
  */
 public function withRelationshipRelatedLink($resource, $relationshipName, $meta = null, $treatAsHref = false)
 {
     $link = $this->container->getSchema($resource)->getRelationshipRelatedLink($resource, $relationshipName, $meta, $treatAsHref);
     return $this->withLinks([DocumentInterface::KEYWORD_RELATED => $link]);
 }