/** * Create a resource from a data array * @param $resourceDefinition * @param array $body * @param ContextContract $context * @return ResourceContract * @throws InvalidPropertyException * @throws InvalidContextAction */ public function fromArray($resourceDefinition, array $body, ContextContract $context) : ResourceContract { $resourceDefinition = ResourceDefinitionLibrary::make($resourceDefinition); if (!Action::isWriteContext($context->getAction())) { throw InvalidContextAction::create('Writeable', $context->getAction()); } $resource = new RESTResource($resourceDefinition); $fields = $resourceDefinition->getFields(); foreach ($fields as $field) { $this->currentPath->push($field); if ($this->isWritable($field, $context)) { if ($field instanceof RelationshipField) { $this->relationshipFromArray($field, $body, $resource, $context); } else { $value = $this->propertyResolver->resolvePropertyInput($this, $body, $field, $context); $resource->setProperty($field, $value, true); } } $this->currentPath->pop(); } return $resource; }
/** * @param Context $context * @param CurrentPath $currentPath * @return bool */ public function shouldInclude(Context $context, CurrentPath $currentPath) { // Check for max depth. if ($maxDepth = $this->getMaxDepth()) { $existing = $currentPath->countSame($this); if ($existing > $maxDepth) { return false; } } return parent::shouldInclude($context, $currentPath); }
/** * @param CurrentPath $fieldPath * @return bool|null */ public function shouldExpandField(CurrentPath $fieldPath) { if (count($this->fieldsToExpand) === 0) { return null; } return $this->arrayPathExists($this->fieldsToExpand, $fieldPath->toArray()); }