/**
  * @param ResourceTransformer $transformer
  * @param &$input
  * @param Field $field
  * @param Context $context
  * @return mixed
  */
 public function resolvePropertyInput(ResourceTransformer $transformer, &$input, Field $field, Context $context)
 {
     if (isset($input[$field->getDisplayName()])) {
         return $input[$field->getDisplayName()];
     }
     return null;
 }
 /**
  * @param ResourceTransformer $transformer
  * @param array $parameters
  * @param Context $context
  * @param Field $field
  * @param null $entity
  * @return \mixed[]
  * @throws VariableNotFoundInContext
  */
 protected function parseParameters(ResourceTransformer $transformer, array $parameters, Context $context, Field $field = null, $entity = null)
 {
     $out = [];
     foreach ($parameters as $v) {
         if ($parameter = $this->getParameter($v)) {
             $value = $this->parseParameter($transformer, $parameter, $context, $entity);
             if ($value !== null) {
                 $out[] = $value;
             } else {
                 if ($field) {
                     throw new VariableNotFoundInContext('Field ' . $field->getName() . ' requires a parameter $' . $parameter . ' to be set in the context, but no such parameter was defined.');
                 } else {
                     throw new VariableNotFoundInContext('A parameter $' . $parameter . ' is required to be set in the context, but no such parameter was defined.');
                 }
             }
         } else {
             $out[] = $v;
         }
     }
     return $out;
 }
 /**
  * @param $path
  * @param Field $field
  * @return string
  */
 private function appendToPath($path, Field $field)
 {
     $display = $field->getDisplayName();
     if ($field instanceof RelationshipField) {
         if ($field->getCardinality() === Cardinality::MANY) {
             $display .= '[]';
         }
     }
     if (!empty($path)) {
         return $path . '.' . $display;
     } else {
         return $display;
     }
 }
Example #4
0
 public function push(Field $field)
 {
     $this->fields[] = $field;
     $this->displayNames[] = $field->getDisplayName();
 }
 /**
  * @param Field $field
  * @param ContextContract $context
  * @return bool
  */
 private function isWritable(Field $field, Context $context)
 {
     return $field->shouldInclude($context, $this->currentPath);
 }
 /**
  * @param ResourceTransformer $transformer
  * @param $entity
  * @param Field $field
  * @param Context $context
  * @return array
  * @throws InvalidPropertyException
  */
 protected function resolvePath(ResourceTransformer $transformer, $entity, Field $field, Context $context)
 {
     $path = $this->splitPathParameters($field->getName());
     $name = array_pop($path);
     // If the water is deep enough, we need to first fetch the corresponding entity.
     // We will NOT create the entity if it doesn't exist.
     // Relationships support this functionality. Regular setters do not.
     if (count($path) > 0) {
         $entity = $this->resolveChildPath($transformer, $entity, $path, $field, $context);
     }
     list($name, $parameters) = $this->getPropertyNameAndParameters($transformer, $name, $context, $field, $entity);
     return [$entity, $name, $parameters];
 }
 /**
  * @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);
 }
 public function __construct(ResourceDefinition $resourceDefinition, $fieldName)
 {
     parent::__construct($resourceDefinition, $fieldName);
     $this->sortable = false;
 }
Example #9
0
 /**
  * Set a value in an entity
  * @param $entity
  * @param ResourceTransformer $resourceTransformer
  * @param PropertyResolver $propertyResolver
  * @param PropertySetter $propertySetter
  * @param EntityFactory $factory
  * @param Context $context
  */
 public function toEntity($entity, ResourceTransformer $resourceTransformer, PropertyResolver $propertyResolver, PropertySetter $propertySetter, EntityFactory $factory, Context $context)
 {
     if ($this->field->canSetProperty()) {
         $propertySetter->setEntityValue($resourceTransformer, $entity, $this->field, $this->value, $context);
     }
 }