| Total Complexity | 47 |
| Total Lines | 167 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ConceptPropertyValue 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 ConceptPropertyValue, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class ConceptPropertyValue extends VocabularyDataObject |
||
| 7 | { |
||
| 8 | /** submembers */ |
||
| 9 | private $submembers; |
||
| 10 | /** property type */ |
||
| 11 | private $type; |
||
| 12 | /** content language */ |
||
| 13 | private $clang; |
||
| 14 | /** whether the property value is external w.r.t. to the subject resource */ |
||
| 15 | private $external; |
||
| 16 | |||
| 17 | public function __construct($model, $vocab, $resource, $prop, $clang = '') |
||
| 18 | { |
||
| 19 | parent::__construct($model, $vocab, $resource); |
||
| 20 | $this->submembers = array(); |
||
| 21 | $this->type = $prop; |
||
| 22 | $this->clang = $clang; |
||
| 23 | // check if the resource is external to the current vocabulary |
||
| 24 | $this->external = ($this->getLabel('', 'null') === null); |
||
| 25 | if ($this->external) { |
||
| 26 | // if we find the resource in another vocabulary, use it instead |
||
| 27 | $exvocab = $this->getExVocab(); |
||
| 28 | if ($exvocab !== null) { |
||
| 29 | $this->vocab = $exvocab; |
||
| 30 | } |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | public function __toString() |
||
| 35 | { |
||
| 36 | return is_string($this->getLabel()) ? $this->getLabel() : $this->getLabel()->getValue(); |
||
|
|
|||
| 37 | } |
||
| 38 | |||
| 39 | public function getLang() |
||
| 42 | } |
||
| 43 | |||
| 44 | public function getLabel($lang = '', $fallbackToUri = 'uri') |
||
| 45 | { |
||
| 46 | if ($this->clang) { |
||
| 47 | $lang = $this->clang; |
||
| 48 | } |
||
| 49 | if ($this->vocab->getConfig()->getLanguageOrder($lang)) { |
||
| 50 | foreach ($this->vocab->getConfig()->getLanguageOrder($lang) as $fallback) { |
||
| 51 | if ($this->resource->label($fallback) !== null) { |
||
| 52 | return $this->resource->label($fallback); |
||
| 53 | } |
||
| 54 | // We need to check all the labels in case one of them matches a subtag of the current language |
||
| 55 | if ($this->resource->allLiterals('skos:prefLabel')) { |
||
| 56 | foreach($this->resource->allLiterals('skos:prefLabel') as $label) { |
||
| 57 | // the label lang code is a subtag of the UI lang eg. en-GB - create a new literal with the main language |
||
| 58 | if ($label !== null && strpos($label->getLang(), $fallback . '-') === 0) { |
||
| 59 | return EasyRdf\Literal::create($label, $fallback); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | if ($this->resource->label($lang) !== null) { // current language |
||
| 67 | return $this->resource->label($lang); |
||
| 68 | } elseif ($this->resource->label($this->vocab->getConfig()->getDefaultLanguage()) !== null) { // vocab default language |
||
| 69 | return $this->resource->label($this->vocab->getConfig()->getDefaultLanguage()); |
||
| 70 | } elseif ($this->resource->label() !== null) { // any language |
||
| 71 | return $this->resource->label(); |
||
| 72 | } elseif ($this->resource->getLiteral('rdf:value', $lang) !== null) { // current language |
||
| 73 | return $this->resource->getLiteral('rdf:value', $lang); |
||
| 74 | } elseif ($this->resource->getLiteral('rdf:value') !== null) { // any language |
||
| 75 | return $this->resource->getLiteral('rdf:value'); |
||
| 76 | } |
||
| 77 | |||
| 78 | // see if we can find a label in another vocabulary known by the skosmos instance |
||
| 79 | $label = $this->getExternalLabel($this->vocab, $this->getUri(), $lang); |
||
| 80 | if ($label) { |
||
| 81 | return $label; |
||
| 82 | } |
||
| 83 | |||
| 84 | if ($fallbackToUri == 'uri') { |
||
| 85 | // return uri if no label is found |
||
| 86 | return $this->resource->shorten() ? $this->resource->shorten() : $this->getUri(); |
||
| 87 | } |
||
| 88 | return null; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function getType() |
||
| 92 | { |
||
| 93 | return $this->type; |
||
| 94 | } |
||
| 95 | |||
| 96 | public function getUri() |
||
| 99 | } |
||
| 100 | |||
| 101 | public function getExVocab() |
||
| 102 | { |
||
| 103 | return $this->model->guessVocabularyFromURI($this->getUri(), $this->vocab->getId()); |
||
| 104 | } |
||
| 105 | |||
| 106 | public function getVocab() |
||
| 107 | { |
||
| 108 | return $this->vocab; |
||
| 109 | } |
||
| 110 | |||
| 111 | public function getVocabName() |
||
| 112 | { |
||
| 113 | return $this->vocab->getShortName(); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function addSubMember($member, $lang = '') |
||
| 117 | { |
||
| 118 | $label = $member->getLabel($lang) ? $member->getLabel($lang) : $member->getLabel(); |
||
| 119 | $this->submembers[$label->getValue()] = $member; |
||
| 120 | $this->sortSubMembers(); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function getSubMembers() |
||
| 124 | { |
||
| 125 | if (empty($this->submembers)) { |
||
| 126 | return null; |
||
| 127 | } |
||
| 128 | |||
| 129 | return $this->submembers; |
||
| 130 | } |
||
| 131 | |||
| 132 | private function sortSubMembers() |
||
| 136 | } |
||
| 137 | |||
| 138 | } |
||
| 139 | |||
| 140 | public function isExternal() { |
||
| 141 | return $this->external; |
||
| 142 | } |
||
| 143 | |||
| 144 | public function getNotation() |
||
| 148 | } |
||
| 149 | |||
| 150 | } |
||
| 151 | |||
| 152 | public function isReified() { |
||
| 154 | } |
||
| 155 | |||
| 156 | public function getReifiedPropertyValues() { |
||
| 157 | $ret = array(); |
||
| 158 | $props = $this->resource->propertyUris(); |
||
| 159 | foreach($props as $prop) { |
||
| 160 | $prop = (EasyRdf\RdfNamespace::shorten($prop) !== null) ? EasyRdf\RdfNamespace::shorten($prop) : $prop; |
||
| 161 | $propkey = str_starts_with($prop, 'dc11:') ? |
||
| 162 | str_replace('dc11:', 'dc:', $prop) : $prop; |
||
| 176 |