Example #1
0
 /**
  * {@inheritdoc}
  */
 public function assert($items)
 {
     if (!isset($items)) {
         $items = [];
     }
     /** @var \Wandu\Validator\Exception\InvalidValueException[] $exceptions */
     $exceptions = [];
     if (!$this->test($items)) {
         throw $this->createException();
     }
     if ($this->validator) {
         foreach ($items as $key => $item) {
             if (!is_int($key)) {
                 $exceptions['.'] = $this->createException();
                 continue;
             }
             try {
                 $this->validator->assert($item);
             } catch (InvalidValueException $e) {
                 $exceptions[$key] = $e;
             }
         }
     } else {
         foreach ($items as $key => $item) {
             if (!is_int($key)) {
                 $exceptions['.'] = $this->createException();
             }
         }
     }
     if (count($exceptions)) {
         throw InvalidValueException::merge($exceptions);
     }
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function assert($item)
 {
     if (!isset($item)) {
         $item = new stdClass();
     }
     /** @var \Wandu\Validator\Exception\InvalidValueException[] $exceptions */
     $exceptions = [];
     if (!$this->test($item)) {
         $exceptions['.'] = $this->createException();
     }
     foreach ($this->properties as $name => $validator) {
         try {
             $value = null;
             if (is_object($item)) {
                 $value = object_get($item, $name);
             }
             $validator->assert($value);
         } catch (InvalidValueException $e) {
             $exceptions[$name] = $e;
         }
     }
     if (count($exceptions)) {
         throw InvalidValueException::merge($exceptions);
     }
 }
 /**
  * @param \Wandu\Validator\Exception\InvalidValueException[] $exceptions
  * @return \Wandu\Validator\Exception\InvalidValueException
  */
 public static function merge(array $exceptions = [])
 {
     $baseException = new InvalidValueException();
     foreach ($exceptions as $name => $exception) {
         if ($name === '.') {
             $baseException->appendTypes($exception->getTypes());
         } else {
             foreach ($exception->getTypes() as $type) {
                 if (strpos($type, '@') === false) {
                     // ex. "exists" => "exists@thisname"
                     $baseException->appendType("{$type}@{$name}");
                 } else {
                     // ex. "exists@foo" => "*****@*****.**"
                     list($type, $key) = explode('@', $type);
                     $baseException->appendType("{$type}@{$name}.{$key}");
                 }
             }
         }
     }
     return $baseException;
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function assert($item)
 {
     if (!isset($item)) {
         return;
     }
     if ($item === '') {
         return;
     }
     /** @var \Wandu\Validator\Exception\InvalidValueException[] $exceptions */
     $exceptions = [];
     if (!$this->test($item)) {
         throw $this->createException();
     }
     foreach ($this->attributes as $name => $validator) {
         try {
             $validator->assert(isset($item[$name]) ? $item[$name] : null);
         } catch (InvalidValueException $e) {
             $exceptions[$name] = $e;
         }
     }
     if (count($exceptions)) {
         throw InvalidValueException::merge($exceptions);
     }
 }