public function __construct(array $enum = [], $validateType = false, $default = null) { parent::__construct(); $this->enum = $enum; $this->validateType = $validateType; $this->default = $default; }
/** * @param float|null $minimum * @param float|null $maximum * @param boolean|null $exclusiveMinimum * @param boolean|null $exclusiveMaximum * @param float|null $multipleOf * @throws \Exception */ public function __construct($minimum = null, $maximum = null, $exclusiveMinimum = null, $exclusiveMaximum = null, $multipleOf = null) { parent::__construct(); if ($minimum !== null && !is_numeric($minimum)) { throw new \Exception('Minimum must be numeric'); } if ($maximum !== null && !is_numeric($maximum)) { throw new \Exception('Maximum must be numeric'); } if ($maximum !== null && $minimum !== null && $maximum < $minimum) { throw new \Exception('maximum cant be less than minimum'); } if ($exclusiveMinimum !== null && !is_bool($exclusiveMinimum)) { throw new \Exception('exclusiveMinimum must be boolean'); } if ($exclusiveMaximum !== null && !is_bool($exclusiveMaximum)) { throw new \Exception('exclusiveMaximum must be boolean'); } if ($multipleOf !== null && !is_numeric($multipleOf)) { throw new \Exception('multipleOf must be numeric'); } $this->minimum = $minimum; $this->maximum = $maximum; $this->exclusiveMinimum = $exclusiveMinimum; $this->exclusiveMaximum = $exclusiveMaximum; $this->multipleOf = $multipleOf; }
/** * @param array $anyOf * @throws \Exception * @example * ```php * $validator = new AnyOf([ * new IntegerValidator(), * new ArrayValidator( * 1, * 4, * new IntegerValidator() * ) * ]); * * //Will parse successfully both * * $parsed = $validator->parse(8); * $parsed = $validator->parse([8, 10]); * ``` */ public function __construct(array $anyOf) { parent::__construct(); foreach ($anyOf as $validator) { if (!$validator instanceof \Phramework\Validate\BaseValidator) { throw new \Exception(sprintf('Items of %s parameter MUST be instances of Phramework\\Validate\\BaseValidator', $this->anyOfProperty)); } } $this->{$this->anyOfProperty} = $anyOf; }
/** * @param integer $minLength *[Optional]* * Minimum number of its characters, default is 0 * @param integer|null $maxLength *[Optional]* * Maximum number of its characters, default is null * @param string|null $pattern *[Optional]* * Regular expression pattern for validating, default is null * @param boolean $raw *[Optional]* * Keep raw value, don't sanitize value after validation, default is false * @throws \Exception */ public function __construct($minLength = 0, $maxLength = null, $pattern = null, $raw = false) { parent::__construct(); if (!is_int($minLength) || $minLength < 0) { throw new \Exception('minLength must be positive integer'); } if ($maxLength !== null && (!is_int($maxLength) || $maxLength < $minLength)) { throw new \Exception('maxLength must be positive integer'); } $this->minLength = $minLength; $this->maxLength = $maxLength; $this->pattern = $pattern; $this->raw = $raw; }
/** * @param integer $minItems * *[Optional]* Default is 0 * @param integer|null $maxItems * *[Optional]* * @param BaseValidator|null $items * *[Optional]* Default is null * @param Boolean $uniqueItems * *[Optional]* * @throws \Exception */ public function __construct($minItems = 0, $maxItems = null, $items = null, $uniqueItems = false) { parent::__construct(); if (is_array($items)) { throw new \Exception('Array for attribute "items" are not supported yet'); } if (!is_int($minItems) || $minItems < 0) { throw new \Exception('minItems must be positive integer'); } if ($maxItems !== null && !is_int($maxItems) || $maxItems < $minItems) { throw new \Exception('maxItems must be positive integer'); } if ($items !== null && !is_subclass_of($items, BaseValidator::class, true)) { throw new \Exception(sprintf('Property "items" MUST extend "%s"', BaseValidator::class)); } $this->minItems = $minItems; $this->maxItems = $maxItems; $this->items = $items; $this->uniqueItems = $uniqueItems; }
public function __construct($default = null) { parent::__construct(); $this->default = $default; }
/** * This method use this validator to parse data from $value argument * and return a clean object * @param array|object $value Input value to validate * @throws \Phramework\Exceptions\MissingParametersException * @throws \Phramework\Exceptions\IncorrectParametersException * @return object * @todo find out if MissingParameters * @todo add errors * @todo additionalProperties */ public function parse($value) { if (is_array($value)) { $value = (object) $value; } return parent::parse($value); }
/** * Create validator from validation object * @param object $object Validation object * @return BaseValidator * @todo cleanup class loading * @throws \Exception When validator class cannot be found for object's type */ public static function createFromObject($object) { $isFromBase = static::class === self::class; if (!is_object($object)) { throw new \Exception('Expecting an object'); } //Test type if it's set if (property_exists($object, 'type') && !empty($object->type)) { if (array_key_exists($object->type, self::$validatorRegistry)) { if (is_object($object->type) || is_array($object->type) || $object->type === null) { throw new \Exception('Expecting string for type'); } $className = self::$validatorRegistry[$object->type]; $validator = new $className(); /*} elseif (class_exists(__NAMESPACE__ . '\\' . $object->type)) { //if already loaded $className = __NAMESPACE__ . '\\' . $object->type; $validator = new $className(); } elseif (class_exists(__NAMESPACE__ . '\\' . $object->type . 'Validator')) { //if already loaded $className = __NAMESPACE__ . '\\' . $object->type . 'Validator'; $validator = new $className();*/ } else { $className = $object->type . 'Validator'; try { //prevent fatal error new \ReflectionClass($className); //attempt to create class $validator = new $className(); } catch (\Exception $e) { //Wont catch the fatal error throw new \Exception(sprintf('Incorrect type "%s" from "%s"', $object->type, static::class)); } } } elseif (($validator = static::createFromObjectForAdditional($object)) !== null) { return $validator; } elseif (!$isFromBase || isset($object->type) && !empty($object->type) && $object->type == static::$type) { $validator = new static(); } else { throw new \Exception(sprintf('Type is required when creating from "%s"', self::class)); } //For each Validator's attribute foreach (array_merge($validator::getTypeAttributes(), $validator::$commonAttributes) as $attribute) { //Check if provided object contains this attribute if (property_exists($object, $attribute)) { if ($attribute == 'properties') { //get properties as array $properties = $object->{$attribute}; $createdProperties = new \stdClass(); foreach ($properties as $key => $property) { //TODO remove //if (!is_object($property)) { // throw new \Exception(sprintf( // 'Expected object for property value "%s"', // $key // )); //} $createdProperties->{$key} = BaseValidator::createFromObject($property); } //push to class $validator->{$attribute} = $createdProperties; } elseif ($attribute == 'items' || $attribute == 'not') { $validator->{$attribute} = BaseValidator::createFromObject($object->{$attribute}); } else { //Use attributes value in Validator object $validator->{$attribute} = $object->{$attribute}; } } } return $validator; }