Skip to content

Commit

Permalink
Merge pull request #256 from ragol/serialiser-typehint
Browse files Browse the repository at this point in the history
Use type hints in Seraliser classes
  • Loading branch information
njh committed Nov 1, 2015
2 parents f3b5d74 + 5447b78 commit 1371c8a
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 96 deletions.
20 changes: 10 additions & 10 deletions lib/Serialiser.php
Expand Up @@ -61,14 +61,8 @@ protected function addPrefix($qname)
* Check and cleanup parameters passed to serialise() method
* @ignore
*/
protected function checkSerialiseParams(&$graph, &$format)
protected function checkSerialiseParams(&$format)
{
if (is_null($graph) or !is_object($graph) or !($graph instanceof Graph)) {
throw new \InvalidArgumentException(
'$graph should be an EasyRdf\Graph object and cannot be null'
);
}

if (is_null($format) or $format == '') {
throw new \InvalidArgumentException(
'$format cannot be null or empty'
Expand Down Expand Up @@ -100,9 +94,15 @@ protected function reversePropertyCount($resource)
}
}


/**
* Sub-classes must follow this protocol
* @ignore
* Serialise an EasyRdf\Graph into desired format.
*
* @param Graph $graph An EasyRdf\Graph object.
* @param Format|string $format The name of the format to convert to.
* @param array $options
*
* @return string The RDF in the new desired format.
*/
abstract public function serialise($graph, $format, array $options = array());
abstract public function serialise(Graph $graph, $format, array $options = array());
}
13 changes: 7 additions & 6 deletions lib/Serialiser/Arc.php
Expand Up @@ -65,20 +65,20 @@ public function __construct()
}
}


/**
* Serialise an EasyRdf\Graph into RDF format of choice.
*
* @param Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param array $options
*
* @return string The RDF in the new desired format.
* @throws Exception
*
* @return string The RDF in the new desired format.
*/
public function serialise($graph, $format, array $options = array())
public function serialise(Graph $graph, $format, array $options = array())
{
parent::checkSerialiseParams($graph, $format);
parent::checkSerialiseParams($format);

if (array_key_exists($format, self::$supportedTypes)) {
$className = self::$supportedTypes[$format];
Expand All @@ -88,6 +88,7 @@ public function serialise($graph, $format, array $options = array())
);
}

/** @var \ARC2_RDFSerializer $serialiser */
$serialiser = \ARC2::getSer($className);
if ($serialiser) {
return $serialiser->getSerializedIndex(
Expand Down
11 changes: 6 additions & 5 deletions lib/Serialiser/GraphViz.php
Expand Up @@ -252,7 +252,7 @@ protected function serialiseRow($node1, $node2 = null, $attributes = array())
*
* @ignore
*/
protected function serialiseDot($graph)
protected function serialiseDot(Graph $graph)
{
$result = "digraph {\n";

Expand Down Expand Up @@ -353,7 +353,7 @@ protected function serialiseDot($graph)
*
* @ignore
*/
public function renderImage($graph, $format = 'png')
public function renderImage(Graph $graph, $format = 'png')
{
$dot = $this->serialiseDot($graph);

Expand All @@ -364,6 +364,7 @@ public function renderImage($graph, $format = 'png')
);
}


/**
* Serialise an EasyRdf\Graph into a GraphViz dot document.
*
Expand All @@ -373,12 +374,12 @@ public function renderImage($graph, $format = 'png')
* @param string $format The name of the format to convert to.
* @param array $options
*
* @throws Exception
* @return string The RDF in the new desired format.
* @throws Exception
*/
public function serialise($graph, $format, array $options = array())
public function serialise(Graph $graph, $format, array $options = array())
{
parent::checkSerialiseParams($graph, $format);
parent::checkSerialiseParams($format);

switch($format) {
case 'dot':
Expand Down
13 changes: 6 additions & 7 deletions lib/Serialiser/Json.php
Expand Up @@ -49,22 +49,21 @@
class Json extends RdfPhp
{
/**
* Method to serialise an EasyRdf\Graph to RDF/JSON
* Serialise an EasyRdf\Graph into a to RDF/JSON document.
*
* http://n2.talis.com/wiki/RDF_JSON_Specification
* docs/appendix-a-rdf-formats-json.md
*
* @param Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param array $options
*
* @throws Exception
*
* @return string The RDF in the new desired format.
* @throws Exception
*/
public function serialise($graph, $format, array $options = array())
public function serialise(Graph $graph, $format, array $options = array())
{
parent::checkSerialiseParams($graph, $format);
parent::checkSerialiseParams($format);

if ($format != 'json') {
throw new Exception(
Expand Down
16 changes: 10 additions & 6 deletions lib/Serialiser/JsonLd.php
Expand Up @@ -36,6 +36,7 @@
* @license http://www.opensource.org/licenses/bsd-license.php
*/
use EasyRdf\Exception;
use EasyRdf\Graph;
use EasyRdf\Serialiser;

use ML\JsonLD as LD;
Expand All @@ -56,17 +57,20 @@ public function __construct()
}
}


/**
* @param \EasyRdf\Graph $graph
* @param string $format
* @param array $options
* Serialise an EasyRdf\Graph into a JSON-LD document.
*
* @param Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param array $options
*
* @return string The RDF in the new desired format.
* @throws Exception
* @return string
*/
public function serialise($graph, $format, array $options = array())
public function serialise(Graph $graph, $format, array $options = array())
{
parent::checkSerialiseParams($graph, $format);
parent::checkSerialiseParams($format);

if ($format != 'jsonld') {
throw new Exception(__CLASS__.' does not support: '.$format);
Expand Down
12 changes: 6 additions & 6 deletions lib/Serialiser/Ntriples.php
Expand Up @@ -192,20 +192,20 @@ public function serialiseValue($value)
}
}


/**
* Serialise an EasyRdf\Graph into N-Triples
*
* @param Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param array $options
*
* @throws Exception
*
* @return string The RDF in the new desired format.
* @throws Exception
*/
public function serialise($graph, $format, array $options = array())
public function serialise(Graph $graph, $format, array $options = array())
{
parent::checkSerialiseParams($graph, $format);
parent::checkSerialiseParams($format);

if ($format == 'ntriples') {
$nt = '';
Expand Down
11 changes: 7 additions & 4 deletions lib/Serialiser/Rapper.php
Expand Up @@ -36,6 +36,7 @@
* @license http://www.opensource.org/licenses/bsd-license.php
*/
use EasyRdf\Exception;
use EasyRdf\Graph;
use EasyRdf\Utils;

/**
Expand Down Expand Up @@ -71,18 +72,20 @@ public function __construct($rapperCmd = 'rapper')
}
}


/**
* Serialise an EasyRdf\Graph to the RDF format of choice.
*
* @param \EasyRdf\Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param \EasyRdf\Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param array $options
*
* @return string The RDF in the new desired format.
* @throws Exception
*/
public function serialise($graph, $format, array $options = array())
public function serialise(Graph $graph, $format, array $options = array())
{
parent::checkSerialiseParams($graph, $format);
parent::checkSerialiseParams($format);

$ntriples = parent::serialise($graph, 'ntriples');

Expand Down
10 changes: 5 additions & 5 deletions lib/Serialiser/RdfPhp.php
Expand Up @@ -55,16 +55,16 @@ class RdfPhp extends Serialiser
* http://n2.talis.com/wiki/RDF_PHP_Specification
* docs/appendix-a-rdf-formats-php.md
*
* @param Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param array $options
*
* @throws Exception
* @return string The RDF in the new desired format.
* @throws Exception
*/
public function serialise($graph, $format, array $options = array())
public function serialise(Graph $graph, $format, array $options = array())
{
parent::checkSerialiseParams($graph, $format);
parent::checkSerialiseParams($format);

if ($format != 'php') {
throw new Exception(
Expand Down
11 changes: 5 additions & 6 deletions lib/Serialiser/RdfXml.php
Expand Up @@ -196,17 +196,16 @@ protected function rdfxmlResource($res, $showNodeId, $depth = 1)
/**
* Method to serialise an EasyRdf\Graph to RDF/XML
*
* @param Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param array $options
*
* @throws Exception
*
* @return string The RDF in the new desired format.
* @throws Exception
*/
public function serialise($graph, $format, array $options = array())
public function serialise(Graph $graph, $format, array $options = array())
{
parent::checkSerialiseParams($graph, $format);
parent::checkSerialiseParams($format);

if ($format != 'rdfxml') {
throw new Exception(
Expand Down
12 changes: 6 additions & 6 deletions lib/Serialiser/Turtle.php
Expand Up @@ -352,20 +352,20 @@ protected function serialiseSubjects(Graph $graph, $filterType)
return $turtle;
}


/**
* Serialise an EasyRdf\Graph to Turtle.
*
* @param Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param array $options
*
* @throws Exception
*
* @return string The RDF in the new desired format.
* @throws Exception
*/
public function serialise($graph, $format, array $options = array())
public function serialise(Graph $graph, $format, array $options = array())
{
parent::checkSerialiseParams($graph, $format);
parent::checkSerialiseParams($format);

if ($format != 'turtle' and $format != 'n3') {
throw new Exception(
Expand Down
14 changes: 9 additions & 5 deletions test/EasyRdf/Serialiser/NtriplesArray.php
Expand Up @@ -35,7 +35,9 @@
* @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
* @license http://www.opensource.org/licenses/bsd-license.php
*/
use EasyRdf\Exception;
use EasyRdf\Format;
use EasyRdf\Graph;

/**
* Class to serialise an EasyRdf\Graph to an array of triples.
Expand Down Expand Up @@ -65,18 +67,20 @@ protected function compareTriples($a, $b)
}
}


/**
* Serialise an EasyRdf\Graph into an array of N-Triples objects
*
* @param \EasyRdf\Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param array $options
* @param Graph $graph An EasyRdf\Graph object.
* @param string $format The name of the format to convert to.
* @param array $options
*
* @return string The RDF in the new desired format.
* @throws Exception
*/
public function serialise($graph, $format, array $options = array())
public function serialise(Graph $graph, $format, array $options = array())
{
parent::checkSerialiseParams($graph, $format);
parent::checkSerialiseParams($format);

$triples = array();
foreach ($graph->toRdfPhp() as $resource => $properties) {
Expand Down

0 comments on commit 1371c8a

Please sign in to comment.