Total Complexity | 42 |
Total Lines | 185 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Mapper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Mapper, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types = 1); |
||
24 | class Mapper |
||
25 | { |
||
26 | /** |
||
27 | * The mapping rules. |
||
28 | */ |
||
29 | protected $rules; |
||
30 | |||
31 | /** |
||
32 | * Default language for literals without language tag. |
||
33 | */ |
||
34 | protected $defaultLanguage; |
||
35 | |||
36 | /** |
||
37 | * Allowed JSKOS class names |
||
38 | */ |
||
39 | public static $JSKOSClasses = [ |
||
40 | 'Concept', |
||
41 | 'ConceptScheme', |
||
42 | 'ConceptType', |
||
43 | 'ConceptBundle', |
||
44 | 'Concordance', |
||
45 | 'Mapping' |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * Create a new mapping based on rules. |
||
50 | */ |
||
51 | public function __construct(array $rules = null) |
||
52 | { |
||
53 | if ($rules === null) { |
||
54 | $rules = json_decode(file_get_contents(__DIR__."/rdf2jskos.json"), true); |
||
55 | } |
||
56 | |||
57 | if (isset($rules['_ns'])) { |
||
58 | foreach ($rules['_ns'] as $prefix => $namespace) { |
||
59 | # TODO: warn if prefix is already defined with different namespace! |
||
60 | \EasyRdf_Namespace::set($prefix, $namespace); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | if (isset($rules['_defaultLanguage'])) { |
||
65 | $this->defaultLanguage = $rules['_defaultLanguage']; |
||
66 | } else { |
||
67 | $this->defaultLanguage = 'und'; |
||
68 | } |
||
69 | |||
70 | foreach ($rules as $field => $config) { |
||
71 | if (substr($field, 0, 1) != '_') { |
||
72 | $this->rules[$field] = $config; |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Apply mapping via extraction of data from an RDF resource and add |
||
79 | * resulting data to a JSKOS Resource. |
||
80 | * |
||
81 | * @param EasyRdf_Resource $rdf |
||
|
|||
82 | * @param PrettyJsonSerializable $jskos |
||
83 | */ |
||
84 | public function applyAtResource(\EasyRdf_Resource $rdf, Resource $jskos) |
||
85 | { |
||
86 | #error_log($rdf->getGraph()->dump('text')); |
||
87 | #error_log(print_r($this->rules,1)); |
||
88 | |||
89 | foreach ($this->rules as $property => $mapping) { |
||
90 | $type = $mapping['type']; |
||
91 | if (isset($mapping['jskos']) && in_array($mapping['jskos'], static::$JSKOSClasses)) { |
||
92 | $class = '\JSKOS\\'.$mapping['jskos']; |
||
93 | } else { |
||
94 | $class = null; |
||
95 | } |
||
96 | |||
97 | foreach ($mapping['properties'] as $rdfProperty) { |
||
98 | if ($type == 'URI') { |
||
99 | foreach (static::getURIs($rdf, $rdfProperty) as $uri) { |
||
100 | if (isset($class)) { |
||
101 | $uri = new $class(['uri'=>$uri]); |
||
102 | } |
||
103 | if (isset($mapping['unique'])) { |
||
104 | $jskos->$property = $uri; |
||
105 | } else { |
||
106 | if (empty($jskos->$property)) { |
||
107 | $jskos->$property = []; |
||
108 | } |
||
109 | $jskos->$property[] = $uri; |
||
110 | } |
||
111 | } |
||
112 | } elseif ($type == 'literal') { |
||
113 | foreach ($rdf->allLiterals($rdfProperty) as $literal) { |
||
114 | $value = static::cleanString($literal); |
||
115 | if (!isset($value)) { |
||
116 | continue; |
||
117 | } |
||
118 | |||
119 | if ($literal->getLang()) { |
||
120 | $language = $literal->getLang(); |
||
121 | } elseif (isset($mapping['_defaultLanguage'])) { |
||
122 | $language = $mapping['_defaultLanguage']; |
||
123 | } else { |
||
124 | $language = $this->defaultLanguage; |
||
125 | } |
||
126 | |||
127 | $languageMap = isset($jskos->$property) ? $jskos->$property : []; |
||
128 | |||
129 | if (isset($mapping['unique'])) { |
||
130 | $languageMap[$language] = $value; |
||
131 | } else { |
||
132 | $list = $languageMap[$language] ?? []; |
||
133 | $list[] = $value; |
||
134 | $languageMap[$language] = $list; |
||
135 | } |
||
136 | |||
137 | $jskos->$property = $languageMap; |
||
138 | } |
||
139 | } elseif ($type == 'plain') { |
||
140 | foreach ($rdf->allLiterals($rdfProperty) as $literal) { |
||
141 | $value = static::cleanString($literal); |
||
142 | if (!isset($value)) { |
||
143 | continue; |
||
144 | } |
||
145 | |||
146 | if (isset($mapping['pattern']) && !preg_match($mapping['pattern'], $value)) { |
||
147 | continue; |
||
148 | } |
||
149 | if (isset($mapping['unique'])) { |
||
150 | $jskos->$property = $value; |
||
151 | } else { |
||
152 | if (empty($jskos->$property)) { |
||
153 | $jskos->$property = [$value]; |
||
154 | } else { |
||
155 | $jskos->$property[] = $value; |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Clean up a string value by trimming whitespace and mapping empty strings to null. |
||
166 | */ |
||
167 | public static function cleanString($string) |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Silently try to load RDF from an URL. |
||
175 | * @return EasyRdf_Resource|null |
||
176 | */ |
||
177 | public static function loadRDF($url, $uri = null, $format = null, $forceHttp = false) |
||
191 | } |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Get a list of RDF object URIs. |
||
196 | */ |
||
197 | public static function getURIs(\EasyRDF_Resource $subject, $predicate, $pattern = null) |
||
209 | } |
||
210 | } |
||
211 |