Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ListElementsFinderContext | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getSupportedParameters | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| validateParameter | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| validateRequiredParameters | |
0.00% |
0 / 8 |
|
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) 2021 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoBackOffice\model\ListElement\Context; |
| 24 | |
| 25 | use InvalidArgumentException; |
| 26 | use core_kernel_classes_Class; |
| 27 | use oat\tao\model\Context\AbstractContext; |
| 28 | |
| 29 | class ListElementsFinderContext extends AbstractContext |
| 30 | { |
| 31 | public const PARAMETER_LIST_CLASS = 'listClass'; |
| 32 | public const PARAMETER_LIMIT = 'limit'; |
| 33 | public const PARAMETER_OFFSET = 'offset'; |
| 34 | |
| 35 | private const REQUIRED_PARAMETERS = [ |
| 36 | self::PARAMETER_LIST_CLASS, |
| 37 | ]; |
| 38 | |
| 39 | public function __construct(array $parameters) |
| 40 | { |
| 41 | $this->validateRequiredParameters($parameters); |
| 42 | |
| 43 | parent::__construct($parameters); |
| 44 | } |
| 45 | |
| 46 | protected function getSupportedParameters(): array |
| 47 | { |
| 48 | return [ |
| 49 | self::PARAMETER_LIST_CLASS, |
| 50 | self::PARAMETER_LIMIT, |
| 51 | self::PARAMETER_OFFSET, |
| 52 | ]; |
| 53 | } |
| 54 | |
| 55 | protected function validateParameter(string $parameter, $parameterValue): void |
| 56 | { |
| 57 | if ($parameter === self::PARAMETER_LIST_CLASS && $parameterValue instanceof core_kernel_classes_Class) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | if ( |
| 62 | in_array($parameter, [self::PARAMETER_LIMIT, self::PARAMETER_OFFSET], true) |
| 63 | && is_int($parameterValue) |
| 64 | ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | throw new InvalidArgumentException( |
| 69 | sprintf( |
| 70 | 'Context parameter %s is not valid.', |
| 71 | $parameter |
| 72 | ) |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | private function validateRequiredParameters(array $parameters): void |
| 77 | { |
| 78 | $missedRequiredParameters = array_diff_key(array_flip(self::REQUIRED_PARAMETERS), $parameters); |
| 79 | |
| 80 | if (!empty($missedRequiredParameters)) { |
| 81 | throw new InvalidArgumentException( |
| 82 | sprintf( |
| 83 | 'Some of the required parameters are missing: %s.', |
| 84 | implode(', ', $missedRequiredParameters) |
| 85 | ) |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 | } |