示例#1
0
 public function testBuildOk()
 {
     $mockSchema = $this->getMockBuilder(SchemaInterface::class)->getMock();
     $mockSchema->expects($this->once())->method('validate')->will($this->returnValue(Ok::unit()));
     $builder = new Builder($mockSchema);
     $result = $builder->firstname('clark')->lastname('kent')->email('*****@*****.**')->tags(['hero', 'security'])->address->street('fleetstreet 23')->zipCode('23542')->city('melmac')->build(['username' => 'superuser', 'email' => '*****@*****.**']);
     $expectedData = ['username' => 'superuser', 'firstname' => 'clark', 'lastname' => 'kent', 'email' => '*****@*****.**', 'tags' => ['hero', 'security'], 'address' => ['street' => 'fleetstreet 23', 'zipCode' => '23542', 'city' => 'melmac']];
     $this->assertInstanceOf(Ok::class, $result);
     $this->assertEquals($expectedData, $result->unwrap());
 }
示例#2
0
 /**
  * Tells if a given value adhere's to one of the property's allowed types.
  *
  * @param mixed $value
  *
  * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned.
  */
 public function validate($value)
 {
     $result = Ok::unit();
     foreach ($this->allowedTypes as $type) {
         $typeChecker = preg_match('/^&/', $type) ? $this->getCustomType($type) : $this->createProperty($type);
         $result = $typeChecker->validate($value);
         if ($result instanceof Ok) {
             return $result;
         }
     }
     return $result;
 }
示例#3
0
 /**
  * Tells if a given array's items adhere to any of the property's allowed types.
  *
  * @param mixed $value
  *
  * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned.
  */
 public function validate($value)
 {
     if (!is_array($value)) {
         return Error::unit([Error::NON_ARRAY]);
     }
     $errors = [];
     foreach ($value as $pos => $item) {
         $result = parent::validate($item);
         if ($result instanceof Error) {
             $errors[$pos] = $result->unwrap();
         }
     }
     return empty($errors) ? Ok::unit() : Error::unit($errors);
 }
示例#4
0
 /**
  * Tells if a given value is a valid int.
  *
  * @param mixed $value
  *
  * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned.
  */
 public function validate($value)
 {
     return is_int($value) ? Ok::unit() : Error::unit([Error::NON_INT]);
 }
示例#5
0
 /**
  * Validation stub that always returns ok.
  *
  * @param mixed $value
  *
  * @return ResultInterface Always returns Ok.
  */
 public function validate($value)
 {
     return Ok::unit();
 }
示例#6
0
 /**
  * If the schema has a property named ':any_name:', this method will validate all keys,
  * that have not been explicitly addressed by the schema.
  *
  * @param mixed[] $data
  *
  * @return ResultInterface
  */
 protected function validateAnyValues(array $data)
 {
     $errors = [];
     foreach (array_diff_key($data, $this->properties) as $key => $value) {
         if (isset($this->properties[':any_name:'])) {
             if ($value === null) {
                 if ($this->properties[':any_name:']->isRequired()) {
                     $errors[$key] = [Error::MISSING_VALUE];
                 }
                 continue;
             }
             $result = $this->properties[':any_name:']->validate($value);
             if ($result instanceof Error) {
                 $errors[$key] = $result->unwrap();
             }
         } else {
             $errors[$key] = [Error::UNEXPECTED_KEY];
         }
     }
     return empty($errors) ? Ok::unit() : Error::unit($errors);
 }
示例#7
0
 /**
  * Tells if a given value is a valid float.
  *
  * @param mixed $value
  *
  * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned.
  */
 public function validate($value)
 {
     return is_float($value) ? Ok::unit() : Error::unit([Error::NON_FLOAT]);
 }
示例#8
0
 /**
  * Tells if a given value is a valid choice according to the property's definition.
  *
  * @param mixed $value
  *
  * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned.
  */
 public function validate($value)
 {
     return in_array($value, $this->choices) ? Ok::unit() : Error::unit([Error::INVALID_CHOICE]);
 }
示例#9
0
 /**
  * Tells if a given value is a fully qualified class name of an existing php class.
  *
  * @param mixed $value
  *
  * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned.
  */
 public function validate($value)
 {
     return class_exists($value) || interface_exists($value) ? Ok::unit() : Error::unit([Error::CLASS_NOT_EXISTS]);
 }
示例#10
0
 /**
  * Tells if a given value is a valid scalar.
  *
  * @param mixed $value
  *
  * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned.
  */
 public function validate($value)
 {
     return is_scalar($value) ? Ok::unit() : Error::unit([Error::NON_SCALAR]);
 }
示例#11
0
 /**
  * Tells if a given value is a valid string.
  *
  * @param mixed $value
  *
  * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned.
  */
 public function validate($value)
 {
     return is_string($value) ? Ok::unit() : Error::unit([Error::NON_STRING]);
 }
示例#12
0
 /**
  * Tells if a given value is a valid bool.
  *
  * @param mixed $value
  *
  * @return ResultInterface Returns Ok if the value is valid, otherwise an Error is returned.
  */
 public function validate($value)
 {
     return is_bool($value) ? Ok::unit() : Error::unit([Error::NON_BOOL]);
 }