Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
27 / 30 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| StatisticalJsonResourceMetadataCompiler | |
90.00% |
27 / 30 |
|
85.71% |
6 / 7 |
18.32 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| withAliases | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| compile | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| compileResource | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getResourceType | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| clearNotAllowedAliases | |
66.67% |
6 / 9 |
|
0.00% |
0 / 1 |
7.33 | |||
| clearAttributes | |
100.00% |
9 / 9 |
|
100.00% |
1 / 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) 2022 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\StatisticalMetadata\DataStore\Compiler; |
| 24 | |
| 25 | use core_kernel_classes_Class; |
| 26 | use core_kernel_classes_Resource; |
| 27 | use oat\tao\model\metadata\compiler\ResourceMetadataCompilerInterface; |
| 28 | use oat\tao\model\TaoOntology; |
| 29 | |
| 30 | class StatisticalJsonResourceMetadataCompiler implements ResourceMetadataCompilerInterface |
| 31 | { |
| 32 | private const ATTRIBUTES_WHITE_LIST = [ |
| 33 | 'type', |
| 34 | 'alias', |
| 35 | 'value', |
| 36 | '@type', |
| 37 | '@id', |
| 38 | '@context', |
| 39 | ]; |
| 40 | |
| 41 | /** @var ResourceMetadataCompilerInterface */ |
| 42 | private $resourceMetadataCompiler; |
| 43 | |
| 44 | /** @var array */ |
| 45 | private $aliases = []; |
| 46 | |
| 47 | public function __construct(ResourceMetadataCompilerInterface $resourceMetadataCompiler) |
| 48 | { |
| 49 | $this->resourceMetadataCompiler = $resourceMetadataCompiler; |
| 50 | } |
| 51 | |
| 52 | public function withAliases(array $aliases): self |
| 53 | { |
| 54 | $this->aliases = $aliases; |
| 55 | |
| 56 | return $this; |
| 57 | } |
| 58 | |
| 59 | public function compile(core_kernel_classes_Resource $resource): array |
| 60 | { |
| 61 | $compiled = $this->compileResource($resource); |
| 62 | $compiled = $this->clearAttributes($compiled); |
| 63 | |
| 64 | return $this->clearNotAllowedAliases($compiled); |
| 65 | } |
| 66 | |
| 67 | private function compileResource(core_kernel_classes_Resource $resource): array |
| 68 | { |
| 69 | $compiled = json_decode(json_encode($this->resourceMetadataCompiler->compile($resource)), true); |
| 70 | $compiled['@type'] = $this->getResourceType($resource); |
| 71 | |
| 72 | return $compiled; |
| 73 | } |
| 74 | |
| 75 | private function getResourceType(core_kernel_classes_Resource $resource): string |
| 76 | { |
| 77 | return $resource->isInstanceOf(new core_kernel_classes_Class(TaoOntology::CLASS_URI_ITEM)) |
| 78 | ? TaoOntology::CLASS_URI_ITEM |
| 79 | : TaoOntology::CLASS_URI_TEST; |
| 80 | } |
| 81 | |
| 82 | private function clearNotAllowedAliases(array $compiled): array |
| 83 | { |
| 84 | foreach ($compiled as $key => $value) { |
| 85 | if (strpos($key, '@') === 0) { |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | if (!isset($value['alias'])) { |
| 90 | unset($compiled[$key]); |
| 91 | |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | if (!empty($this->aliases) && !in_array($value['alias'], $this->aliases, true)) { |
| 96 | unset($compiled[$key]); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return $compiled; |
| 101 | } |
| 102 | |
| 103 | private function clearAttributes(array $compiled): array |
| 104 | { |
| 105 | $allowedKeys = self::ATTRIBUTES_WHITE_LIST; |
| 106 | |
| 107 | foreach ($compiled as $key => $value) { |
| 108 | if (isset($value['alias']) && in_array($value['alias'], $this->aliases, true)) { |
| 109 | $allowedKeys[] = $key; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | foreach ($compiled as $key => $value) { |
| 114 | if (!in_array($key, $allowedKeys, true)) { |
| 115 | unset($compiled[$key]); |
| 116 | unset($compiled['@context'][$key]); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return $compiled; |
| 121 | } |
| 122 | } |