| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 | <?php namespace System\Validation\Rules;use System\Str;use System\Validation\Rule;class Size_Of extends Rule {	/**	 * The exact size the attribute must be.	 *	 * @var int	 */	public $length;	/**	 * The maximum size of the attribute.	 *	 * @var int	 */	public $maximum;	/**	 * The minimum size of the attribute.	 *	 * @var int	 */	public $minimum;	/**	 * Evaluate the validity of an attribute.	 *	 * @param  string  $attribute	 * @param  array   $attributes	 * @return void	 */	public function check($attribute, $attributes)	{		if ( ! array_key_exists($attribute, $attributes))		{			return true;		}		if (is_numeric($attributes[$attribute]))		{			return $this->check_number($attribute, $attributes);		}		else		{			return $this->check_string($attribute, $attributes);		}	}	/**	 * Evaluate the validity of a numeric attribute.	 *	 * @param  string  $attribute	 * @param  array   $attributes	 * @return void	 */	private function check_number($attribute, $attributes)	{		if ( ! is_null($this->length) and $attributes[$attribute] !== $this->length)		{			return false;		}		if ( ! is_null($this->maximum) and $attributes[$attribute] > $this->maximum)		{			return false;		}		if ( ! is_null($this->minimum and $attributes[$attribute] < $this->minimum))		{			return false;		}		return true;	}	/**	 * Evaluate the validity of a string attribute.	 *	 * @param  string  $attribute	 * @param  array   $attributes	 * @return void	 */	public function check_string($attribute, $attributes)	{		$value = trim((string) $attributes[$attribute]);		if ( ! is_null($this->length) and Str::length($value) !== $this->length)		{			return false;		}		if ( ! is_null($this->maximum) and Str::length($value) > $this->maximum)		{			return false;		}		if ( ! is_null($this->minimum) and Str::length($value) < $this->minimum)		{			return false;		}		return true;	}	/**	 * Set the exact size the attribute must be.	 *	 * @param  int  $length	 * @return Size_Of	 */	public function is($length)	{		$this->length = $length;		return $this;	}	/**	 * Set the minimum and maximum size of the attribute.	 *	 * @param  int  $minimum	 * @param  int  $maximum	 * @return Size_Of	 */	public function between($minimum, $maximum)	{		$this->minimum = $minimum;		$this->maximum = $maximum;		return $this;	}	/**	 * Set the minimum size the attribute.	 *	 * @param  int  $minimum	 * @return Size_Of	 */	public function at_least($minimum)	{		$this->minimum = $minimum;		return $this;	}	/**	 * Set the maximum size the attribute.	 *	 * @param  int  $maximum	 * @return Size_Of	 */	public function less_than($maximum)	{		$this->maximum = $maximum;		return $this;	}}
 |