Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 19 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_form_validators_Unique | |
0.00% |
0 / 19 |
|
0.00% |
0 / 4 |
110 | |
0.00% |
0 / 1 |
| getDefaultMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setOptions | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getProperty | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
30 | |||
| evaluate | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| 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 | * Validator to ensure a property value is unique |
| 27 | * |
| 28 | * @access public |
| 29 | * @author Joel Bout, <joel@taotesting.com> |
| 30 | * @package tao |
| 31 | */ |
| 32 | class tao_helpers_form_validators_Unique extends tao_helpers_form_Validator |
| 33 | { |
| 34 | private $property; |
| 35 | /** |
| 36 | * (non-PHPdoc) |
| 37 | * @see tao_helpers_form_Validator::getDefaultMessage() |
| 38 | */ |
| 39 | protected function getDefaultMessage() |
| 40 | { |
| 41 | return __('The value for the property "%s" must be unique.', $this->getProperty()->getLabel()); |
| 42 | } |
| 43 | |
| 44 | public function setOptions(array $options) |
| 45 | { |
| 46 | unset($this->property); |
| 47 | |
| 48 | parent::setOptions($options); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * @return core_kernel_classes_Property |
| 54 | * @throws common_exception_Error |
| 55 | */ |
| 56 | protected function getProperty() |
| 57 | { |
| 58 | if (!isset($this->property) || empty($this->property)) { |
| 59 | if (!$this->hasOption('property')) { |
| 60 | throw new common_exception_Error('Property not set'); |
| 61 | } |
| 62 | |
| 63 | $this->property = ($this->getOption('property') instanceof core_kernel_classes_Property) |
| 64 | ? $this->getOption('property') |
| 65 | : new core_kernel_classes_Property($this->getOption('property')); |
| 66 | } |
| 67 | |
| 68 | return $this->property; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * (non-PHPdoc) |
| 73 | * @see tao_helpers_form_Validator::evaluate() |
| 74 | */ |
| 75 | public function evaluate($values) |
| 76 | { |
| 77 | $domain = $this->getProperty()->getDomain(); |
| 78 | foreach ($domain as $class) { |
| 79 | $resources = $class->searchInstances( |
| 80 | [$this->getProperty()->getUri() => $values], |
| 81 | ['recursive' => true, 'like' => false] |
| 82 | ); |
| 83 | if (count($resources) > 0) { |
| 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | } |