/**
  * @param $resourceDefinition
  * @param $content
  * @param EntityFactoryContract $factory
  * @param ContextContract $context
  * @return array
  * @throws InvalidContextAction
  */
 public function entitiesFromIdentifiers($resourceDefinition, $content, EntityFactoryContract $factory, ContextContract $context)
 {
     $resourceDefinition = ResourceDefinitionLibrary::make($resourceDefinition);
     if (!Action::isWriteContext($context->getAction())) {
         throw InvalidContextAction::create('Writeable', $context->getAction());
     }
     $out = [];
     if (isset($content[self::RELATIONSHIP_ITEMS])) {
         // This is a list of items
         foreach ($content[self::RELATIONSHIP_ITEMS] as $item) {
             $entity = $this->fromIdentifier($resourceDefinition, $item, $factory, $context);
             if ($entity) {
                 $out[] = $entity;
             }
         }
     } else {
         $entity = $this->fromIdentifier($resourceDefinition, $content, $factory, $context);
         if ($entity) {
             $out[] = $entity;
         }
     }
     return $out;
 }
Esempio n. 2
0
 /**
  * @param string $type
  * @param null $context
  * @return ReturnValue
  * @throws \CatLab\Charon\Exceptions\InvalidContextAction
  */
 public function many($type = null, $context = null) : ReturnValue
 {
     $this->cardinality = 'many';
     if (isset($type)) {
         $this->type = $type;
     }
     if (isset($context)) {
         Action::checkValid($context);
         $this->context = $context;
     }
     return $this;
 }
 /**
  * @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']]]];
     }
 }