BaseValidator.php
6 years ago
CharacterLength.php
6 years ago
DateTime.php
6 years ago
Email.php
6 years ago
Exception.php
6 years ago
IdSite.php
6 years ago
IpRanges.php
6 years ago
NotEmpty.php
6 years ago
NumberRange.php
6 years ago
Regex.php
6 years ago
UrlLike.php
6 years ago
WhitelistedValue.php
6 years ago
BaseValidator.php
49 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - 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 | abstract class BaseValidator |
| 13 | { |
| 14 | |
| 15 | /** |
| 16 | * The method to validate a value. If the value has not an expected format, an instance of |
| 17 | * {@link Piwik\Validators\Exception} should be thrown. |
| 18 | * |
| 19 | * @param $value |
| 20 | * @throws Exception |
| 21 | */ |
| 22 | abstract public function validate($value); |
| 23 | |
| 24 | protected function isValueBare($value) |
| 25 | { |
| 26 | // we allow this value. if it is supposed to be not empty, please use NotEmpty validator on top |
| 27 | return $value === false || $value === null || $value === ''; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Lets you easily check a value against multiple validators. |
| 32 | * |
| 33 | * @param string $name The name/description of the field you want to validate the value for. |
| 34 | * The name will be prefixed in case there is any error. |
| 35 | * @param mixed $value The value which needs to be tested |
| 36 | * @param BaseValidator[] $validators |
| 37 | */ |
| 38 | public static function check($name, $value, $validators) |
| 39 | { |
| 40 | foreach ($validators as $validator) { |
| 41 | try { |
| 42 | $validator->validate($value); |
| 43 | } catch (\Exception $e) { |
| 44 | throw new Exception(strip_tags($name) . ': ' . $e->getMessage(), $e->getCode()); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 |