diff -r 2b3247d02769 -r f55970e41793 server/src/app/Models/Document.php --- a/server/src/app/Models/Document.php Wed Jun 24 01:36:46 2015 +0200 +++ b/server/src/app/Models/Document.php Mon Oct 05 17:02:10 2015 +0200 @@ -4,16 +4,22 @@ use Config; use CorpusParole\Libraries\Utils; use CorpusParole\Libraries\RdfModel\RdfModelResource; +use JsonSerializable; use Log; +use EasyRdf\Literal; +use EasyRdf\Resource; +use EasyRdf\Graph; +use EasyRdf\Isomorphic; + /** - * Model class for Document. Inherit from EasyRdf_Resource + * Model class for Document. Inherit from EasyRd\Resource * SELECT DISTINCT ?g WHERE {GRAPH ?g {?s ?p ?o}} */ -class Document extends RdfModelResource { +class Document extends RdfModelResource implements JsonSerializable { public function __construct($uri, $graph = null) { - parent::__construct($uri, $graph, 'foaf:primaryTopic'); + parent::__construct($uri, $graph); } private $id = null; @@ -26,40 +32,60 @@ } public function getTitle() { - return $this->innerDocument->getLiteral('dc11:title'); + try { + return $this->getLiteral(''); + } catch(\Exception $e) { + return null; + } } public function getPublishers() { - return $this->innerDocument->allLiterals('dc11:publisher'); + try { + return $this->allLiterals('dc11:publisher'); + } catch(\Exception $e) { + return []; + } } public function getMediaArray() { //TODO: add media type $res = []; - $formats = $this->innerDocument->allResources("dc:isFormatOf"); + $formats = []; + try { + $formats = $this->allResources("dc:isFormatOf"); + } catch(\Exception $e) { + // do nothing + } foreach ($formats as $f) { $uri = $f->getUri(); $mimetype = Utils::get_mimetype($uri); array_push($res, ["url" => $uri, "format" => $mimetype]); } - array_push($res, ["url" => $this->innerDocument->getUri(), "format" => $this->innerDocument->getLiteral('dc11:format')]); + + $format = null; + try { + $format = $this->getLiteral('dc11:format'); + } catch(\Exception $e) { + // do nothing + } + array_push($res, ["url" => $this->getUri(), "format" => $format]); return $res; } public function getTypes() { - return $this->innerDocument->all('dc11:type'); + return $this->all('dc11:type'); } public function getDiscourseTypes() { return array_values(array_filter($this->getTypes(), function($v) { - return $v instanceof \EasyRdf_Literal && $v->getDatatypeUri() === Config::get('constants.OLAC_DISCOURSE_TYPE')['uri']; + return $v instanceof Literal && $v->getDatatypeUri() === Config::get('constants.OLAC_DISCOURSE_TYPE')['uri']; })); } public function getOtherTypes() { $res = array_values(array_filter($this->getTypes(), function($v) { - return $v instanceof \EasyRdf_Resource || $v->getDatatypeUri() !== Config::get('constants.OLAC_DISCOURSE_TYPE')['uri']; + return $v instanceof Resource || $v->getDatatypeUri() !== Config::get('constants.OLAC_DISCOURSE_TYPE')['uri']; })); return $res; } @@ -69,19 +95,19 @@ $this->startDelta(); foreach($this->getDiscourseTypes() as $discourseType) { - $this->innerDocument->delete('dc11:type', $discourseType); - $this->currentDelta->deletedGraph->add($this->innerDocument, 'dc11:type', $discourseType); + $this->delete('dc11:type', $discourseType); + $this->currentDelta->getDeletedGraph()->add($this, 'dc11:type', new Literal($discourseType, null, Config::get('constants.OLAC_DISCOURSE_TYPE')['uri'])); } // re-add them foreach($discoursesTypes as $dType) { - $this->innerDocument->add('dc11:type', new \EasyRdf_Literal($dType, null, Config::get('constants.OLAC_DISCOURSE_TYPE')['uri'])); - $this->currentDelta->addedGraph->add($this->innerDocument, 'dc11:type', $discourseType); + $this->add('dc11:type', new Literal($dType, null, Config::get('constants.OLAC_DISCOURSE_TYPE')['uri'])); + $this->currentDelta->getAddedGraph()->add($this, 'dc11:type', new Literal($dType, null, Config::get('constants.OLAC_DISCOURSE_TYPE')['uri'])); } } public function isIsomorphic($doc) { - return \EasyRdf_Isomorphic::isomorphic($this->graph, $doc->graph); + return Isomorphic::isomorphic($this->graph, $doc->graph); } /* @@ -90,8 +116,35 @@ */ public function __clone() { - $this->graph = new \EasyRdf_Graph($this->graph->getUri(), $this->graph->toRdfPhp()); - $this->innerDocument = $this->getResource('foaf:primaryTopic'); + $this->graph = new Graph($this->graph->getUri(), $this->graph->toRdfPhp()); + } + + public function jsonSerialize() { + if(!$this->graph) { + return [ + 'id' => $this->getId(), + ]; + } else { + $mediaArray = array_map( + function($m) { + $f = Utils::process_literal_or_string($m['format']); + return ['url' => $m['url'], 'format' => $f];}, + $this->getMediaArray() + ); + + $publishers = array_map( + function($v) { return Utils::process_literal_or_string($v); }, + $this->getPublishers() + ); + + return [ + 'id' => $this->getId(), + 'uri' => $this->getUri(), + 'title' => $this->getTitle()->getValue(), + 'publishers' => $publishers, + 'mediaArray'=> $mediaArray + ]; + } } }