Example #1
0
 /**
  * Construct an instance of a BetweenConstraint.
  *
  * @param int|float $min
  * @param int|float $max
  *
  * @throws LackOfCoffeeException
  * @throws InvalidArgumentException
  */
 public function __construct($min, $max)
 {
     parent::__construct();
     Arguments::define(new NumericConstraint(), new NumericConstraint())->check($min, $max);
     if ($min > $max) {
         throw new LackOfCoffeeException('Min should be smaller than or equal to max.');
     }
     $this->min = $min;
     $this->max = $max;
 }
Example #2
0
 /**
  * Get string representation of this constraint.
  *
  * @return mixed
  */
 public function toString()
 {
     $one = $this->one->toString();
     $other = $this->other->toString();
     if ($this->one instanceof AbstractTypeConstraint) {
         $one = $this->one->isUnion() ? sprintf('(%s)', $one) : $one;
     }
     if ($this->other instanceof AbstractTypeConstraint) {
         $other = $this->other->isUnion() ? sprintf('(%s)', $other) : $other;
     }
     return sprintf('%s|%s', $one, $other);
 }
 /**
  * Construct an instance of a StringLengthConstraint.
  *
  * @param int $min
  * @param int $max
  *
  * @throws InvalidArgumentException
  */
 public function __construct($min = 0, $max = -1)
 {
     parent::__construct();
     Arguments::define(Boa::integer(), Boa::integer())->check($min, $max);
     if ($min < 0) {
         throw new InvalidArgumentException('The minimum length is expected to be >= 0.');
     }
     if ($max != -1 && $max < $min || $max < -1) {
         throw new InvalidArgumentException('
             The maximum length is expected to be: (== -1 || >= minimum) &&' . ' >= -1.');
     }
     $this->min = $min;
     $this->max = $max;
 }
 /**
  * Encode constraints into JSON schema types/constraints.
  *
  * @param Spec $spec
  * @param AbstractConstraint|AbstractConstraint[]|Spec $input
  * @param string $title
  *
  * @throws CoreException
  * @return array
  */
 public function encodeConstraints(Spec $spec, $input, $title)
 {
     if ($input instanceof AbstractConstraint) {
         $schema = ['description' => $input->getDescription()];
         if ($input instanceof PrimitiveTypeConstraint) {
             $schema['type'] = $input->toString();
         }
         return $schema;
     } elseif (is_array($input)) {
         $schema = [];
         $descriptions = [];
         foreach ($input as $constraint) {
             $descriptions[] = $constraint->getDescription();
             if ($constraint instanceof PrimitiveTypeConstraint) {
                 $schema['type'] = $constraint->toString();
             }
         }
         $schema['description'] = implode('. ', $descriptions);
         return $schema;
     } elseif ($input instanceof Spec) {
         return (new static())->encode($input, $title);
     }
     throw new CoreException('Unexpected constraint type.');
 }
 /**
  * Construct an instance of a ExactlyOneRecordConstraint.
  *
  * @param Builder $query
  * @param array $where
  */
 public function __construct(Builder $query, array $where = [])
 {
     parent::__construct();
     $this->query = $query;
     $this->where = $where;
 }
Example #6
0
 /**
  * Construct an instance of a ClosureConstraint.
  *
  * @param Closure $closure
  * @param string|null $description
  */
 public function __construct(Closure $closure, $description = null)
 {
     parent::__construct();
     $this->closure = $closure;
     $this->description = Std::coalesce($description, 'The value is expected to meet the constraint.');
 }
 /**
  * Construct an instance of a AtLeastOneOfConstraint.
  *
  * @param array $fields
  */
 public function __construct(array $fields)
 {
     parent::__construct();
     Arguments::define(Boa::arrOf(Boa::string()))->check($fields);
     $this->fields = $fields;
 }
Example #8
0
 /**
  * Construct an instance of a RegexConstraint.
  *
  * @param string $pattern
  */
 public function __construct($pattern)
 {
     parent::__construct();
     $this->pattern = $pattern;
 }