Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| OntologyMapper | |
0.00% |
0 / 34 |
|
0.00% |
0 / 9 |
420 | |
0.00% |
0 / 1 |
| formatValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| map | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| combine | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| isEmpty | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getProperties | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getReport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| buildMandatoryProperties | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| buildOptionalProperties | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
| addValue | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| 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) 2018 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\import\service; |
| 22 | |
| 23 | use oat\oatbox\service\ConfigurableService; |
| 24 | use common_report_Report as Report; |
| 25 | |
| 26 | class OntologyMapper extends ConfigurableService implements ImportMapperInterface |
| 27 | { |
| 28 | /** @var array */ |
| 29 | protected $propertiesMapped = []; |
| 30 | |
| 31 | /** @var Report */ |
| 32 | protected $report; |
| 33 | |
| 34 | /** |
| 35 | * @param $property |
| 36 | * @param $value |
| 37 | * @return mixed |
| 38 | */ |
| 39 | protected function formatValue($property, $value) |
| 40 | { |
| 41 | return $value; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param array $data |
| 46 | * @return ImportMapperInterface |
| 47 | * @throws MandatoryFieldException |
| 48 | * @throws \Exception |
| 49 | */ |
| 50 | public function map(array $data = []) |
| 51 | { |
| 52 | $this->report = Report::createInfo(); |
| 53 | |
| 54 | $schema = $this->getOption(static::OPTION_SCHEMA); |
| 55 | $this->buildMandatoryProperties($data, $schema); |
| 56 | $this->buildOptionalProperties($data, $schema); |
| 57 | |
| 58 | return $this; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @param array $extraProperties |
| 63 | * @return array|mixed |
| 64 | */ |
| 65 | public function combine(array $extraProperties) |
| 66 | { |
| 67 | $this->propertiesMapped = array_merge($this->propertiesMapped, $extraProperties); |
| 68 | |
| 69 | return $this; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return bool |
| 74 | */ |
| 75 | public function isEmpty() |
| 76 | { |
| 77 | return empty($this->propertiesMapped); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @return array |
| 82 | */ |
| 83 | public function getProperties() |
| 84 | { |
| 85 | return $this->propertiesMapped; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @inheritdoc |
| 90 | */ |
| 91 | public function getReport() |
| 92 | { |
| 93 | return $this->report; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param array $data |
| 98 | * @param array $schema |
| 99 | * @throws MandatoryFieldException |
| 100 | * @throws RdsResourceNotFoundException |
| 101 | * @throws \common_exception_Error |
| 102 | */ |
| 103 | protected function buildMandatoryProperties(array $data, array $schema) |
| 104 | { |
| 105 | $mandatoryFields = $schema[static::OPTION_SCHEMA_MANDATORY] ?? []; |
| 106 | |
| 107 | foreach ($mandatoryFields as $key => $propertyKey) { |
| 108 | if (!isset($data[$key])) { |
| 109 | throw new MandatoryFieldException('Mandatory field "' . $key . '" should exists.'); |
| 110 | } |
| 111 | |
| 112 | if (empty($data[$key])) { |
| 113 | throw new MandatoryFieldException('Mandatory field "' . $key . '" should not be empty.'); |
| 114 | } |
| 115 | |
| 116 | $this->addValue($propertyKey, $data[$key]); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @param array $data |
| 122 | * @param array $schema |
| 123 | * @throws RdsResourceNotFoundException |
| 124 | * @throws \common_exception_Error |
| 125 | */ |
| 126 | protected function buildOptionalProperties(array $data, array $schema) |
| 127 | { |
| 128 | $optionalFields = isset($schema[static::OPTION_SCHEMA_OPTIONAL]) ? $schema[static::OPTION_SCHEMA_OPTIONAL] : []; |
| 129 | |
| 130 | foreach ($optionalFields as $key => $propertyKey) { |
| 131 | if (!isset($data[$key]) || $data[$key] === '') { |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | $this->addValue($propertyKey, $data[$key]); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @param string $propertyKey |
| 141 | * @param string $value |
| 142 | * @throws RdsResourceNotFoundException |
| 143 | * @throws \common_exception_Error |
| 144 | */ |
| 145 | protected function addValue($propertyKey, $value) |
| 146 | { |
| 147 | if (is_array($propertyKey) && count($propertyKey) === 1) { |
| 148 | $valueMapper = reset($propertyKey); |
| 149 | if ($valueMapper instanceof ImportValueMapperInterface) { |
| 150 | $propertyKey = key($propertyKey); |
| 151 | $this->propagate($valueMapper); |
| 152 | $this->propertiesMapped[$propertyKey] = $valueMapper->map($value); |
| 153 | $this->report->add($valueMapper->getReport()); |
| 154 | } |
| 155 | } else { |
| 156 | $this->propertiesMapped[$propertyKey] = $this->formatValue($propertyKey, $value); |
| 157 | $successMessage = 'Property mapped with success: ' . $propertyKey . ':'; |
| 158 | $successMessage .= is_array($value) ? implode(',', $value) : $value; |
| 159 | $this->report->add(Report::createSuccess($successMessage)); |
| 160 | } |
| 161 | } |
| 162 | } |