Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_form_validators_Callback | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
240 | |
0.00% |
0 / 1 |
| setOptions | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
42 | |||
| evaluate | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
90 | |||
| 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 | * |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * Short description of class tao_helpers_form_validators_Callback |
| 27 | * |
| 28 | * @access public |
| 29 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 30 | * @package tao |
| 31 | */ |
| 32 | class tao_helpers_form_validators_Callback extends tao_helpers_form_Validator |
| 33 | { |
| 34 | public function setOptions(array $options) |
| 35 | { |
| 36 | parent::setOptions($options); |
| 37 | |
| 38 | if ( |
| 39 | !$this->hasOption('function') |
| 40 | && !(($this->hasOption('class') || $this->hasOption('object')) |
| 41 | && $this->hasOption('method')) |
| 42 | ) { |
| 43 | throw new Exception("Please define a callback function or method"); |
| 44 | } |
| 45 | |
| 46 | if ($this->hasOption('message')) { |
| 47 | $this->setMessage($this->getOption('message')); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Short description of method evaluate |
| 53 | * |
| 54 | * @access public |
| 55 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 56 | * @param string $values |
| 57 | * @return bool |
| 58 | * @throws common_Exception |
| 59 | * @internal param $values |
| 60 | */ |
| 61 | public function evaluate($values) |
| 62 | { |
| 63 | $returnValue = (bool) false; |
| 64 | |
| 65 | if ($this->hasOption('function')) { |
| 66 | $function = $this->getOption('function'); |
| 67 | if (function_exists($function)) { |
| 68 | $callback = [$function]; |
| 69 | } else { |
| 70 | throw new common_Exception("callback function does not exist"); |
| 71 | } |
| 72 | } elseif ($this->hasOption('class')) { |
| 73 | $class = $this->getOption('class'); |
| 74 | $method = $this->getOption('method'); |
| 75 | if (class_exists($class) && method_exists($class, $method)) { |
| 76 | $callback = [$class, $method]; |
| 77 | } else { |
| 78 | throw new common_Exception("callback method does not exist"); |
| 79 | } |
| 80 | } elseif ($this->hasOption('object')) { |
| 81 | $object = $this->getOption('object'); |
| 82 | $method = $this->getOption('method'); |
| 83 | if (method_exists($object, $method)) { |
| 84 | $callback = [$object, $method]; |
| 85 | } else { |
| 86 | throw new common_Exception("callback method does not exist"); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if ($this->hasOption('param')) { |
| 91 | $returnValue = (bool)call_user_func($callback, $values, $this->getOption('param')); |
| 92 | } else { |
| 93 | $returnValue = (bool)call_user_func($callback, $values); |
| 94 | } |
| 95 | |
| 96 | return (bool)$returnValue; |
| 97 | } |
| 98 | } |