BaseValidator.php
2 years ago
CharacterLength.php
2 years ago
DateTime.php
2 years ago
Email.php
2 years ago
Exception.php
2 years ago
IdSite.php
2 years ago
IpRanges.php
2 years ago
NotEmpty.php
2 years ago
NumberRange.php
2 years ago
Regex.php
2 years ago
UrlLike.php
2 years ago
WhitelistedValue.php
2 years ago
CharacterLength.php
50 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | * |
| 9 | */ |
| 10 | namespace Piwik\Validators; |
| 11 | |
| 12 | use Piwik\Piwik; |
| 13 | class CharacterLength extends \Piwik\Validators\BaseValidator |
| 14 | { |
| 15 | /** |
| 16 | * @var null|int |
| 17 | */ |
| 18 | private $min; |
| 19 | /** |
| 20 | * @var null|int |
| 21 | */ |
| 22 | private $max; |
| 23 | /** |
| 24 | * @param null|int $min |
| 25 | * @param null|int $max |
| 26 | */ |
| 27 | public function __construct($min = null, $max = null) |
| 28 | { |
| 29 | if (isset($min)) { |
| 30 | $this->min = (int) $min; |
| 31 | } |
| 32 | if (isset($max)) { |
| 33 | $this->max = (int) $max; |
| 34 | } |
| 35 | } |
| 36 | public function validate($value) |
| 37 | { |
| 38 | if (!is_string($value) && !is_numeric($value)) { |
| 39 | return; |
| 40 | } |
| 41 | $lenValue = mb_strlen($value); |
| 42 | if (isset($this->min) && $this->min > $lenValue) { |
| 43 | throw new \Piwik\Validators\Exception(Piwik::translate('General_ValidatorErrorCharacterTooShort', array($lenValue, $this->min))); |
| 44 | } |
| 45 | if (isset($this->max) && $this->max < $lenValue) { |
| 46 | throw new \Piwik\Validators\Exception(Piwik::translate('General_ValidatorErrorCharacterTooLong', array($lenValue, $this->max))); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 |