Example #1
0
 /**
  * @expectedException \Shrink0r\PhpSchema\Exception
  * @expectedExceptionMessage Given property type 'moep' has not been registered.
  */
 public function testInvalidPropertyType()
 {
     $mockSchema = $this->getMockBuilder(SchemaInterface::class)->getMock();
     $mockSchema->method('getFactory')->willReturn(new Factory());
     $property = new EnumProperty($mockSchema, 'value', ['required' => true, 'one_of' => ['moep']]);
     $property->validate(23);
 }
Example #2
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);
 }