Exemplo n.º 1
0
 /**
  * Transform an incoming value, which is described by the given attributespec,
  * to it's input (entity compatible) representation and set result on the given entity.
  *
  * @param mixed $input_value
  * @param EntityInterface $entity
  * @param SpecificationInterface $specification
  *
  * @return void
  */
 public function revert($input_value, EntityInterface $entity, SpecificationInterface $specification)
 {
     $attribute_name = $specification->getOption('attribute', $specification->getName());
     $entity->setValue($attribute_name, $input_value);
 }
Exemplo n.º 2
0
 /**
  * Sets either given default value or value from option to the given attribute.
  *
  * @param Entity $entity the entity to modify
  * @param string $attribute_name the name of the attribute to set a value for
  * @param mixed $default_value Default value to set.
  * @param array $options Array containing a `attribute_name => $mixed` entry.
  *                       $mixed is set as value instead of $default_value.
  *                       If $mixed is a closure it will be called and used.
  *                       $mixed may also be another callable like an array
  *                       `array($class, "$methodName")` or a string like
  *                       `'Your\Namespace\Foo::getStaticTrololo'`.
  *
  * @return void
  */
 protected function setValue(EntityInterface $entity, AttributeInterface $attribute, $default_value, array $options = array())
 {
     $attribute_name = $attribute->getName();
     $attribute_options = array();
     if (!empty($options[self::OPTION_FIELD_VALUES]) && is_array($options[self::OPTION_FIELD_VALUES])) {
         $attribute_options = $options[self::OPTION_FIELD_VALUES];
     }
     if (empty($attribute_options[$attribute_name])) {
         $entity->setValue($attribute_name, $default_value);
     } else {
         $option = $attribute_options[$attribute_name];
         if (is_callable($option)) {
             $entity->setValue($attribute_name, call_user_func($option));
         } else {
             $entity->setValue($attribute_name, $option);
         }
     }
 }