Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
64.29% |
9 / 14 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Datatype | |
64.29% |
9 / 14 |
|
28.57% |
2 / 7 |
14.56 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| selfCheck | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| validate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| fix | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getValue | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| setValue | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| 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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoQtiItem\model\qti\datatype; |
| 24 | |
| 25 | use oat\taoQtiItem\model\qti\exception\QtiModelException; |
| 26 | |
| 27 | /** |
| 28 | * It is the top class of every basic data type used in QTI |
| 29 | * |
| 30 | * @access public |
| 31 | * @author Sam, <sam@taotesting.com> |
| 32 | * @package taoQTI |
| 33 | |
| 34 | */ |
| 35 | abstract class Datatype |
| 36 | { |
| 37 | protected $value = null; |
| 38 | |
| 39 | public function __construct($value = null) |
| 40 | { |
| 41 | $this->setValue($value); |
| 42 | } |
| 43 | |
| 44 | public function __toString() |
| 45 | { |
| 46 | return (string) $this->getValue(); |
| 47 | } |
| 48 | |
| 49 | public function selfCheck() |
| 50 | { |
| 51 | return static::validate($this->value); |
| 52 | } |
| 53 | |
| 54 | public static function validate($value) |
| 55 | { |
| 56 | throw new QtiModelException('the method "validate" must be implemented by ' . get_called_class()); |
| 57 | } |
| 58 | |
| 59 | public static function fix($value) |
| 60 | { |
| 61 | throw new QtiModelException('the method "fix" must be implemented by ' . get_called_class()); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Return the base type representation of the value |
| 66 | * |
| 67 | * @return mixed |
| 68 | */ |
| 69 | public function getValue() |
| 70 | { |
| 71 | |
| 72 | $returnValue = null; |
| 73 | |
| 74 | if (!is_null($this->value)) { |
| 75 | $returnValue = $this->value; |
| 76 | } |
| 77 | |
| 78 | return $returnValue; |
| 79 | } |
| 80 | |
| 81 | public function setValue($value) |
| 82 | { |
| 83 | if (static::validate($value)) { |
| 84 | $this->value = $value; |
| 85 | } elseif (!is_null(static::fix($value))) { |
| 86 | $this->value = static::fix($value); |
| 87 | } else { |
| 88 | throw new DatatypeException('cannot set invalid value to datatype ' . get_class($this) . ':' . $value); |
| 89 | } |
| 90 | } |
| 91 | } |