From 6de9485e12f6a65e494f23c719f78646a46f6b15 Mon Sep 17 00:00:00 2001 From: Markus Lanthaler Date: Wed, 6 Feb 2013 15:40:56 +0100 Subject: [PATCH] Make interfaces more fluent This addresses #15. --- Document.php | 6 +++++- DocumentInterface.php | 4 ++++ Graph.php | 4 ++++ GraphInterface.php | 4 ++++ LanguageTaggedString.php | 4 ++++ Node.php | 30 +++++++++++++++++++++--------- NodeInterface.php | 16 ++++++++++++++++ TypedValue.php | 4 ++++ Value.php | 4 ++++ 9 files changed, 66 insertions(+), 10 deletions(-) diff --git a/Document.php b/Document.php index 339b07a..0d8d6ee 100644 --- a/Document.php +++ b/Document.php @@ -84,6 +84,8 @@ public function __construct($iri = null) public function setIri($iri) { $this->iri = new IRI($iri); + + return $this; } /** @@ -150,7 +152,7 @@ public function removeGraph($graph = null) if (null === $graph) { $this->defaultGraph = new Graph($this); - return; + return $this; } $name = $graph; @@ -171,6 +173,8 @@ public function removeGraph($graph = null) unset($this->namedGraphs[$name]); } + + return $this; } /** diff --git a/DocumentInterface.php b/DocumentInterface.php index 00c8c4b..84bdde2 100644 --- a/DocumentInterface.php +++ b/DocumentInterface.php @@ -22,6 +22,8 @@ interface DocumentInterface * Set the document's IRI * * @param string|IRI The IRI. + * + * @return self */ public function setIri($iri); @@ -80,6 +82,8 @@ public function containsGraph($name); * @param null|string|GraphInterface $graph The graph (or its name) to * remove. If null is passed, * the default will be reset. + * + * @return self */ public function removeGraph($graph = null); } diff --git a/Graph.php b/Graph.php index 23362c5..a8fe531 100644 --- a/Graph.php +++ b/Graph.php @@ -84,6 +84,8 @@ public function removeNode(NodeInterface $node) } unset($this->nodes[$id]); + + return $this; } /** @@ -167,6 +169,8 @@ public function removeFromDocument() $this->document = null; $doc->removeGraph($this); + + return $this; } /** diff --git a/GraphInterface.php b/GraphInterface.php index f7414d9..f202ef1 100644 --- a/GraphInterface.php +++ b/GraphInterface.php @@ -39,6 +39,8 @@ public function createNode($id = null); * document. * * @param NodeInterface $node The node to remove from the document. + * + * @return self */ public function removeNode(NodeInterface $node); @@ -94,6 +96,8 @@ public function getDocument(); /** * Removes the graph from the document + * + * @return self */ public function removeFromDocument(); diff --git a/LanguageTaggedString.php b/LanguageTaggedString.php index 5337087..8e80c82 100644 --- a/LanguageTaggedString.php +++ b/LanguageTaggedString.php @@ -43,6 +43,8 @@ public function __construct($value, $language) * * @param string $language The language. * + * @return self + * * @throws \InvalidArgumentException If the language is not a string. No * further checks are currently done. */ @@ -53,6 +55,8 @@ public function setLanguage($language) } $this->language = $language; + + return $this; } /** diff --git a/Node.php b/Node.php index 343c780..71202f4 100644 --- a/Node.php +++ b/Node.php @@ -81,7 +81,9 @@ public function setType($type) } } - return $this->setProperty(self::TYPE, $type); + $this->setProperty(self::TYPE, $type); + + return $this; } /** @@ -89,7 +91,9 @@ public function setType($type) */ public function addType(NodeInterface $type) { - return $this->addPropertyValue(self::TYPE, $type); + $this->addPropertyValue(self::TYPE, $type); + + return $this; } /** @@ -97,7 +101,9 @@ public function addType(NodeInterface $type) */ public function removeType(NodeInterface $type) { - return $this->removePropertyValue(self::TYPE, $type); + $this->removePropertyValue(self::TYPE, $type); + + return $this; } /** @@ -157,6 +163,8 @@ public function removeFromGraph() $this->graph = null; $g->removeNode($this); + + return $this; } /** @@ -174,11 +182,11 @@ public function setProperty($property, $value) { if (null === $value) { $this->removeProperty($property); - - return; + } else { + $this->doMergeIntoProperty((string) $property, array(), $value); } - $this->doMergeIntoProperty((string) $property, array(), $value); + return $this; } /** @@ -195,6 +203,8 @@ public function addPropertyValue($property, $value) } $this->doMergeIntoProperty((string) $property, $existing, $value); + + return $this; } /** @@ -248,7 +258,7 @@ private function doMergeIntoProperty($property, $existingValues, $value) public function removeProperty($property) { if (!isset($this->properties[(string) $property])) { - return; + return $this; } $values = is_array($this->properties[(string) $property]) @@ -262,6 +272,8 @@ public function removeProperty($property) } unset($this->properties[(string) $property]); + + return $this; } /** @@ -270,7 +282,7 @@ public function removeProperty($property) public function removePropertyValue($property, $value) { if (!$this->isValidPropertyValue($value) || !isset($this->properties[(string) $property])) { - return; + return $this; } $values =& $this->properties[(string) $property]; @@ -293,7 +305,7 @@ public function removePropertyValue($property, $value) if (0 === count($values)) { unset($this->properties[(string) $property]); - return; + return $this; } $this->properties[(string) $property] = array_values($values); // re-index the array diff --git a/NodeInterface.php b/NodeInterface.php index 8dfad58..6c8b7b5 100644 --- a/NodeInterface.php +++ b/NodeInterface.php @@ -28,6 +28,8 @@ public function getId(); * * @param null|NodeInterface|array[NodeInterface] The type(s) of this node. * + * @return self + * * @throws \InvalidArgumentException If type is not null, a Node or an * array of Nodes. */ @@ -37,6 +39,8 @@ public function setType($type); * Add a type to this node * * @param NodeInterface The type to add. + * + * @return self */ public function addType(NodeInterface $type); @@ -44,6 +48,8 @@ public function addType(NodeInterface $type); * Remove a type from this node * * @param NodeInterface The type to remove. + * + * @return self */ public function removeType(NodeInterface $type); @@ -78,6 +84,8 @@ public function getGraph(); * * This will also remove all references to and from other nodes in this * node's graph. + * + * @return self */ public function removeFromGraph(); @@ -104,6 +112,8 @@ public function isBlankNode(); * @param mixed $value The value of the property. This MUST NOT be * an array. Use null to remove the property. * + * @return self + * * @throws \InvalidArgumentException If value is an array or an object * which is neither a language-tagged * string nor a typed value or a node. @@ -125,6 +135,8 @@ public function setProperty($property, $value); * @param mixed $value The value of the property. This MUST NOT be * an array. * + * @return self + * * @throws \InvalidArgumentException If value is an array or an object * which is neither a language-tagged * string nor a typed value or a node. @@ -135,6 +147,8 @@ public function addPropertyValue($property, $value); * Removes a property and all it's values * * @param string $property The name of the property to remove. + * + * @return self */ public function removeProperty($property); @@ -144,6 +158,8 @@ public function removeProperty($property); * @param string $property The name of the property. * @param mixed $value The value of the property. This MUST NOT be * an array. + * + * @return self */ public function removePropertyValue($property, $value); diff --git a/TypedValue.php b/TypedValue.php index 3a22cd6..10e2c86 100644 --- a/TypedValue.php +++ b/TypedValue.php @@ -46,6 +46,8 @@ public function __construct($value, $type) * * @param string $type The type. * + * @return self + * * @throws \InvalidArgumentException If the type is not a string. No * further checks are currently done. */ @@ -56,6 +58,8 @@ public function setType($type) } $this->type = $type; + + return $this; } /** diff --git a/Value.php b/Value.php index 1370c15..caa357c 100644 --- a/Value.php +++ b/Value.php @@ -31,6 +31,8 @@ abstract class Value implements JsonLdSerializable * * @param string $value The value. * + * @return self + * * @throws \InvalidArgumentException If the value is not a string. */ public function setValue($value) @@ -40,6 +42,8 @@ public function setValue($value) } $this->value = $value; + + return $this; } /**