Overview

Namespaces

  • None
  • Wei
    • Validator

Classes

  • Wei\Validator\All
  • Wei\Validator\AllOf
  • Wei\Validator\Alnum
  • Wei\Validator\Alpha
  • Wei\Validator\BaseValidator
  • Wei\Validator\Between
  • Wei\Validator\Blank
  • Wei\Validator\Callback
  • Wei\Validator\CharLength
  • Wei\Validator\Chinese
  • Wei\Validator\Color
  • Wei\Validator\Contains
  • Wei\Validator\CreditCard
  • Wei\Validator\Date
  • Wei\Validator\DateTime
  • Wei\Validator\Decimal
  • Wei\Validator\Digit
  • Wei\Validator\Dir
  • Wei\Validator\DivisibleBy
  • Wei\Validator\DoubleByte
  • Wei\Validator\Email
  • Wei\Validator\EndsWith
  • Wei\Validator\EqualTo
  • Wei\Validator\Exists
  • Wei\Validator\FieldExists
  • Wei\Validator\File
  • Wei\Validator\GreaterThan
  • Wei\Validator\GreaterThanOrEqual
  • Wei\Validator\IdCardCn
  • Wei\Validator\IdCardHk
  • Wei\Validator\IdCardMo
  • Wei\Validator\IdCardTw
  • Wei\Validator\IdenticalTo
  • Wei\Validator\Image
  • Wei\Validator\In
  • Wei\Validator\Ip
  • Wei\Validator\Length
  • Wei\Validator\LessThan
  • Wei\Validator\LessThanOrEqual
  • Wei\Validator\Lowercase
  • Wei\Validator\Luhn
  • Wei\Validator\MaxLength
  • Wei\Validator\MinLength
  • Wei\Validator\MobileCn
  • Wei\Validator\NaturalNumber
  • Wei\Validator\NoneOf
  • Wei\Validator\Null
  • Wei\Validator\Number
  • Wei\Validator\OneOf
  • Wei\Validator\Password
  • Wei\Validator\Phone
  • Wei\Validator\PhoneCn
  • Wei\Validator\PlateNumberCn
  • Wei\Validator\PositiveInteger
  • Wei\Validator\PostcodeCn
  • Wei\Validator\Present
  • Wei\Validator\QQ
  • Wei\Validator\RecordExists
  • Wei\Validator\Regex
  • Wei\Validator\Required
  • Wei\Validator\SomeOf
  • Wei\Validator\StartsWith
  • Wei\Validator\Time
  • Wei\Validator\Tld
  • Wei\Validator\Type
  • Wei\Validator\Uppercase
  • Wei\Validator\Url
  • Wei\Validator\Uuid
  • Overview
  • Namespace
  • Function
  1: <?php
  2: /**
  3:  * Wei Framework
  4:  *
  5:  * @copyright   Copyright (c) 2008-2015 Twin Huang
  6:  * @license     http://opensource.org/licenses/mit-license.php MIT License
  7:  */
  8: 
  9: namespace Wei\Validator;
 10: 
 11: /**
 12:  * Check if the length (or size) of input is equals specified length or in
 13:  * specified length range
 14:  *
 15:  * @author      Twin Huang <twinhuang@qq.com>
 16:  */
 17: class Length extends BaseValidator
 18: {
 19:     protected $notDetectedMessage = '%name%\'s length could not be detected';
 20: 
 21:     protected $lengthMessage = '%name% must have a length of %length%';
 22: 
 23:     protected $lengthItemMessage = '%name% must contain %length% item(s)';
 24: 
 25:     protected $notInMessage = '%name% must have a length between %min% and %max%';
 26: 
 27:     protected $notInItemMessage = '%name% must contain %min% to %max% item(s)';
 28: 
 29:     protected $min;
 30: 
 31:     protected $max;
 32: 
 33:     /**
 34:      * The required exactly length of input
 35:      *
 36:      * @var int
 37:      */
 38:     protected $length;
 39: 
 40:     /**
 41:      * Whether count the string length by characters or bytes
 42:      *
 43:      * @var bool
 44:      */
 45:     protected $countByChars = false;
 46: 
 47:     /**
 48:      * The character encoding
 49:      *
 50:      * @var string
 51:      */
 52:     protected $charset = 'UTF-8';
 53: 
 54:     /**
 55:      * {@inheritdoc}
 56:      */
 57:     public function __invoke($input, $min = null, $max = null)
 58:     {
 59:         // ($input, $min, $max)
 60:         if (is_numeric($min) && is_numeric($max)) {
 61:             $this->storeOption('min', $min);
 62:             $this->storeOption('max', $max);
 63:         // ($input, $length)
 64:         } elseif (is_numeric($min) && is_null($max)) {
 65:             $this->storeOption('length', $min);
 66:         }
 67: 
 68:         return $this->isValid($input);
 69:     }
 70: 
 71:     /**
 72:      * {@inheritdoc}
 73:      */
 74:     protected function doValidate($input)
 75:     {
 76:         if (false === ($len = $this->getLength($input))) {
 77:             $this->addError('notDetected');
 78:             return false;
 79:         }
 80: 
 81:         if (!is_null($this->length)) {
 82:             if ($this->length != $len) {
 83:                 $this->addError(is_scalar($input) ? 'length' : 'lengthItem');
 84:                 return false;
 85:             }
 86:         } elseif ($this->min > $len || $this->max < $len) {
 87:             $this->addError(is_scalar($input) ? 'notIn' : 'notInItem');
 88:             return false;
 89:         }
 90: 
 91:         return true;
 92:     }
 93: 
 94:     /**
 95:      * Return the input's length or false when could not detected
 96:      *
 97:      * @param string|array|\Countable $input
 98:      * @return int|false
 99:      */
100:     public function getLength($input)
101:     {
102:         if (is_scalar($input)) {
103:             if ($this->countByChars) {
104:                 return mb_strlen($input, $this->charset);
105:             } else {
106:                 return strlen($input);
107:             }
108:         } elseif (is_array($input) || $input instanceof \Countable) {
109:             return count($input);
110:         } else {
111:             return false;
112:         }
113:     }
114: }
115: 
Wei Framework API documentation generated by ApiGen