public function validate(Model $model, $data)
 {
     // First validate the base
     if (!parent::validate($model, $data)) {
         return false;
     }
     if (isset($data) && isset($this->childProperty)) {
         $okay = true;
         foreach ($data as $v) {
             if (!$this->childProperty->validate($model, $v)) {
                 $okay = false;
             }
         }
         return $okay;
     } else {
         return true;
     }
 }
 public function validate(Model $model, $data)
 {
     // First validate the base
     if (!parent::validate($model, $data)) {
         return false;
     }
     if (isset($data)) {
         return $this->model->validate($data);
     } else {
         return true;
     }
 }
 public function testDimensions()
 {
     $input = array('name' => 'This is my name', 'user' => array('id' => 1, 'name' => 'Thijs'));
     $validator = new Validator();
     $model = new Model('testModel');
     $validator->addModel($model);
     $property = new Property('name');
     $property->addRequirement(new IsType('string'));
     $model->addProperty($property);
     // Submodel
     $userModel = new Model('user');
     $property = new Property('id');
     $property->addRequirement(new IsType('numeric'));
     $userModel->addProperty($property);
     $property = new Property('name');
     $property->addRequirement(new IsType('string'));
     $userModel->addProperty($property);
     $property = new ModelProperty($userModel);
     $property->addRequirement(new Exists());
     $model->addProperty($property);
     $this->assertEquals(true, $validator->validate('testModel', $input));
 }
 /**
  * @param $name
  * @param $property
  * @return Property
  */
 private function getPropertyFromInput($name, $property)
 {
     // Is this property an ArrayProperty?
     if (substr($name, -2) === '[]' || substr($name, -3) === '[]?') {
         return $this->getArrayPropertyFromInput($name, $property);
     } else {
         if (is_array($property)) {
             // If plain array, (not assoc), this is a list of filters(
             if (isset($property[0])) {
                 // Property name is numeric; this is a filter.
                 // Make a property and continue.
                 return Property::make($name, $property);
             } else {
                 // Is this model optional or required?
                 $required = true;
                 if (substr($name, -1) === '?') {
                     $name = substr($name, 0, -1);
                     $required = false;
                 }
                 // Create a model property (with a nice prefix)
                 $prefix = $this->getPrefix() . $name . '.';
                 // Make the model
                 $model = self::make($name, $property, $prefix);
                 // Make the model property
                 $prop = new ModelProperty($model);
                 // Remember when we checked for the "?"
                 if ($required) {
                     $prop->addRequirement(new Exists());
                 }
                 // Return the model property
                 return $prop;
             }
         } else {
             return Property::make($name, $property);
         }
     }
 }
 /**
  * @param Model $model
  * @param Property $property
  * @return string
  */
 protected function getPropertyName(Model $model, Property $property)
 {
     return $model->getPrefix() . $property->getName();
 }
 private function getModel($id, $prefix = null, $depth = 0)
 {
     if (!isset($this->library[$id])) {
         throw new ModelNotDefined("Model " . $id . " was not found in swagger specifications.");
     }
     $data = $this->library[$id];
     $model = new Model($data['id']);
     $model->setPrefix($prefix);
     // Has reached depth? In that case, ignore teh properties.
     if ($this->hasReachedDepth($depth)) {
         return $model;
     }
     /** @var Property[] $properties */
     $properties = array();
     if (isset($data['properties'])) {
         foreach ($data['properties'] as $propName => $property) {
             if (!empty($property['$ref'])) {
                 $subModel = $this->getModel($property['$ref'], $model->getPrefix() . $property['$ref'] . '.', $depth + 1);
                 $properties[$propName] = new ModelProperty($subModel, $propName);
                 $model->addProperty($properties[$propName]);
             } else {
                 if (isset($property['$ref'])) {
                     // Do nothing.
                     throw new \InvalidArgumentException("Empty reference found.");
                 } else {
                     if (isset($property['type']) && strtolower($property['type']) == 'array') {
                         // Is array specified?
                         if (isset($property['items'])) {
                             if (isset($property['items']['$ref'])) {
                                 $subModel = $this->getModel($property['items']['$ref'], $model->getPrefix() . $property['items']['$ref'] . '[].', $depth + 1);
                                 $childProperty = new ModelProperty($subModel, $propName);
                             } else {
                                 if (isset($property['items']['type'])) {
                                     $childProperty = new Property($propName);
                                     $childProperty->addRequirement(new IsType($property['items']['type']));
                                 } else {
                                     throw new \InvalidArgumentException('Array without type or $ref found.');
                                 }
                             }
                             // Nope? Regular array.
                             $properties[$propName] = new ArrayProperty($propName, $childProperty);
                             $model->addProperty($properties[$propName]);
                             $properties[$propName]->addRequirement(new IsArray());
                         } else {
                             // Nope? Regular array.
                             $properties[$propName] = new Property($propName);
                             $model->addProperty($properties[$propName]);
                             $properties[$propName]->addRequirement(new IsArray());
                         }
                     } else {
                         $properties[$propName] = new Property($propName);
                         $model->addProperty($properties[$propName]);
                         if (isset($property['type'])) {
                             $properties[$propName]->addRequirement(new IsType($property['type']));
                         }
                     }
                 }
             }
         }
     }
     // Requirements
     if (isset($data['required'])) {
         foreach ($data['required'] as $v) {
             $properties[$v]->addRequirement(new Exists());
         }
     }
     return $model;
 }
 /**
  * @param $value
  * @param Property $property
  * @return bool
  */
 public function validate($value, Property $property)
 {
     // Guess the type
     $type = $property->getType();
     return $this->convertValue($this->value, $type) == $this->convertValue($value, $type);
 }