Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
36 / 36 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
core_kernel_classes_ResourceFormatter | |
100.00% |
36 / 36 |
|
100.00% |
2 / 2 |
13 | |
100.00% |
1 / 1 |
getResourceDescription | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
8 | |||
propertiesValuestoStdClasses | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 |
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) 2015 (original work) Open Assessment Technologies SA; |
19 | * |
20 | * |
21 | */ |
22 | |
23 | class core_kernel_classes_ResourceFormatter |
24 | { |
25 | public function getResourceDescription(core_kernel_classes_Resource $resource, $fromDefinition = true) |
26 | { |
27 | $returnValue = new stdClass(); |
28 | $properties = []; |
29 | if ($fromDefinition) { |
30 | $types = $resource->getTypes(); |
31 | foreach ($types as $type) { |
32 | foreach ($type->getProperties(true) as $property) { |
33 | //$this->$$property->getUri() = array($property->getLabel(),$this->getPropertyValues()); |
34 | $properties[$property->getUri()] = $property; |
35 | } |
36 | } |
37 | //var_dump($properties); |
38 | $properties = array_unique($properties); |
39 | $propertiesValues = $resource->getPropertiesValues($properties); |
40 | if (count($propertiesValues) == 0) { |
41 | throw new common_exception_NoContent(); |
42 | } |
43 | $propertiesValuesStdClasses = $this->propertiesValuestoStdClasses($propertiesValues); |
44 | } else { //get effective triples and map the returned information into the same structure |
45 | $triples = $resource->getRdfTriples(); |
46 | if (count($triples) == 0) { |
47 | throw new common_exception_NoContent(); |
48 | } |
49 | foreach ($triples as $triple) { |
50 | $properties[$triple->predicate][] = common_Utils::isUri($triple->object) |
51 | ? new core_kernel_classes_Resource($triple->object) |
52 | : new core_kernel_classes_Literal($triple->object); |
53 | } |
54 | $propertiesValuesStdClasses = $this->propertiesValuestoStdClasses($properties); |
55 | } |
56 | |
57 | $returnValue->uri = $resource->getUri(); |
58 | $returnValue->properties = $propertiesValuesStdClasses; |
59 | return $returnValue; |
60 | } |
61 | |
62 | /** |
63 | * small helper provide more convenient data structure for propertiesValues for exchange |
64 | * @return array |
65 | */ |
66 | private function propertiesValuestoStdClasses($propertiesValues = null) |
67 | { |
68 | $returnValue = []; |
69 | foreach ($propertiesValues as $uri => $values) { |
70 | $propStdClass = new stdClass(); |
71 | $propStdClass->predicateUri = $uri; |
72 | foreach ($values as $value) { |
73 | $stdValue = new stdClass(); |
74 | $stdValue->valueType = (get_class($value) == "core_kernel_classes_Literal") ? "literal" : "resource"; |
75 | $stdValue->value = (get_class($value) == "core_kernel_classes_Literal") |
76 | ? $value->__toString() |
77 | : $value->getUri(); |
78 | $propStdClass->values[] = $stdValue; |
79 | } |
80 | $returnValue[] = $propStdClass; |
81 | } |
82 | return $returnValue; |
83 | } |
84 | } |