BaseValidator.php
1 year 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
1 year ago
UrlLike.php
2 years ago
WhitelistedValue.php
1 year ago
BaseValidator.php
45 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Validators; |
| 10 | |
| 11 | abstract class BaseValidator |
| 12 | { |
| 13 | /** |
| 14 | * The method to validate a value. If the value has not an expected format, an instance of |
| 15 | * {@link Piwik\Validators\Exception} should be thrown. |
| 16 | * |
| 17 | * @param $value |
| 18 | * @throws Exception |
| 19 | */ |
| 20 | public abstract function validate($value); |
| 21 | protected function isValueBare($value) |
| 22 | { |
| 23 | // we allow this value. if it is supposed to be not empty, please use NotEmpty validator on top |
| 24 | return $value === \false || $value === null || $value === ''; |
| 25 | } |
| 26 | /** |
| 27 | * Lets you easily check a value against multiple validators. |
| 28 | * |
| 29 | * @param string $name The name/description of the field you want to validate the value for. |
| 30 | * The name will be prefixed in case there is any error. |
| 31 | * @param mixed $value The value which needs to be tested |
| 32 | * @param BaseValidator[] $validators |
| 33 | */ |
| 34 | public static function check($name, $value, $validators) |
| 35 | { |
| 36 | foreach ($validators as $validator) { |
| 37 | try { |
| 38 | $validator->validate($value); |
| 39 | } catch (\Exception $e) { |
| 40 | throw new \Piwik\Validators\Exception(strip_tags($name) . ': ' . $e->getMessage(), $e->getCode()); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 |