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