Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 76 |
|
0.00% |
0 / 22 |
CRAP | |
0.00% |
0 / 1 |
| TimePoint | |
0.00% |
0 / 76 |
|
0.00% |
0 / 22 |
1722 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
| toArray | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| fromArray | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
42 | |||
| jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| serialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| unserialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setTimestamp | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getTimestamp | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getNormalizedTimestamp | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setType | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setTarget | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getTarget | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addTag | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| removeTag | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getTag | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setTags | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| getTags | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRef | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| match | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| compare | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| sort | |
0.00% |
0 / 4 |
|
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) 2016 (original work) Open Assessment Technologies SA ; |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * @author Jean-Sébastien Conan <jean-sebastien.conan@vesperiagroup.com> |
| 23 | */ |
| 24 | |
| 25 | namespace oat\taoTests\models\runner\time; |
| 26 | |
| 27 | /** |
| 28 | * Class TimePoint |
| 29 | * |
| 30 | * Describes a temporal point by storing a timestamp with microseconds and some flags. |
| 31 | * A TimePoint can describe a START or a END temporal point used to define a time range. |
| 32 | * Each TimePoint can be related to a target (CLIENT or SERVER). |
| 33 | * A list of tags can be attached to a TimePoint to explain its role or its context. |
| 34 | * |
| 35 | * @package oat\taoTests\models\runner\time |
| 36 | */ |
| 37 | class TimePoint implements ArraySerializable, \Serializable, \JsonSerializable |
| 38 | { |
| 39 | /** |
| 40 | * Type of TimePoint: start of range |
| 41 | */ |
| 42 | public const TYPE_START = 1; |
| 43 | |
| 44 | /** |
| 45 | * Type of TimePoint: end of range |
| 46 | */ |
| 47 | public const TYPE_END = 2; |
| 48 | |
| 49 | /** |
| 50 | * Represents all types of TimePoint |
| 51 | */ |
| 52 | public const TYPE_ALL = 3; |
| 53 | |
| 54 | /** |
| 55 | * Type of TimePoint target: client side |
| 56 | */ |
| 57 | public const TARGET_CLIENT = 1; |
| 58 | |
| 59 | /** |
| 60 | * Type of TimePoint target: server side |
| 61 | */ |
| 62 | public const TARGET_SERVER = 2; |
| 63 | |
| 64 | /** |
| 65 | * Represents all types of TimePoint targets |
| 66 | */ |
| 67 | public const TARGET_ALL = 3; |
| 68 | |
| 69 | /** |
| 70 | * The decimal precision used to compare timestamps |
| 71 | */ |
| 72 | public const PRECISION = 10000; |
| 73 | |
| 74 | /** |
| 75 | * The timestamp representing the TimePoint |
| 76 | * @var float |
| 77 | */ |
| 78 | protected $timestamp = 0.0; |
| 79 | |
| 80 | /** |
| 81 | * A collection of tags attached to the TimePoint |
| 82 | * @var array |
| 83 | */ |
| 84 | protected $tags = []; |
| 85 | |
| 86 | /** |
| 87 | * The type of TimePoint. Must be a value from TYPE_START or TYPE_END constants. |
| 88 | * @var int |
| 89 | */ |
| 90 | protected $type = 0; |
| 91 | |
| 92 | /** |
| 93 | * The type of target. Must be a value from TARGET_CLIENT or TARGET_SERVER constants. |
| 94 | * @var int |
| 95 | */ |
| 96 | protected $target = 0; |
| 97 | |
| 98 | /** |
| 99 | * The unique reference to name the TimePoint |
| 100 | * @var string |
| 101 | */ |
| 102 | protected $ref; |
| 103 | |
| 104 | /** |
| 105 | * QtiTimePoint constructor. |
| 106 | * @param string|array $tags |
| 107 | * @param float $timestamp |
| 108 | * @param int $type |
| 109 | * @param int $target |
| 110 | */ |
| 111 | public function __construct($tags = null, $timestamp = null, $type = null, $target = null) |
| 112 | { |
| 113 | if (isset($tags)) { |
| 114 | $this->setTags($tags); |
| 115 | } |
| 116 | |
| 117 | if (isset($timestamp)) { |
| 118 | $this->setTimestamp($timestamp); |
| 119 | } |
| 120 | |
| 121 | if (isset($type)) { |
| 122 | $this->setType($type); |
| 123 | } |
| 124 | |
| 125 | if (isset($target)) { |
| 126 | $this->setTarget($target); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Exports the internal state to an array |
| 132 | * @return array |
| 133 | */ |
| 134 | public function toArray() |
| 135 | { |
| 136 | return [ |
| 137 | 'ts' => $this->getTimestamp(), |
| 138 | 'type' => $this->getType(), |
| 139 | 'target' => $this->getTarget(), |
| 140 | 'tags' => $this->getTags(), |
| 141 | ]; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Imports the internal state from an array |
| 146 | * @param array $data |
| 147 | */ |
| 148 | public function fromArray($data) |
| 149 | { |
| 150 | if ($data) { |
| 151 | if (isset($data['tags'])) { |
| 152 | $this->setTags($data['tags']); |
| 153 | } |
| 154 | |
| 155 | if (isset($data['ts'])) { |
| 156 | $this->setTimestamp($data['ts']); |
| 157 | } |
| 158 | |
| 159 | if (isset($data['type'])) { |
| 160 | $this->setType($data['type']); |
| 161 | } |
| 162 | |
| 163 | if (isset($data['target'])) { |
| 164 | $this->setTarget($data['target']); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Specify data which should be serialized to JSON |
| 171 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
| 172 | * @return mixed data which can be serialized by <b>json_encode</b>, |
| 173 | * which is a value of any type other than a resource. |
| 174 | * @since 5.4.0 |
| 175 | */ |
| 176 | public function jsonSerialize() |
| 177 | { |
| 178 | return $this->toArray(); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * String representation of object |
| 183 | * @link http://php.net/manual/en/serializable.serialize.php |
| 184 | * @return string the string representation of the object or null |
| 185 | * @since 5.1.0 |
| 186 | */ |
| 187 | public function serialize() |
| 188 | { |
| 189 | return serialize($this->toArray()); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Constructs the object |
| 194 | * @link http://php.net/manual/en/serializable.unserialize.php |
| 195 | * @param string $serialized <p> |
| 196 | * The string representation of the object. |
| 197 | * </p> |
| 198 | * @return void |
| 199 | * @since 5.1.0 |
| 200 | */ |
| 201 | public function unserialize($serialized) |
| 202 | { |
| 203 | $this->fromArray(unserialize($serialized)); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Sets the timestamp of the TimePoint |
| 208 | * @param float $timestamp |
| 209 | * @return TimePoint |
| 210 | */ |
| 211 | public function setTimestamp($timestamp) |
| 212 | { |
| 213 | $this->timestamp = floatval($timestamp); |
| 214 | return $this; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Gets the timestamp of the TimePoint |
| 219 | * @return float |
| 220 | */ |
| 221 | public function getTimestamp() |
| 222 | { |
| 223 | return $this->timestamp; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Gets the normalized value of the timestamp. This value is the result of: |
| 228 | * `normalized_timestamp = timestamp_with_microseconds * precision` |
| 229 | * @return int |
| 230 | */ |
| 231 | public function getNormalizedTimestamp() |
| 232 | { |
| 233 | return round($this->getTimestamp() * self::PRECISION); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Sets the type of TimePoint |
| 238 | * @param int $type Must be a value from TYPE_START or TYPE_END constants. |
| 239 | * @return TimePoint |
| 240 | */ |
| 241 | public function setType($type) |
| 242 | { |
| 243 | $this->type = intval($type); |
| 244 | return $this; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Gets the type of TimePoint |
| 249 | * @return int |
| 250 | */ |
| 251 | public function getType() |
| 252 | { |
| 253 | return $this->type; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Sets the target type of the TimePoint |
| 258 | * @param int $target Must be a value from TARGET_CLIENT or TARGET_SERVER constants. |
| 259 | * @return TimePoint |
| 260 | */ |
| 261 | public function setTarget($target) |
| 262 | { |
| 263 | $this->target = intval($target); |
| 264 | return $this; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Gets the target type of the TimePoint |
| 269 | * @return int |
| 270 | */ |
| 271 | public function getTarget() |
| 272 | { |
| 273 | return $this->target; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Adds another tag to the TimePoint |
| 278 | * @param string $tag |
| 279 | * @return TimePoint |
| 280 | */ |
| 281 | public function addTag($tag) |
| 282 | { |
| 283 | $this->tags[] = (string)$tag; |
| 284 | $this->ref = null; |
| 285 | return $this; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Removes a tag from the TimePoint |
| 290 | * @param string $tag |
| 291 | * @return TimePoint |
| 292 | */ |
| 293 | public function removeTag($tag) |
| 294 | { |
| 295 | $index = array_search($tag, $this->tags); |
| 296 | |
| 297 | if ($index !== false) { |
| 298 | array_splice($this->tags, $index, 1); |
| 299 | $this->ref = null; |
| 300 | } |
| 301 | |
| 302 | return $this; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Gets a tag from the TimePoint. By default, it will return the first tag. |
| 307 | * @param int $index |
| 308 | * @return string |
| 309 | */ |
| 310 | public function getTag($index = 0) |
| 311 | { |
| 312 | $index = min(max(0, $index), count($this->tags)); |
| 313 | return $this->tags[$index]; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Sets the tags of the TimePoint |
| 318 | * @param string|array $tags |
| 319 | * @return TimePoint |
| 320 | */ |
| 321 | public function setTags($tags) |
| 322 | { |
| 323 | $this->tags = []; |
| 324 | $this->ref = null; |
| 325 | |
| 326 | if (is_array($tags)) { |
| 327 | foreach ($tags as $tag) { |
| 328 | $this->addTag($tag); |
| 329 | } |
| 330 | } else { |
| 331 | $this->addTag($tags); |
| 332 | } |
| 333 | |
| 334 | return $this; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Gets all tags from the TimePoint |
| 339 | * @return array |
| 340 | */ |
| 341 | public function getTags() |
| 342 | { |
| 343 | return $this->tags; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Gets a unique reference to name the TimePoint |
| 348 | * @return string |
| 349 | */ |
| 350 | public function getRef() |
| 351 | { |
| 352 | if (is_null($this->ref)) { |
| 353 | $tags = $this->tags; |
| 354 | sort($tags); |
| 355 | $this->ref = md5(implode('-', $tags)); |
| 356 | } |
| 357 | return $this->ref; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Checks if a TimePoint matches the criteria |
| 362 | * @param array $tags |
| 363 | * @param int $target |
| 364 | * @param int $type |
| 365 | * @return bool |
| 366 | */ |
| 367 | public function match(array $tags = null, $target = self::TARGET_ALL, $type = self::TYPE_ALL) |
| 368 | { |
| 369 | $match = ($this->getType() & $type) && ($this->getTarget() & $target); |
| 370 | |
| 371 | if ($match && isset($tags)) { |
| 372 | $match = (count(array_intersect($tags, $this->getTags())) == count($tags)); |
| 373 | } |
| 374 | |
| 375 | return $match; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Compares the TimePoint with another instance. |
| 380 | * The comparison is made in this order: |
| 381 | * - reference |
| 382 | * - target |
| 383 | * - timestamp |
| 384 | * - type |
| 385 | * |
| 386 | * CAUTION!: The result order is not based on chronological order. |
| 387 | * Its goal is to gather TimePoint by reference and target, then sort by type and timestamp. |
| 388 | * |
| 389 | * @param TimePoint $point |
| 390 | * @return int |
| 391 | */ |
| 392 | public function compare(TimePoint $point) |
| 393 | { |
| 394 | $diff = strcmp($this->getRef(), $point->getRef()); |
| 395 | if ($diff == 0) { |
| 396 | $diff = $this->getTarget() - $point->getTarget(); |
| 397 | if ($diff == 0) { |
| 398 | $diff = $this->getNormalizedTimestamp() - $point->getNormalizedTimestamp(); |
| 399 | if ($diff == 0) { |
| 400 | $diff = $this->getType() - $point->getType(); |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | return $diff; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Sorts a range of TimePoint |
| 409 | * @param array $range |
| 410 | * @return array |
| 411 | */ |
| 412 | public static function sort(array &$range) |
| 413 | { |
| 414 | usort($range, function (TimePoint $a, TimePoint $b) { |
| 415 | return $a->compare($b); |
| 416 | }); |
| 417 | return $range; |
| 418 | } |
| 419 | } |