Exemplo n.º 1
0
 public function testIsAddingTransformer()
 {
     $typeHandler = new TypeHandler([], ['my_datetime' => new DateTimeTransformer()]);
     $this->assertEquals(new \DateTime('2014-01-01 00:00:01'), $typeHandler->convertType('my_datetime', '2014-01-01 00:00:01'));
 }
Exemplo n.º 2
0
 protected function walk($node, $requestData, $parent = null)
 {
     $response = [];
     /*
      * TODO
      *
      * This loop needs some serious refactoring. Each node should have
      * it's own class (ArrayNode, ObjectNode, etc.) and handle data
      * properly by implementing methods from the NodeInterface.
      *
      * Only constraints, type checks and conversions should be handled in
      * this loop!
      */
     foreach ($node as $key => $item) {
         if (!isset($requestData[$key])) {
             if ($item->isRequired()) {
                 $this->errors[] = sprintf('"%s" is required', $key);
             }
             continue;
         }
         if ($item->isMixed()) {
             $response[$key] = $item;
         }
         if ($item->isCollection()) {
             $collectionType = $item->getCollectionType();
             foreach ($requestData[$key] as $requestItem) {
                 $instance = new $collectionType();
                 $this->walk($item, $requestItem, $instance);
                 if ($parent) {
                     $method = 'add' . Inflector::classify(Inflector::singularize($key));
                     $parent->{$method}($instance);
                 } else {
                     $response[$key][] = $instance;
                 }
             }
             continue;
         }
         if ($item->isScalarCollection()) {
             $scalarData = $requestData[$key];
             foreach ($scalarData as $value) {
                 if (!call_user_func('is_' . $item->getCollectionType(), $value)) {
                     $this->errors[] = sprintf('Value "%s", from "%s", is not of type %s', $value, $key, $item->getCollectionType());
                 }
             }
             if ($parent) {
                 $method = 'set' . Inflector::classify($key);
                 $parent->{$method}($scalarData);
             } else {
                 $response[$key] = $scalarData;
             }
             continue;
         }
         if ($item->hasChildren()) {
             $response[$key] = $this->walk($item, $requestData[$key]);
             continue;
         }
         if ($item->isObject()) {
             $class = $item->getType();
             $instance = new $class();
             $this->walk($item, $requestData[$key], $instance);
             if ($parent) {
                 $method = 'set' . Inflector::classify($key);
                 $parent->{$method}($instance);
             } else {
                 $response[$key] = $instance;
             }
             continue;
         }
         if (!$this->typeHandler->checkType($item->getType(), $requestData[$key])) {
             $this->errors[] = sprintf('"%s" should be of type %s, %s received.', $key, $item->getType(), gettype($requestData[$key]));
             continue;
         }
         foreach ($item->getConstraints() as $contraint) {
             if (!$contraint->validate($requestData[$key])) {
                 $this->errors[] = sprintf('"%s" constraint: %s', $key, $contraint->getErrorMessage());
                 continue;
             }
         }
         $value = $this->typeHandler->convertType($item->getType(), $requestData[$key]);
         if ($parent) {
             $method = 'set' . Inflector::classify($key);
             $parent->{$method}($value);
         } else {
             $response[$key] = $value;
         }
     }
     return $response;
 }