Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
56.52% |
26 / 46 |
|
41.67% |
5 / 12 |
CRAP | |
0.00% |
0 / 1 |
| Attribute | |
56.52% |
26 / 46 |
|
41.67% |
5 / 12 |
86.92 | |
0.00% |
0 / 1 |
| __construct | |
57.89% |
11 / 19 |
|
0.00% |
0 / 1 |
12.78 | |||
| __toString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| isRequired | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isNull | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setNull | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| validateValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| validateCardinality | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDefault | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getValue | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| setValue | |
50.00% |
7 / 14 |
|
0.00% |
0 / 1 |
8.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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoQtiItem\model\qti\attribute; |
| 24 | |
| 25 | use oat\taoQtiItem\model\qti\datatype\DatatypeException; |
| 26 | |
| 27 | /** |
| 28 | * It is the top class of every attributes used in QTI |
| 29 | * |
| 30 | * @access public |
| 31 | * @author Sam, <sam@taotesting.com> |
| 32 | * @package taoQTI |
| 33 | |
| 34 | */ |
| 35 | abstract class Attribute |
| 36 | { |
| 37 | // phpcs:disable Generic.NamingConventions.UpperCaseConstantName |
| 38 | public const QTI_v2p0 = '2.0'; |
| 39 | public const QTI_v2p1 = '2.1'; |
| 40 | // phpcs:enable Generic.NamingConventions.UpperCaseConstantName |
| 41 | |
| 42 | /** |
| 43 | * The name of the attribute defined in the QTI standard |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | protected static $name = ''; |
| 48 | |
| 49 | /** |
| 50 | * The class of datatype (a subclass of oat\taoQtiItem\model\qti\datatype\Datatype) |
| 51 | * |
| 52 | * @var string |
| 53 | */ |
| 54 | protected static $type = ''; |
| 55 | |
| 56 | /** |
| 57 | * Define if this attribute is required or not |
| 58 | * |
| 59 | * @var boolean |
| 60 | */ |
| 61 | protected static $required = false; |
| 62 | |
| 63 | /** |
| 64 | * Define the default value of the attribute |
| 65 | * |
| 66 | * @var mixed |
| 67 | */ |
| 68 | protected static $defaultValue = null; |
| 69 | |
| 70 | /** |
| 71 | * Define the default value of the attribute |
| 72 | * |
| 73 | * @var mixed |
| 74 | */ |
| 75 | protected static $taoDefaultValue = null; |
| 76 | |
| 77 | /** |
| 78 | * The object holding the value of the attribute |
| 79 | * |
| 80 | * @var \oat\taoQtiItem\model\qti\datatype\Datatype |
| 81 | */ |
| 82 | protected $value = null; |
| 83 | protected $version = self::QTI_v2p1; |
| 84 | |
| 85 | /** |
| 86 | * Instantiate the attribute object |
| 87 | * |
| 88 | * @param mixed $value |
| 89 | * @throws \oat\taoQtiItem\model\qti\attribute\AttributeException |
| 90 | */ |
| 91 | public function __construct($value = null, $version = self::QTI_v2p1) |
| 92 | { |
| 93 | |
| 94 | $this->version = $version; |
| 95 | |
| 96 | if (empty(static::$name) || empty(static::$type)) { |
| 97 | throw new AttributeException( |
| 98 | 'Fail to extend QTI_attribute_Attribute class properly: wrong QTI Attribute property definition: "' |
| 99 | . __CLASS__ . '"' |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | if ( |
| 104 | class_exists(static::$type) |
| 105 | && is_subclass_of(static::$type, 'oat\\taoQtiItem\\model\\qti\\datatype\\Datatype') |
| 106 | ) { |
| 107 | if (!is_null($value)) { |
| 108 | $this->value = new static::$type($value); |
| 109 | } elseif (!is_null(static::$defaultValue)) { |
| 110 | $this->value = new static::$type(static::$defaultValue); |
| 111 | } elseif (!is_null(static::$taoDefaultValue)) { |
| 112 | $this->value = new static::$type(static::$taoDefaultValue); |
| 113 | } else { |
| 114 | $this->setNull(); |
| 115 | } |
| 116 | } else { |
| 117 | throw new AttributeException( |
| 118 | 'Fail to extend QTI_attribute_Attribute class properly: the attribute type class does not exist: "' |
| 119 | . static::$type . '"' |
| 120 | ); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public function __toString() |
| 125 | { |
| 126 | return $this->isNull() ? '' : (string) $this->value->getValue(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Check if this attribute is required |
| 131 | * |
| 132 | * @return boolean |
| 133 | */ |
| 134 | public function isRequired() |
| 135 | { |
| 136 | return (bool) static::$required; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Check if a value has been set to this attribute |
| 141 | * |
| 142 | * @return boolean |
| 143 | */ |
| 144 | public function isNull() |
| 145 | { |
| 146 | return is_null($this->value); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Clear, empty, nullify the value of the attribute |
| 151 | * |
| 152 | * @return null |
| 153 | */ |
| 154 | public function setNull() |
| 155 | { |
| 156 | return $this->value = null; |
| 157 | } |
| 158 | /** |
| 159 | * Check if the attribute is valid in terms of value |
| 160 | * |
| 161 | * @param mixed $value |
| 162 | * @return boolean |
| 163 | */ |
| 164 | public function validateValue($value) |
| 165 | { |
| 166 | return call_user_func([static::$type, 'validate'], $value); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Check if the cardinality of the attribute value is correct |
| 171 | * |
| 172 | * @return boolean |
| 173 | */ |
| 174 | public function validateCardinality() |
| 175 | { |
| 176 | return $this->isRequired() ? !$this->isNull() : true; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Get the attribute name |
| 181 | * |
| 182 | * @return string |
| 183 | */ |
| 184 | public function getName() |
| 185 | { |
| 186 | return static::$name; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Get the Qti BaseType class |
| 191 | * |
| 192 | * @return string |
| 193 | */ |
| 194 | public function getType() |
| 195 | { |
| 196 | return static::$type; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Get the default value of the attribute defined in standard QTI 2.1 |
| 201 | * |
| 202 | * @return mixed |
| 203 | */ |
| 204 | public function getDefault() |
| 205 | { |
| 206 | return static::$defaultValue; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Return the value of the attribute in base type int, string, null |
| 211 | * |
| 212 | * @return mixed |
| 213 | */ |
| 214 | public function getValue($returnObject = false) |
| 215 | { |
| 216 | |
| 217 | $returnValue = null; |
| 218 | |
| 219 | if (!is_null($this->value)) { |
| 220 | $returnValue = ($returnObject) ? $this->value : $this->value->getValue(); //return mixed |
| 221 | } |
| 222 | |
| 223 | return $returnValue; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Set the attribute value |
| 228 | * |
| 229 | * @param mixed $value |
| 230 | * @return boolean |
| 231 | * @throws \oat\taoQtiItem\model\qti\attribute\AttributeException |
| 232 | */ |
| 233 | public function setValue($value) |
| 234 | { |
| 235 | |
| 236 | $returnValue = false; |
| 237 | |
| 238 | if (!is_null($value)) { |
| 239 | try { |
| 240 | $value = new static::$type($value); |
| 241 | if (!is_null($value)) { |
| 242 | $this->value = $value; |
| 243 | $returnValue = true; |
| 244 | } |
| 245 | } catch (DatatypeException $de) { |
| 246 | $type = '(' . gettype($value) . ')'; |
| 247 | if ($type == 'object') { |
| 248 | $type .= '(' . get_class($value) . ')'; |
| 249 | } |
| 250 | throw new AttributeException( |
| 251 | 'Cannot assign the value to attribute: ' . static::$name . ' -> ' . $type . ' ' . $value |
| 252 | ); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return $returnValue; |
| 257 | } |
| 258 | } |