Exemple #1
0
 function __construct(...$args)
 {
     parent::__construct(...$args);
     if (isset($this->props)) {
         if (!is_array($this->props)) {
             throw new \InvalidArgumentException();
         }
         foreach ($this->props as $p => &$c) {
             if (!is_string($p)) {
                 throw new \InvalidArgumentException();
             }
             if ($c !== true) {
                 if (!is_array($c) && !$c instanceof \Traversable) {
                     $c = [$c];
                 }
                 foreach ($c as $check) {
                     if (!$check instanceof \Fulfil\CheckInterface) {
                         throw new \InvalidArgumentException("Invalid check type for property {$p}. Expected \\Fulfil\\CheckInterface, found " . Func::getType($check));
                     }
                 }
             }
         }
         unset($c);
     }
 }
Exemple #2
0
 protected function applyValue($input, \Fulfil\Context $ctx)
 {
     if ($input === null) {
         goto done;
     }
     if (!$input instanceof $this->class) {
         $found = Func::getType($input);
         $ctx->addReason($this, ['id' => 'instance.invalidType', 'params' => ['expected' => $this->class, 'found' => $found]]);
     } elseif ($input instanceof \Fulfil\Lang\SelfChecker) {
         $input->check($ctx);
     }
     done:
     return $input;
 }
Exemple #3
0
 protected function applyValue($input, Context $ctx)
 {
     if ($input === null) {
         goto done;
     }
     $isArray = is_array($input);
     $len = null;
     type:
     if (!$isArray && !$input instanceof \Traversable && !$input instanceof \Countable) {
         $ctx->addReason($this, ['id' => 'array.invalidType', 'params' => ['type' => Func::getType($input)]]);
         goto done;
     }
     $len = $this->length !== null || $this->lengthMax !== null || $this->lengthMin !== null || $this->unique !== null ? count($input) : null;
     length:
     if ($this->length) {
         if ($len != $this->length) {
             $ctx->addReason($this, ['id' => 'array.length', 'params' => ['len' => $len, 'expected' => $this->length]]);
         }
     } elseif ($this->lengthMin !== null && $this->lengthMax !== null) {
         if ($len < $this->lengthMin || $len > $this->lengthMax) {
             $ctx->addReason($this, ['id' => 'array.lengthBetween', 'params' => ['len' => $len, 'min' => $this->lengthMin, 'max' => $this->lengthMax]]);
         }
     } elseif ($this->lengthMin !== null) {
         if ($len < $this->lengthMin) {
             $ctx->addReason($this, ['id' => 'array.lengthAtLeast', 'params' => ['len' => $len, 'min' => $this->lengthMin]]);
         }
     } elseif ($this->lengthMax !== null) {
         if ($len > $this->lengthMax) {
             $ctx->addReason($this, ['id' => 'array.lengthAtMost', 'params' => ['len' => $len, 'max' => $this->lengthMax]]);
         }
     }
     unique:
     if ($this->unique !== null && $this->unique) {
         if ($len === null) {
             $len = count($input);
         }
         if (count(array_unique($input, SORT_REGULAR)) != $len) {
             $ctx->addReason($this, ['id' => 'array.unique']);
         }
     }
     done:
     return $input;
 }