Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
77.78% |
14 / 18 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| AbstractContext | |
77.78% |
14 / 18 |
|
80.00% |
4 / 5 |
8.70 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| hasParameter | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getParameter | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setParameter | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getSupportedParameters | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| validateParameter | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| checkParameterSupport | |
100.00% |
7 / 7 |
|
100.00% |
1 / 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) 2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\Context; |
| 24 | |
| 25 | use InvalidArgumentException; |
| 26 | |
| 27 | /** |
| 28 | * @deprecated Use \oat\generis\model\Context\AbstractContext instead |
| 29 | */ |
| 30 | abstract class AbstractContext implements ContextInterface |
| 31 | { |
| 32 | protected $parameters = []; |
| 33 | |
| 34 | public function __construct(array $parameters) |
| 35 | { |
| 36 | foreach ($parameters as $parameter => $parameterValue) { |
| 37 | $this->setParameter($parameter, $parameterValue); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | public function hasParameter(string $parameter): bool |
| 42 | { |
| 43 | try { |
| 44 | $this->checkParameterSupport($parameter); |
| 45 | } catch (InvalidArgumentException $exception) { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | return isset($this->parameters[$parameter]); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @deprecated Use \oat\generis\model\Context\AbstractContext::getParameter() instead |
| 54 | */ |
| 55 | public function getParameter(string $parameter, $default = null) |
| 56 | { |
| 57 | $this->checkParameterSupport($parameter); |
| 58 | |
| 59 | return $this->parameters[$parameter] ?? $default; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @deprecated Use \oat\generis\model\Context\AbstractContext::setParameter() instead |
| 64 | */ |
| 65 | public function setParameter(string $parameter, $parameterValue): void |
| 66 | { |
| 67 | $this->checkParameterSupport($parameter); |
| 68 | $this->validateParameter($parameter, $parameterValue); |
| 69 | |
| 70 | $this->parameters[$parameter] = $parameterValue; |
| 71 | } |
| 72 | |
| 73 | abstract protected function getSupportedParameters(): array; |
| 74 | |
| 75 | /** |
| 76 | * @param mixed $parameterValue |
| 77 | */ |
| 78 | abstract protected function validateParameter(string $parameter, $parameterValue): void; |
| 79 | |
| 80 | private function checkParameterSupport(string $parameter): void |
| 81 | { |
| 82 | if (!in_array($parameter, $this->getSupportedParameters(), true)) { |
| 83 | throw new InvalidArgumentException( |
| 84 | sprintf( |
| 85 | 'Context parameter %s is not supported.', |
| 86 | $parameter |
| 87 | ) |
| 88 | ); |
| 89 | } |
| 90 | } |
| 91 | } |