/**
  * @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;
 }
 /**
  * @param SwaggerBuilder $builder
  * @param $action
  * @return mixed[]
  */
 public function toSwagger(SwaggerBuilder $builder, $action)
 {
     if (Action::isReadContext($action) && $this->isExpanded()) {
         return ['type' => 'object', 'schema' => $builder->getRelationshipSchema(ResourceDefinitionLibrary::make($this->childResource), $this->getExpandContext(), $this->cardinality)];
     } elseif (Action::isWriteContext($action)) {
         if ($this->linkOnly) {
             return ['type' => 'object', 'schema' => $builder->getRelationshipSchema(ResourceDefinitionLibrary::make($this->childResource), Action::IDENTIFIER, $this->cardinality)];
         } else {
             return ['type' => 'object', 'schema' => $builder->getRelationshipSchema(ResourceDefinitionLibrary::make($this->childResource), Action::CREATE, $this->cardinality)];
         }
     } else {
         return ['type' => 'object', 'schema' => ['properties' => [ResourceTransformer::RELATIONSHIP_LINK => ['type' => 'string']]]];
     }
 }