Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_form_validators_Url | |
0.00% |
0 / 13 |
|
0.00% |
0 / 3 |
90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| evaluate | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
56 | |||
| getDefaultMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * 2020 (original work) (update and modification) Open Assessment Technologies SA |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * Short description of class tao_helpers_form_validators_Url |
| 27 | * |
| 28 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 29 | */ |
| 30 | class tao_helpers_form_validators_Url extends tao_helpers_form_Validator |
| 31 | { |
| 32 | private const OPTION_ALLOW_EMPTY = 'allow_empty'; |
| 33 | |
| 34 | /** |
| 35 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 36 | */ |
| 37 | public function __construct(array $options = []) |
| 38 | { |
| 39 | parent::__construct($options); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param string $value |
| 44 | * @return bool |
| 45 | */ |
| 46 | public function evaluate($value) |
| 47 | { |
| 48 | if ( |
| 49 | '' === $value |
| 50 | && $this->hasOption(self::OPTION_ALLOW_EMPTY) |
| 51 | && $this->getOption(self::OPTION_ALLOW_EMPTY) == true |
| 52 | ) { |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | //backward compatible behavior: |
| 57 | //scheme should be prepended if not found (pattern includes spelling errors) |
| 58 | if (preg_match('/^[a-zA-Z]{1,10}[:\/]{1,3}/', $value) === false) { |
| 59 | $value = 'http://' . $value; |
| 60 | } |
| 61 | |
| 62 | $returnValue = !(filter_var($value, FILTER_VALIDATE_URL) === false); |
| 63 | |
| 64 | //'isset' is backward compatible behavior |
| 65 | if (!$this->hasOption('allow_parameters')) { |
| 66 | $returnValue = $returnValue && (strpos($value, '?') === false); |
| 67 | } |
| 68 | |
| 69 | return $returnValue; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Default error message |
| 74 | * |
| 75 | * @return string |
| 76 | */ |
| 77 | protected function getDefaultMessage() |
| 78 | { |
| 79 | return __('Provided URL is not valid'); |
| 80 | } |
| 81 | } |