Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
14 / 14 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| ConfigSets | |
100.00% |
14 / 14 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
| hashSet | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| hashRemove | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| hashGet | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getOption | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| hasOption | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| setOption | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getHashOption | |
100.00% |
4 / 4 |
|
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) 2014 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\oatbox\config; |
| 23 | |
| 24 | /** |
| 25 | * Configurable base class |
| 26 | * |
| 27 | * inspired by Solarium\Core\Configurable by Bas de Nooijer |
| 28 | * https://github.com/basdenooijer/solarium/blob/master/library/Solarium/Core/Configurable.php |
| 29 | * |
| 30 | * @author Joel Bout <joel@taotesting.com> |
| 31 | */ |
| 32 | trait ConfigSets |
| 33 | { |
| 34 | /** |
| 35 | * Sets a specific field of an option |
| 36 | * |
| 37 | * @param string $key |
| 38 | * @param string $field |
| 39 | * @param mixed $value |
| 40 | * @throws \common_exception_InconsistentData |
| 41 | * @return boolean success |
| 42 | */ |
| 43 | public function hashSet($key, $field, $value) |
| 44 | { |
| 45 | $option = $this->getHashOption($key); |
| 46 | $option[$field] = $value; |
| 47 | return $this->setOption($key, $option); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Removes a specific field of an option |
| 52 | * |
| 53 | * @param string $key |
| 54 | * @param string $field |
| 55 | * @throws \common_exception_InconsistentData |
| 56 | * @return boolean |
| 57 | */ |
| 58 | public function hashRemove($key, $field) |
| 59 | { |
| 60 | $option = $this->getHashOption($key); |
| 61 | if (!isset($option[$field])) { |
| 62 | return false; |
| 63 | } else { |
| 64 | unset($option[$field]); |
| 65 | return $this->setOption($key, $option); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns a specific field of an option |
| 71 | * |
| 72 | * @param string $key |
| 73 | * @throws \common_exception_InconsistentData |
| 74 | * @param string $field |
| 75 | * @return mixed |
| 76 | */ |
| 77 | public function hashGet($key, $field) |
| 78 | { |
| 79 | $option = $this->getHashOption($key); |
| 80 | return isset($option[$field]) ? $option[$field] : null; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param string $name |
| 85 | * @param mixed|null $default |
| 86 | * @return mixed |
| 87 | */ |
| 88 | abstract public function getOption($name, $default = null); |
| 89 | |
| 90 | /** |
| 91 | * @param string $name |
| 92 | * @return boolean |
| 93 | */ |
| 94 | abstract public function hasOption($name); |
| 95 | /** |
| 96 | * @param string $name |
| 97 | * @param mixed $value |
| 98 | */ |
| 99 | abstract public function setOption($name, $value); |
| 100 | |
| 101 | /** |
| 102 | * Retroieve an option and ensure it is an array |
| 103 | * |
| 104 | * @param string $key |
| 105 | * @throws \common_exception_InconsistentData |
| 106 | * @return array |
| 107 | */ |
| 108 | private function getHashOption($key) |
| 109 | { |
| 110 | $option = $this->hasOption($key) ? $this->getOption($key) : []; |
| 111 | if (!is_array($option)) { |
| 112 | throw new \common_exception_InconsistentData($key . ' is not an array'); |
| 113 | } |
| 114 | return $option; |
| 115 | } |
| 116 | } |