Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| ResourceCompiledMetadataHelper | |
100.00% |
11 / 11 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
| getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getLabel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| unserialize | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| 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) 2018 (original work) Open Assessment Technologies SA ; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\helpers\metadata; |
| 22 | |
| 23 | /** |
| 24 | * Class ResourceCompiledMetadataHelper |
| 25 | * @package oat\tao\helpers\metadata |
| 26 | */ |
| 27 | class ResourceCompiledMetadataHelper |
| 28 | { |
| 29 | /** |
| 30 | * Compiled resource metadata. |
| 31 | * |
| 32 | * @var array |
| 33 | */ |
| 34 | private $metaData = []; |
| 35 | |
| 36 | /** |
| 37 | * Gets a particular value from compiled metadata of a Resource. |
| 38 | * |
| 39 | * In case of no value can be found with given $language, |
| 40 | * the implementation will try to retrieve a value for the default installation |
| 41 | * language. Otherwise, the method returns NULL. |
| 42 | * |
| 43 | * @param string $language |
| 44 | * @param string $name |
| 45 | * @return mixed |
| 46 | */ |
| 47 | public function getValue($name) |
| 48 | { |
| 49 | return isset($this->metaData[$name]) ? $this->metaData[$name] : null; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Return resource label |
| 54 | * |
| 55 | * @return string|null |
| 56 | */ |
| 57 | public function getLabel() |
| 58 | { |
| 59 | return $this->getValue('label'); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Unpacks resource metadata from a string. |
| 64 | * |
| 65 | * @param string $data |
| 66 | * @return array |
| 67 | * @throws \common_exception_InconsistentData |
| 68 | */ |
| 69 | public function unserialize($data) |
| 70 | { |
| 71 | if (!is_string($data)) { |
| 72 | throw new \common_exception_InconsistentData( |
| 73 | 'The encoded resource metadata should be provided as a string' |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | $metaData = json_decode($data, true); |
| 78 | |
| 79 | if (!is_array($metaData)) { |
| 80 | throw new \common_exception_InconsistentData('The decoded resource metadata should be an array'); |
| 81 | } |
| 82 | $this->metaData = $metaData; |
| 83 | |
| 84 | return $this->metaData; |
| 85 | } |
| 86 | } |