Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ListPropertyWriter | |
0.00% |
0 / 21 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| format | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getListService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 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) 2016 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\metadata\writer\ontologyWriter; |
| 23 | |
| 24 | use oat\generis\model\OntologyRdf; |
| 25 | use oat\generis\model\OntologyRdfs; |
| 26 | use oat\tao\model\metadata\exception\InconsistencyConfigException; |
| 27 | use oat\tao\model\metadata\exception\reader\MetadataReaderNotFoundException; |
| 28 | |
| 29 | /** |
| 30 | * Class PropertyWriter |
| 31 | * Writer to write one value to a property |
| 32 | * |
| 33 | * @author Camille Moyon |
| 34 | * @package oat\tao\model\metadata\writer\ontologyWriter |
| 35 | */ |
| 36 | class ListPropertyWriter extends PropertyWriter |
| 37 | { |
| 38 | protected $list; |
| 39 | |
| 40 | /** |
| 41 | * ListPropertyWriter constructor. |
| 42 | * |
| 43 | * @param array $params |
| 44 | * @throws InconsistencyConfigException |
| 45 | */ |
| 46 | public function __construct(array $params) |
| 47 | { |
| 48 | parent::__construct($params); |
| 49 | |
| 50 | $listClass = $this->getClass( |
| 51 | $this->getClass($this->getOption(self::PROPERTY_KEY)) |
| 52 | ->getOnePropertyValue($this->getProperty(OntologyRdfs::RDFS_RANGE)) |
| 53 | ); |
| 54 | |
| 55 | $list = $this->getListService()->getListElements($listClass); |
| 56 | |
| 57 | if (empty($list)) { |
| 58 | throw new InconsistencyConfigException( |
| 59 | 'List "' . $listClass->getUri() . '" does not contain element or not correctly configured.' |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | /** @var \core_kernel_classes_Resource $element */ |
| 64 | foreach ($list as $element) { |
| 65 | $this->list[$element->getUri()] = [ |
| 66 | $element->getLabel(), |
| 67 | $element->getOnePropertyValue($this->getProperty(OntologyRdf::RDF_VALUE))->literal |
| 68 | ]; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Format an array to expected value to be written |
| 74 | * |
| 75 | * @param array $data |
| 76 | * @return int|string |
| 77 | * @throws MetadataReaderNotFoundException |
| 78 | */ |
| 79 | public function format(array $data) |
| 80 | { |
| 81 | $value = parent::format($data); |
| 82 | foreach ($this->list as $uri => $values) { |
| 83 | if (in_array($value, $values)) { |
| 84 | return $uri; |
| 85 | } |
| 86 | } |
| 87 | return ''; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Return the list associated to self::PROPERTY_KEY property |
| 92 | * |
| 93 | * @return\tao_models_classes_ListService |
| 94 | */ |
| 95 | protected function getListService() |
| 96 | { |
| 97 | return \tao_models_classes_ListService::singleton(); |
| 98 | } |
| 99 | } |