Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
66.67% |
12 / 18 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| KeyReader | |
66.67% |
12 / 18 |
|
50.00% |
2 / 4 |
12.00 | |
0.00% |
0 / 1 |
| __construct | |
42.86% |
3 / 7 |
|
0.00% |
0 / 1 |
4.68 | |||
| getValue | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| hasValue | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| __toPhpCode | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 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 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\metadata\reader; |
| 23 | |
| 24 | use oat\tao\model\metadata\exception\InconsistencyConfigException; |
| 25 | use oat\tao\model\metadata\exception\reader\MetadataReaderNotFoundException; |
| 26 | |
| 27 | /** |
| 28 | * Class KeyReader, read a $key of given $data |
| 29 | * |
| 30 | * @author Camille Moyon |
| 31 | * @package oat\tao\model\metadata\reader |
| 32 | */ |
| 33 | class KeyReader implements Reader |
| 34 | { |
| 35 | /** |
| 36 | * Source key label to find into $dataSource array |
| 37 | */ |
| 38 | public const KEY_SOURCE = 'key'; |
| 39 | |
| 40 | protected $key; |
| 41 | |
| 42 | /** |
| 43 | * KeyReader constructor. |
| 44 | * |
| 45 | * @param string $key Index to find |
| 46 | * @throws \Exception |
| 47 | */ |
| 48 | public function __construct($options) |
| 49 | { |
| 50 | if (! is_array($options)) { |
| 51 | throw new InconsistencyConfigException('Reader options has to be an array.'); |
| 52 | } |
| 53 | |
| 54 | if (! array_key_exists(self::KEY_SOURCE, $options)) { |
| 55 | throw new InconsistencyConfigException( |
| 56 | 'Missing configuration keys for reader, attribute "' . self::KEY_SOURCE . '" not found' |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | $this->key = $options[self::KEY_SOURCE]; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get value of $data using $key |
| 65 | * |
| 66 | * @param array $data A CSV line |
| 67 | * @return string |
| 68 | * @throws MetadataReaderNotFoundException |
| 69 | */ |
| 70 | public function getValue(array $data) |
| 71 | { |
| 72 | $key = strtolower($this->key); |
| 73 | if ($this->hasValue($data, $key)) { |
| 74 | return $data[$key]; |
| 75 | } |
| 76 | |
| 77 | throw new MetadataReaderNotFoundException( |
| 78 | __CLASS__ . ' cannot found value associated to key "' . $this->key . '".' |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Check if $key of $data array exists |
| 84 | * |
| 85 | * @param array $data |
| 86 | * @param $key |
| 87 | * @return bool |
| 88 | */ |
| 89 | protected function hasValue(array $data, $key) |
| 90 | { |
| 91 | if (! is_string($key)) { |
| 92 | return false; |
| 93 | } |
| 94 | return isset($data[$key]); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Configuration serialization |
| 99 | * |
| 100 | * @return array |
| 101 | */ |
| 102 | public function __toPhpCode() |
| 103 | { |
| 104 | $options = ['key' => $this->key]; |
| 105 | return empty($options) ? '' : \common_Utils::toHumanReadablePhpString($options); |
| 106 | } |
| 107 | } |