Ejemplo n.º 1
0
 /**
  * @return \DataDefinition\Data\ValueInterface
  */
 public function execute()
 {
     $key = $this->field->getName();
     if (isset($this->object[$key])) {
         return $this->object->getValue($key);
     }
     return new NullValue();
 }
Ejemplo n.º 2
0
 /**
  * Apply this patch to an object.
  *
  * @param \DataDefinition\Data\ObjectInterface $object
  *
  * @return \DataDefinition\Data\ObjectInterface
  */
 public function applyObject(ObjectInterface $object)
 {
     // Get the newly modified structure.
     $structure = $this->applyStructure($object->getStructure());
     // Get the original values from the object.
     $values = array();
     foreach ($object as $key => $value) {
         $values[$key] = $value->getValue();
     }
     // Get the name and the default value for the field.
     $name = $this->field->getName();
     $value = $this->field->getValue()->getValue();
     // Apply the patching.
     switch ($this->type) {
         case 'add':
             if (!array_key_exists($name, $values)) {
                 $values[$name] = $value;
             }
             break;
         case 'remove':
             if (array_key_exists($name, $values)) {
                 unset($values[$name]);
             }
             break;
         case 'replace':
             if (array_key_exists($name, $values)) {
                 $values[$name] = $value;
             }
             break;
         case 'copy':
             $original = $this->original->getName();
             if (array_key_exists($original, $values) && !array_key_exists($name, $values)) {
                 $values[$name] = $values[$original];
             }
             break;
         case 'move':
             $original = $this->original->getName();
             if (array_key_exists($original, $values) && !array_key_exists($name, $values)) {
                 $values[$name] = $values[$original];
                 unset($values[$original]);
             }
             break;
     }
     // Create a new object.
     $class = get_class($object);
     $patched = new $class($structure, $object->getIdentifier(), $object->getOptions());
     // Apply new values to the patched object.
     foreach ($values as $key => $value) {
         $patched[$key] = $value;
     }
     // Return the newly patched object.
     return $patched;
 }
 /**
 * Add
 *
 *@param \DataDefinition\Data\FieldInterface $field
 *
 * @return void
 */
 protected function addField(FieldInterface $field)
 {
     $this->patches[] = new Patch('add', $field, new NullField($field->getName()));
     $this->fields[$field->getName()] = $field;
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function getValue()
 {
     return (new ArrayValue($this->field->getValue(), $this->constructor))->setConfig($this->getConfig());
 }