Skip to content

Commit c3394af

Browse files
author
Harry Moore
committed
add node associate method
1 parent a9980c2 commit c3394af

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/Node.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,43 @@ public function uri()
2020
return $this->branch->uri() . "/nodes/" . $this->id;
2121
}
2222

23+
/**
24+
* Associates a target node to this node.
25+
*
26+
* @param {String|Node} other_node - the id of the target node or the target node itself
27+
* @param {Object|String} association - a string identifying the type of association or a JSON object body for the new assocation node with the association type in _type
28+
* @param {Boolean} association_directionality - if true, a directed association is created. Otherwise the association will be undirected (i.e. mutual)
29+
*/
30+
public function associate($other_node, $association, $association_directionality = false)
31+
{
32+
// $other_node could be a Node object or a sting. If a string then it should be a node id
33+
if ($other_node instanceof Node) {
34+
// pull the other node's id. that's all we need for the .../associate API call
35+
$other_node = $other_node->id;
36+
}
37+
38+
// if $association is a string assume it is the type of the association to create
39+
// if an object assume it is the complete set of properties for the assocation node to create
40+
if (is_a($association, 'String')) {
41+
$association = array("_type" => $association);
42+
}
43+
44+
$uri = $this->uri() . "/associate";
45+
$association_node = null;
46+
try
47+
{
48+
$params = array("node" => $other_node);
49+
$res = $this->client->post($uri, $params, array("_type" => $association));
50+
$association_node = new Node($this, $res);
51+
}
52+
catch (\GuzzleHttp\Exception\ClientException $ex)
53+
{
54+
// return null if not found
55+
}
56+
57+
return $association_node;
58+
}
59+
2360
// Static
2461

2562
public static function nodeList($branch, $data)

tests/NodeTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,26 @@ public function testNodeQueryAndFind()
9191
$node3->delete();
9292
$node4->delete();
9393
}
94+
95+
public function testNodeAssociation()
96+
{
97+
$nodeObj1 = array(
98+
"title" => "Cheese burger",
99+
"meal" => "lunch"
100+
);
101+
$nodeObj2 = array(
102+
"title" => "Ham burger",
103+
"meal" => "lunch"
104+
);
105+
106+
$node1 = $this->branch->createNode($nodeObj1);
107+
$node2 = $this->branch->createNode($nodeObj2);
108+
109+
$associationNode = $node1->associate($node2, "a:linked", true);
110+
111+
$this->assertTrue($associationNode instanceof Node);
112+
113+
$node1->delete();
114+
$node2->delete();
115+
}
94116
}

0 commit comments

Comments
 (0)