Skip to content

Commit

Permalink
Make interfaces more fluent
Browse files Browse the repository at this point in the history
This addresses #15.
  • Loading branch information
lanthaler committed Feb 6, 2013
1 parent 908b759 commit 6de9485
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 10 deletions.
6 changes: 5 additions & 1 deletion Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public function __construct($iri = null)
public function setIri($iri)
{
$this->iri = new IRI($iri);

return $this;
}

/**
Expand Down Expand Up @@ -150,7 +152,7 @@ public function removeGraph($graph = null)
if (null === $graph) {
$this->defaultGraph = new Graph($this);

return;
return $this;
}

$name = $graph;
Expand All @@ -171,6 +173,8 @@ public function removeGraph($graph = null)

unset($this->namedGraphs[$name]);
}

return $this;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions DocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ interface DocumentInterface
* Set the document's IRI
*
* @param string|IRI The IRI.
*
* @return self
*/
public function setIri($iri);

Expand Down Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public function removeNode(NodeInterface $node)
}

unset($this->nodes[$id]);

return $this;
}

/**
Expand Down Expand Up @@ -167,6 +169,8 @@ public function removeFromDocument()
$this->document = null;

$doc->removeGraph($this);

return $this;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions GraphInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -94,6 +96,8 @@ public function getDocument();

/**
* Removes the graph from the document
*
* @return self
*/
public function removeFromDocument();

Expand Down
4 changes: 4 additions & 0 deletions LanguageTaggedString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -53,6 +55,8 @@ public function setLanguage($language)
}

$this->language = $language;

return $this;
}

/**
Expand Down
30 changes: 21 additions & 9 deletions Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,29 @@ public function setType($type)
}
}

return $this->setProperty(self::TYPE, $type);
$this->setProperty(self::TYPE, $type);

return $this;
}

/**
* {@inheritdoc}
*/
public function addType(NodeInterface $type)
{
return $this->addPropertyValue(self::TYPE, $type);
$this->addPropertyValue(self::TYPE, $type);

return $this;
}

/**
* {@inheritdoc}
*/
public function removeType(NodeInterface $type)
{
return $this->removePropertyValue(self::TYPE, $type);
$this->removePropertyValue(self::TYPE, $type);

return $this;
}

/**
Expand Down Expand Up @@ -157,6 +163,8 @@ public function removeFromGraph()
$this->graph = null;

$g->removeNode($this);

return $this;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -195,6 +203,8 @@ public function addPropertyValue($property, $value)
}

$this->doMergeIntoProperty((string) $property, $existing, $value);

return $this;
}

/**
Expand Down Expand Up @@ -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])
Expand All @@ -262,6 +272,8 @@ public function removeProperty($property)
}

unset($this->properties[(string) $property]);

return $this;
}

/**
Expand All @@ -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];
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -37,13 +39,17 @@ public function setType($type);
* Add a type to this node
*
* @param NodeInterface The type to add.
*
* @return self
*/
public function addType(NodeInterface $type);

/**
* Remove a type from this node
*
* @param NodeInterface The type to remove.
*
* @return self
*/
public function removeType(NodeInterface $type);

Expand Down Expand Up @@ -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();

Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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);

Expand All @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions TypedValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -56,6 +58,8 @@ public function setType($type)
}

$this->type = $type;

return $this;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -40,6 +42,8 @@ public function setValue($value)
}

$this->value = $value;

return $this;
}

/**
Expand Down

0 comments on commit 6de9485

Please sign in to comment.