diff -r 6af61adbd570 -r 64caee7ce38d server/src/app/Models/DocumentResult.php --- a/server/src/app/Models/DocumentResult.php Tue Aug 23 23:05:25 2016 +0200 +++ b/server/src/app/Models/DocumentResult.php Sat Aug 06 21:27:53 2016 +0700 @@ -14,189 +14,68 @@ /** */ -class DocumentResult extends RdfModelResource implements JsonSerializable { +class DocumentResult extends DocumentBase { public function __construct($uri, $graph = null) { - //print($graph->dump('html')); parent::__construct($uri, $graph); } - private $id = null; - - // memoization - private $providedCHO = null; - private $title = false; - private $lang = null; - private $langResolved = null; - private $issued = null; - private $modified = null; - - public function getProvidedCHO() { - if(is_null($this->providedCHO)) { - $this->providedCHO = $this->get(""); - } - return $this->providedCHO; - } + private $publishers = false; + private $duration = false; + private $durationMs = -1; protected function clearMemoizationCache() { - $this->providedCHO = null; - $this->title = false; - $this->lang = null; - $this->langResolved = null; - $this->issued = null; - $this->modified = null; - } + parent::clearMemoizationCache(); + $this->publishers = false; + $this->duration = false; + $this->$durationMs = -1; - public function getId() { - if(is_null($this->id)) { - $ids = $this->getProvidedCHO()->all(''); - foreach ($ids as $id) { - if($id instanceof Literal && strpos($id->getValue(), config('corpusparole.corpus_id_scheme')) === 0) { - $this->id = $id->getValue(); - } - } - if(is_null($this->id)) { - $this->id = CocoonUtils::getIdFromCorpusUri($this->uri); - } - } - return $this->id; } - public function getLanguage() { - if(is_null($this->lang)) { + public function getPublishers() { + if($this->publishers === false) { try { - $langs = $this->getProvidedCHO()->all(''); - if(count($langs) > 0) { - $this->lang = $langs[0]; - } + $this->publishers = $this->getProvidedCHO()->getLiteral('dc11:publisher'); } catch(\Exception $e) { - $this->lang = null; + $this->publishers = null; } } - return $this->lang; + return $this->publishers; } - public function getLanguageValue() { - $lang = $this->getLanguage(); - if($lang instanceof Resource) { - return $lang->getUri(); - } else if($lan instanceof Literal) { - return $lang->getValue(); - } - return null; - } - - public function getLanguageResolved() { - return $this->langResolved; - } - public function setLanguageResolved($languageResolved) { - $this->langResolved = $languageResolved; - } - - - public function getTitle() { - if($this->title === false) { - try { - $this->title = $this->getProvidedCHO()->getLiteral(''); - } catch(\Exception $e) { - $this->title = null; - } - } - return $this->title; + public function getPublishersValue() { + $publishers = $this->getPublishers(); + return is_null($publishers)?null:$publishers->getValue(); } - public function setTitle($value, $lang="fr") { - $oldTitle = $this->getTitle(); - if($oldTitle && $oldTitle->getValue() != $value && $oldTitle->getLang() != $lang) { - $literalTitle = new Literal($value, $lang, null); - $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/elements/1.1/title', $oldTitle, $literalTitle); - //clear cache - $this->title = false; - } - } - - - - public function getTitleValue() { - $title = $this->getTitle(); - return is_null($title)?null:$title->getValue(); - } - - public function getIssued() { - if(is_null($this->issued)) { + public function getDuration() { + if($this->duration === false) { try { - $this->issued = $this->getProvidedCHO()->getLiteral(""); + $this->duration = $this->getProvidedCHO()->getLiteral(''); } catch(\Exception $e) { - $this->issued = null; + $this->duration = null; } } - return $this->issued; - } - - public function getIssuedValue() { - $issued = $this->getIssued(); - return is_null($issued)?null:$issued->getValue(); - } - - public function getModified() { - if(is_null($this->modified)) { - try { - $this->modified = $this->getProvidedCHO()->getLiteral(""); - if(is_null($this->modified)) { - $this->modified = $this->getIssued(); - } - } catch(\Exception $e) { - $this->modified = null; - } - } - return $this->modified; + return $this->duration; } - public function setModified($value = null) { - if(is_null($value)) { - $value = gmdate(\DateTime::ATOM); - } elseif ($value instanceof \DateTime) { - $value = $value->format(\DateTime::ATOM); + public function getDurationValue() { + if($this->durationMs === -1) { + $this->durationMs = Utils::iso8601IntervalToMillis($this->getDuration()); } - $value = preg_replace('/[\+\-]00(\:?)00$/', 'Z', $value); - - $modified = $this->getModified(); - if($value && (!$modified || $modified->getValue() !== $value) ) { - - $newModified = new Literal($value, null, "http://purl.org/dc/terms/W3CDTF"); - $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/terms/modified', $modified, $newModified); - - $this->modified = null; - } - } - - - public function getModifiedValue() { - $modified = $this->getModified(); - return is_null($modified)?null:$modified->getValue(); + return $this->durationMs; } + public function jsonSerialize() { - public function jsonSerialize() { - if(!$this->graph) { - return [ - 'id' => $this->getId(), - ]; - } else { - $res = [ - 'id' => $this->getId(), - 'uri' => $this->getUri(), - 'title' => $this->getTitleValue(), - 'language' => $this->getLanguageValue(), - 'modified' => $this->getModifiedValue(), - 'issued' => $this->getIssuedValue() - ]; + $res = parent::jsonSerialize(); - if($this->language_resolved) { - $res['language_resolved'] = $this->getLanguageResolved(); - } - - return $res; + if($this->graph) { + $res = array_merge($res, [ + 'publishers' => $this->getPublishersValue(), + 'duration' => $this->getDurationValue() + ]); } + return $res; } }