/**
  * @param mixed $value
  *
  * @return mixed
  */
 protected function transformRecursive($value)
 {
     if (is_array($value) || $value instanceof \Traversable) {
         foreach ($value as &$subvalue) {
             $subvalue = $this->transformRecursive($subvalue);
         }
     } else {
         $value = $this->transformer->transform($value);
     }
     return $value;
 }
 /**
  * @inheritdoc
  */
 public function transform($value)
 {
     // not an array, must be traversable
     if (!is_array($value) && !$value instanceof \Traversable) {
         throw new TransformationFailedException(sprintf('Expected an array or \\Traversable to transform, got "%s" instead.', gettype($value)));
     }
     // traverse through object, transforming each item
     foreach ($value as $key => $val) {
         $value[$key] = $this->transformer->transform($val);
     }
     return $value;
 }
Example #3
0
 /**
  * @param ParameterBag $item
  *
  * @throws TransformationFailedException
  */
 public function transform(ParameterBag $item)
 {
     if (!$item->has($this->field)) {
         return;
     }
     $value = $item->get($this->field);
     try {
         $newValue = $this->transformer->transform($value);
         $item->set($this->field, $newValue);
     } catch (TransformationFailedException $e) {
         // set the value to null as we couldn't transform it
         $item->set($this->field, null);
         throw new TransformationFailedException(sprintf('Transforming "%s" using "%s" failed with message: %s.', $this->field, get_class($this->transformer), $e->getMessage()), null, $e);
     }
 }