/** * @param mixed $other * @return bool */ public function matches($other) { $this->validator = new Validator(); $this->validator->addModel($this->model); $valid = $this->validator->validate($this->model->getName(), $other); return $valid; }
public function addModel(Model $model) { if (isset($this->models[$model->getName()])) { throw new ModelAlreadyDefined("Model " . $model->getName() . " is already defined."); } // Set error container $model->setErrors($this->errors); // Add the model. $this->models[$model->getName()] = $model; }
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; } }
/** * @param Model $model * @param Property $property * @return string */ protected function getPropertyName(Model $model, Property $property) { return $model->getPrefix() . $property->getName(); }
/** * @test */ public function testSimpleOptionalArray() { $validator = new Validator(); $model = Model::make('ArrayModel', array('id' => 'required|int', 'collection' => array('count' => 'int|required', 'items[]?' => 'int'))); $validator->addModel($model); $correctData = array('id' => 1, 'collection' => array('count' => 2, 'items' => array(1, 2, 3))); $this->assertTrue($validator->validate('ArrayModel', $correctData)); $incorrectData = array('id' => 1, 'collection' => array('count' => 2)); $this->assertTrue($validator->validate('ArrayModel', $incorrectData)); }
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; }
<?php use CatLab\Validator\Models\Model; use CatLab\Validator\Validator; require '../vendor/autoload.php'; $validator = new Validator(); $model = Model::make('ArrayModel', array('id' => 'required|int', 'collection' => array('count' => 'int|required', 'items[]' => 'int'))); $validator->addModel($model); $correctData = array('id' => 1, 'collection' => array('count' => 2, 'items' => array(1, 2, 3))); var_dump($validator->validate('ArrayModel', $correctData));