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