server/src/app/Models/Document.php
changeset 2 00e2916104fe
child 3 2b3247d02769
equal deleted inserted replaced
1:01a844d292ac 2:00e2916104fe
       
     1 <?php
       
     2 namespace CorpusParole\Models;
       
     3 
       
     4 use Config;
       
     5 use CorpusParole\Libraries\Utils;
       
     6 use Log;
       
     7 
       
     8 /**
       
     9  * Model class for Document. Inherit from EasyRdf_Resource
       
    10  */
       
    11 class Document extends \EasyRdf_Resource {
       
    12 
       
    13     public function __construct($uri, $graph = null) {
       
    14         parent::__construct($uri, $graph);
       
    15         $this->innerDocument = $this->getResource('foaf:primaryTopic');
       
    16     }
       
    17 
       
    18     private $id = null;
       
    19     private $innerDocument = null;
       
    20 
       
    21     public function getId() {
       
    22         if(is_null($this->id)) {
       
    23             $this->id = substr($this->uri, strlen(Config::get('corpusparole.cocoon_doc_id_base_uri')));
       
    24         }
       
    25         return $this->id;
       
    26     }
       
    27 
       
    28     public function getTitle() {
       
    29         return $this->innerDocument->getLiteral('dc11:title');
       
    30     }
       
    31 
       
    32     public function getPublishers() {
       
    33         return $this->innerDocument->allLiterals('dc11:publisher');
       
    34     }
       
    35 
       
    36     public function getMediaArray() {
       
    37 
       
    38         //TODO: add media type
       
    39         $res = [];
       
    40         $formats = $this->innerDocument->allResources("dc:isFormatOf");
       
    41         foreach ($formats as $f) {
       
    42             $uri = $f->getUri();
       
    43             $mimetype = Utils::get_mimetype($uri);
       
    44             array_push($res, ["url" => $uri, "format" => $mimetype]);
       
    45         }
       
    46         array_push($res, ["url" => $this->innerDocument->getUri(), "format" => $this->innerDocument->getLiteral('dc11:format')]);
       
    47         return $res;
       
    48     }
       
    49 
       
    50     public function getTypes() {
       
    51         return $this->innerDocument->all('dc11:type');
       
    52     }
       
    53 
       
    54     public function getDiscourseTypes() {
       
    55         return array_values(array_filter($this->getTypes(), function($v) {
       
    56             return $v instanceof \EasyRdf_Literal && $v->getDatatypeUri() === Config::get('constants.OLAC_DISCOURSE_TYPE')['uri'];
       
    57         }));
       
    58     }
       
    59 
       
    60     public function getOtherTypes() {
       
    61         $res = array_values(array_filter($this->getTypes(), function($v) {
       
    62             return $v instanceof \EasyRdf_Resource || $v->getDatatypeUri() !== Config::get('constants.OLAC_DISCOURSE_TYPE')['uri'];
       
    63         }));
       
    64         return $res;
       
    65     }
       
    66 
       
    67     public function updateDiscourseTypes(array $discoursesTypes) {
       
    68         foreach($this->getDiscourseTypes() as $discourseType) {
       
    69             $this->innerDocument->delete('dc11:type', $discourseType);
       
    70         }
       
    71         // re-add them
       
    72 
       
    73         foreach($discoursesTypes as $dType) {
       
    74             $this->innerDocument->add('dc11:type', new \EasyRdf_Literal($dType, null, Config::get('constants.OLAC_DISCOURSE_TYPE')['uri']));
       
    75         }
       
    76     }
       
    77 
       
    78     public function isIsomorphic($doc) {
       
    79         return \EasyRdf_Isomorphic::isomorphic($this->graph, $doc->graph);
       
    80     }
       
    81 
       
    82     /*
       
    83      * Clone document.
       
    84      * clone also the innerDocumenent
       
    85      */
       
    86     public function __clone() {
       
    87 
       
    88         $this->graph = new \EasyRdf_Graph($this->graph->getUri(), $this->graph->toRdfPhp());
       
    89         $this->innerDocument = $this->getResource('foaf:primaryTopic');
       
    90     }
       
    91 
       
    92 }