|
1 <?php |
|
2 namespace CorpusParole\Models; |
|
3 |
|
4 use Config; |
|
5 use CorpusParole\Libraries\Utils; |
|
6 use CorpusParole\Libraries\CocoonUtils; |
|
7 use CorpusParole\Libraries\RdfModel\RdfModelResource; |
|
8 use JsonSerializable; |
|
9 use Log; |
|
10 use EasyRdf\Literal; |
|
11 use EasyRdf\Resource; |
|
12 use EasyRdf\Graph; |
|
13 |
|
14 |
|
15 /** |
|
16 */ |
|
17 class DocumentResult extends RdfModelResource implements JsonSerializable { |
|
18 |
|
19 public function __construct($uri, $graph = null) { |
|
20 //print($graph->dump('html')); |
|
21 parent::__construct($uri, $graph); |
|
22 } |
|
23 |
|
24 private $id = null; |
|
25 |
|
26 // memoization |
|
27 private $providedCHO = null; |
|
28 private $title = false; |
|
29 private $lang = null; |
|
30 private $langResolved = null; |
|
31 private $issued = null; |
|
32 private $modified = null; |
|
33 |
|
34 public function getProvidedCHO() { |
|
35 if(is_null($this->providedCHO)) { |
|
36 $this->providedCHO = $this->get("<http://www.europeana.eu/schemas/edm/aggregatedCHO>"); |
|
37 } |
|
38 return $this->providedCHO; |
|
39 } |
|
40 |
|
41 protected function clearMemoizationCache() { |
|
42 $this->providedCHO = null; |
|
43 $this->title = false; |
|
44 $this->lang = null; |
|
45 $this->langResolved = null; |
|
46 $this->issued = null; |
|
47 $this->modified = null; |
|
48 } |
|
49 |
|
50 public function getId() { |
|
51 if(is_null($this->id)) { |
|
52 $ids = $this->getProvidedCHO()->all('<http://purl.org/dc/elements/1.1/identifier>'); |
|
53 foreach ($ids as $id) { |
|
54 if($id instanceof Literal && strpos($id->getValue(), config('corpusparole.corpus_id_scheme')) === 0) { |
|
55 $this->id = $id->getValue(); |
|
56 } |
|
57 } |
|
58 if(is_null($this->id)) { |
|
59 $this->id = CocoonUtils::getIdFromCorpusUri($this->uri); |
|
60 } |
|
61 } |
|
62 return $this->id; |
|
63 } |
|
64 |
|
65 public function getLanguage() { |
|
66 if(is_null($this->lang)) { |
|
67 try { |
|
68 $langs = $this->getProvidedCHO()->all('<http://purl.org/dc/elements/1.1/language>'); |
|
69 if(count($langs) > 0) { |
|
70 $this->lang = $langs[0]; |
|
71 } |
|
72 } catch(\Exception $e) { |
|
73 $this->lang = null; |
|
74 } |
|
75 } |
|
76 return $this->lang; |
|
77 } |
|
78 |
|
79 public function getLanguageValue() { |
|
80 $lang = $this->getLanguage(); |
|
81 if($lang instanceof Resource) { |
|
82 return $lang->getUri(); |
|
83 } else if($lan instanceof Literal) { |
|
84 return $lang->getValue(); |
|
85 } |
|
86 return null; |
|
87 } |
|
88 |
|
89 public function getLanguageResolved() { |
|
90 return $this->langResolved; |
|
91 } |
|
92 public function setLanguageResolved($languageResolved) { |
|
93 $this->langResolved = $languageResolved; |
|
94 } |
|
95 |
|
96 |
|
97 public function getTitle() { |
|
98 if($this->title === false) { |
|
99 try { |
|
100 $this->title = $this->getProvidedCHO()->getLiteral('<http://purl.org/dc/elements/1.1/title>'); |
|
101 } catch(\Exception $e) { |
|
102 $this->title = null; |
|
103 } |
|
104 } |
|
105 return $this->title; |
|
106 } |
|
107 |
|
108 public function setTitle($value, $lang="fr") { |
|
109 $oldTitle = $this->getTitle(); |
|
110 if($oldTitle && $oldTitle->getValue() != $value && $oldTitle->getLang() != $lang) { |
|
111 $literalTitle = new Literal($value, $lang, null); |
|
112 $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/elements/1.1/title', $oldTitle, $literalTitle); |
|
113 //clear cache |
|
114 $this->title = false; |
|
115 } |
|
116 } |
|
117 |
|
118 |
|
119 |
|
120 public function getTitleValue() { |
|
121 $title = $this->getTitle(); |
|
122 return is_null($title)?null:$title->getValue(); |
|
123 } |
|
124 |
|
125 public function getIssued() { |
|
126 if(is_null($this->issued)) { |
|
127 try { |
|
128 $this->issued = $this->getProvidedCHO()->getLiteral("<http://purl.org/dc/terms/issued>"); |
|
129 } catch(\Exception $e) { |
|
130 $this->issued = null; |
|
131 } |
|
132 } |
|
133 return $this->issued; |
|
134 } |
|
135 |
|
136 public function getIssuedValue() { |
|
137 $issued = $this->getIssued(); |
|
138 return is_null($issued)?null:$issued->getValue(); |
|
139 } |
|
140 |
|
141 public function getModified() { |
|
142 if(is_null($this->modified)) { |
|
143 try { |
|
144 $this->modified = $this->getProvidedCHO()->getLiteral("<http://purl.org/dc/terms/modified>"); |
|
145 if(is_null($this->modified)) { |
|
146 $this->modified = $this->getIssued(); |
|
147 } |
|
148 } catch(\Exception $e) { |
|
149 $this->modified = null; |
|
150 } |
|
151 } |
|
152 return $this->modified; |
|
153 } |
|
154 |
|
155 public function setModified($value = null) { |
|
156 if(is_null($value)) { |
|
157 $value = gmdate(\DateTime::ATOM); |
|
158 } elseif ($value instanceof \DateTime) { |
|
159 $value = $value->format(\DateTime::ATOM); |
|
160 } |
|
161 $value = preg_replace('/[\+\-]00(\:?)00$/', 'Z', $value); |
|
162 |
|
163 $modified = $this->getModified(); |
|
164 if($value && (!$modified || $modified->getValue() !== $value) ) { |
|
165 |
|
166 $newModified = new Literal($value, null, "http://purl.org/dc/terms/W3CDTF"); |
|
167 $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/terms/modified', $modified, $newModified); |
|
168 |
|
169 $this->modified = null; |
|
170 } |
|
171 } |
|
172 |
|
173 |
|
174 public function getModifiedValue() { |
|
175 $modified = $this->getModified(); |
|
176 return is_null($modified)?null:$modified->getValue(); |
|
177 } |
|
178 |
|
179 |
|
180 public function jsonSerialize() { |
|
181 if(!$this->graph) { |
|
182 return [ |
|
183 'id' => $this->getId(), |
|
184 ]; |
|
185 } else { |
|
186 $res = [ |
|
187 'id' => $this->getId(), |
|
188 'uri' => $this->getUri(), |
|
189 'title' => $this->getTitleValue(), |
|
190 'language' => $this->getLanguageValue(), |
|
191 'modified' => $this->getModifiedValue(), |
|
192 'issued' => $this->getIssuedValue() |
|
193 ]; |
|
194 |
|
195 if($this->language_resolved) { |
|
196 $res['language_resolved'] = $this->getLanguageResolved(); |
|
197 } |
|
198 |
|
199 return $res; |
|
200 } |
|
201 } |
|
202 } |