server/src/app/Models/DocumentResult.php
changeset 260 64caee7ce38d
parent 168 17f10b56c079
child 275 a4d8618c2f1b
equal deleted inserted replaced
259:6af61adbd570 260:64caee7ce38d
    12 use EasyRdf\Graph;
    12 use EasyRdf\Graph;
    13 
    13 
    14 
    14 
    15 /**
    15 /**
    16  */
    16  */
    17 class DocumentResult extends RdfModelResource implements JsonSerializable {
    17 class DocumentResult extends DocumentBase {
    18 
    18 
    19     public function __construct($uri, $graph = null) {
    19     public function __construct($uri, $graph = null) {
    20         //print($graph->dump('html'));
       
    21         parent::__construct($uri, $graph);
    20         parent::__construct($uri, $graph);
    22     }
    21     }
    23 
    22 
    24     private $id = null;
    23     private $publishers = false;
       
    24     private $duration = false;
       
    25     private $durationMs = -1;
    25 
    26 
    26     // memoization
    27     protected function clearMemoizationCache() {
    27     private $providedCHO = null;
    28         parent::clearMemoizationCache();
    28     private $title = false;
    29         $this->publishers = false;
    29     private $lang = null;
    30         $this->duration = false;
    30     private $langResolved = null;
    31         $this->$durationMs = -1;
    31     private $issued = null;
       
    32     private $modified = null;
       
    33 
    32 
    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     }
    33     }
    40 
    34 
    41     protected function clearMemoizationCache() {
    35     public function getPublishers() {
    42         $this->providedCHO = null;
    36         if($this->publishers === false) {
    43         $this->title = false;
    37             try {
    44         $this->lang = null;
    38                 $this->publishers = $this->getProvidedCHO()->getLiteral('dc11:publisher');
    45         $this->langResolved = null;
    39             } catch(\Exception $e) {
    46         $this->issued = null;
    40                 $this->publishers = null;
    47         $this->modified = null;
    41             }
       
    42         }
       
    43         return $this->publishers;
    48     }
    44     }
    49 
    45 
    50     public function getId() {
    46     public function getPublishersValue() {
    51         if(is_null($this->id)) {
    47         $publishers = $this->getPublishers();
    52             $ids = $this->getProvidedCHO()->all('<http://purl.org/dc/elements/1.1/identifier>');
    48         return is_null($publishers)?null:$publishers->getValue();
    53             foreach ($ids as $id) {
    49     }
    54                 if($id instanceof Literal && strpos($id->getValue(), config('corpusparole.corpus_id_scheme')) === 0) {
    50 
    55                     $this->id = $id->getValue();
    51     public function getDuration() {
    56                 }
    52         if($this->duration === false) {
    57             }
    53             try {
    58             if(is_null($this->id)) {
    54                 $this->duration = $this->getProvidedCHO()->getLiteral('<http://purl.org/dc/terms/extent>');
    59                 $this->id = CocoonUtils::getIdFromCorpusUri($this->uri);
    55             } catch(\Exception $e) {
       
    56                 $this->duration = null;
    60             }
    57             }
    61         }
    58         }
    62         return $this->id;
    59         return $this->duration;
    63     }
    60     }
    64 
    61 
    65     public function getLanguage() {
    62     public function getDurationValue() {
    66         if(is_null($this->lang)) {
    63         if($this->durationMs === -1) {
    67             try {
    64             $this->durationMs = Utils::iso8601IntervalToMillis($this->getDuration());
    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         }
    65         }
    76         return $this->lang;
    66         return $this->durationMs;
    77     }
    67     }
    78 
    68 
    79     public function getLanguageValue() {
    69     public function jsonSerialize() {
    80         $lang = $this->getLanguage();
    70 
    81         if($lang instanceof Resource) {
    71         $res = parent::jsonSerialize();
    82             return $lang->getUri();
    72 
    83         } else if($lan instanceof Literal) {
    73         if($this->graph) {
    84             return $lang->getValue();
    74             $res = array_merge($res, [
       
    75                 'publishers' => $this->getPublishersValue(),
       
    76                 'duration' => $this->getDurationValue()
       
    77             ]);
    85         }
    78         }
    86         return null;
    79         return $res;
    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     }
    80     }
   202 }
    81 }