/** * @param ResourceTransformer $transformer * @param PropertyResolver $propertyResolver * @param $parent * @param PropertyValues $identifiers * @param Context $context * @return mixed */ protected function getChildByIdentifiers(ResourceTransformer $transformer, PropertyResolver $propertyResolver, &$parent, PropertyValues $identifiers, Context $context) { $childEntity = $propertyResolver->resolveProperty($transformer, $parent, $this->getField(), $context); if (!$childEntity || !$propertyResolver->doesResourceRepresentEntity($transformer, $childEntity, $this->child, $context)) { $childEntity = null; } return $childEntity; }
/** * @param ResourceDefinition|string $resourceDefinition * @param mixed $entity * @param ContextContract $context * @param RelationshipValue $parent * @param null $parentEntity * @return ResourceContract * @throws InvalidContextAction * @throws InvalidEntityException * @throws InvalidPropertyException */ public function toResource($resourceDefinition, $entity, ContextContract $context, RelationshipValue $parent = null, $parentEntity = null) : ResourceContract { $resourceDefinition = ResourceDefinitionLibrary::make($resourceDefinition); $this->checkEntityType($resourceDefinition, $entity); if (!Action::isReadContext($context->getAction())) { throw InvalidContextAction::create('Readable', $context->getAction()); } if ($resourceDefinition instanceof DynamicContext) { $resourceDefinition->transformContext($context, $entity); } if ($entity instanceof DynamicContext) { $entity->transformContext($context, $entity); } $resource = new RESTResource($resourceDefinition); $fields = $resourceDefinition->getFields(); $this->parents->push($entity); foreach ($fields as $field) { $this->currentPath->push($field); $visible = $this->shouldInclude($field, $context); if ($visible || $field->isSortable()) { if ($field instanceof RelationshipField) { if ($this->shouldExpand($field, $context)) { $this->expandRelationship($field, $entity, $resource, $context, $visible); } else { $this->linkRelationship($field, $entity, $resource, $context, $visible); } } else { $resource->setProperty($field, $this->propertyResolver->resolveProperty($this, $entity, $field, $context), $visible); } } $this->currentPath->pop(); } $context->getProcessors()->processResource($this, $resource, $resourceDefinition, $context, $parent, $parentEntity); $this->parents->pop(); return $resource; }