server/src/app/Models/WebResource.php
author ymh <ymh.work@gmail.com>
Thu, 02 Jun 2016 18:24:19 +0200
changeset 168 17f10b56c079
child 169 8fddc113095e
permissions -rw-r--r--
improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning

<?php
namespace CorpusParole\Models;

use CorpusParole\Libraries\RdfModel\RdfModelResource;
use JsonSerializable;
use Log;

/**
 * Model class for Document. Inherit from EasyRd\Resource
 * SELECT DISTINCT ?g WHERE {GRAPH ?g {?s ?p ?o}}
 */
abstract class WebResource extends RdfModelResource implements JsonSerializable {

    public function __construct(...$args) {
        list($uri, $graph) = $args;
        parent::__construct($uri, $graph);
    }

    private $format = -1;

    abstract protected function doClearMemoizationCache();

    public function clearMemoizationCache() {
        $this->format = -1;
        $this->doClearMemoizationCache();
    }

    public function getFormat() {
        if($this->format === -1) {
            $format = $this->getLiteral("<http://purl.org/dc/elements/1.1/format>");
            $this->format = is_null($format)?null:$format->getValue();
        }
        return $this->format;
    }

    public function getUrl() {
        return $this->getUri();
    }

    abstract protected function jsonSerializeExtra();

    public function jsonSerialize() {
        return array_merge(
            [
                'url' => $this->getUrl(),
                'format' => $this->getFormat()
            ],
            $this->jsonSerializeExtra()
        );
    }

}