Skip to content

Commit 2c11223

Browse files
committed
features, attachments, associations, traversal, tests
1 parent b6acc13 commit 2c11223

18 files changed

+851
-68
lines changed

res/cloudcms.png

11.5 KB
Loading

res/headphones.png

1.54 KB
Loading

src/AbstractDocument.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ abstract class AbstractDocument
66
{
77
protected $client;
88
public $id;
9+
public $data;
910

1011
public function __construct($client, $data)
1112
{
@@ -22,7 +23,7 @@ public function reload()
2223
try
2324
{
2425
$newData = $this->client->get($this->uri());
25-
$this->data =$newData;
26+
$this->data = $newData;
2627
}
2728
catch (\GuzzleHttp\Exception\ClientException $ex)
2829
{

src/Association.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace CloudCMS;
4+
5+
class Association extends BaseNode
6+
{
7+
public function __construct($branch, $data)
8+
{
9+
parent::__construct($branch, $data);
10+
}
11+
12+
public function getSourceTypeQName()
13+
{
14+
return $this->data["source_type"];
15+
}
16+
17+
public function setSourceTypeQName($sourceTypeQName)
18+
{
19+
$this->data["source_type"] = $sourceTypeQName;
20+
}
21+
22+
public function getSourceNodeId()
23+
{
24+
return $this->data["source"];
25+
}
26+
27+
public function setSourceNodeId($sourceNodeId)
28+
{
29+
$this->data["source"] = $sourceNodeId;
30+
}
31+
32+
public function getTargetTypeQName()
33+
{
34+
return $this->data["target_type"];
35+
}
36+
37+
public function setTargetTypeQName($targetTypeQName)
38+
{
39+
$this->data["target_type"] = $targetTypeQName;
40+
}
41+
42+
public function getTargetNodeId()
43+
{
44+
return $this->data["target"];
45+
}
46+
47+
public function setTargetNodeId($targetNodeId)
48+
{
49+
$this->data["target"] = $targetNodeId;
50+
}
51+
52+
public function getDirectionality()
53+
{
54+
return $this->data["directionality"];
55+
}
56+
57+
public function setDirectionality($directionality)
58+
{
59+
$this->data["directionality"] = $directionality;
60+
}
61+
62+
public function readSourceNode()
63+
{
64+
$nodeId = $this->getSourceNodeId();
65+
return $this->branch->readNode($nodeId);
66+
}
67+
68+
public function readTargetNode()
69+
{
70+
$nodeId = $this->getTargetNodeId();
71+
return $this->branch->readNode($nodeId);
72+
}
73+
74+
// Static
75+
76+
public static function associationList($branch, $data)
77+
{
78+
$associations = array();
79+
foreach($data as $obj)
80+
{
81+
$association = new Association($branch, $obj);
82+
array_push($associations, $association);
83+
}
84+
85+
return $associations;
86+
}
87+
}

src/Attachment.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace CloudCMS;
4+
5+
class Attachment
6+
{
7+
public $attachable;
8+
public $id;
9+
public $objectId;
10+
public $length;
11+
public $filename;
12+
public $contentType;
13+
14+
public function __construct($attachable, $obj)
15+
{
16+
$this->attachable = $attachable;
17+
$this->id = $obj["attachmentId"];
18+
$this->objectId = $obj["objectId"];
19+
$this->length = $obj["length"];
20+
$this->filename = $obj["filename"];
21+
$this->contentType = $obj["contentType"];
22+
}
23+
24+
public function downloadAttachment()
25+
{
26+
return $this->attachable->downloadAttachment($this->id);
27+
}
28+
29+
// Static
30+
public static function attachmentMap($attachable, $data)
31+
{
32+
$attachments = array();
33+
foreach($data as $obj)
34+
{
35+
$attachment = new Attachment($attachable, $obj);
36+
$attachments[$attachment->id] = $attachment;
37+
}
38+
39+
return $attachments;
40+
}
41+
42+
}

src/BaseNode.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace CloudCMS;
4+
5+
abstract class BaseNode extends AbstractRepositoryDocument
6+
{
7+
protected $branch;
8+
public $branchId;
9+
10+
public function __construct($branch, $data)
11+
{
12+
parent::__construct($branch->repository, $data);
13+
14+
$this->branch = $branch;
15+
$this->branchId = $branch->id;
16+
}
17+
18+
public function uri()
19+
{
20+
return $this->branch->uri() . "/nodes/" . $this->id;
21+
}
22+
23+
public function downloadAttachment($attachmentId = "default")
24+
{
25+
$uri = $this->uri() . "/attachments/" . $attachmentId;
26+
return $this->client->download($uri);
27+
}
28+
29+
public function uploadAttachment($file, $contentType, $attachmentId = "default", $filename = null)
30+
{
31+
$uri = $this->uri() . "/attachments/" . $attachmentId;
32+
$name = $attachmentId;
33+
if ($filename != null)
34+
{
35+
$name = $filename;
36+
}
37+
38+
return $this->client->upload($uri, $name, $file, $contentType);
39+
}
40+
41+
public function deleteAttachment($attachmentId = "default")
42+
{
43+
$uri = $this->uri() . "/attachments/" . $attachmentId;
44+
return $this->client->delete($uri);
45+
}
46+
47+
public function listAttachments()
48+
{
49+
$uri = $this->uri() . "/attachments";
50+
$response = $this->client->get($uri);
51+
52+
return Attachment::attachmentMap($this, $response["rows"]);
53+
}
54+
55+
public function getFeatureIds()
56+
{
57+
$featuresObj = $this->data["_features"] ?? array();
58+
return array_keys($featuresObj);
59+
}
60+
61+
public function getFeature($featureId)
62+
{
63+
$featuresObj = $this->data["_features"] ?? array();
64+
return $featuresObj[$featureId] ?? null;
65+
}
66+
67+
public function hasFeature($featureId)
68+
{
69+
$features_obj = $this->data["_features"] ?? array();
70+
return array_key_exists($featureId, $features_obj);
71+
}
72+
73+
public function addFeature($featureId, $featureConfig)
74+
{
75+
$uri = $this->uri() . "/features/" . $featureId;
76+
$this->client->post($uri, array(), $featureConfig);
77+
$this->reload();
78+
}
79+
80+
public function removeFeature($featureId)
81+
{
82+
$uri = $this->uri() . "/features/" . $featureId;
83+
$this->client->delete($uri);
84+
$this->reload();
85+
}
86+
87+
// Static
88+
89+
public static function buildNode($branch, $data, $forceAssociation = false)
90+
{
91+
if ($forceAssociation || (array_key_exists("is_association", $data) && $data["is_association"] == true))
92+
{
93+
return new Association($branch, $data);
94+
}
95+
else
96+
{
97+
return new Node($branch, $data);
98+
}
99+
}
100+
101+
public static function nodeList($branch, $data)
102+
{
103+
$nodes = array();
104+
foreach($data as $obj)
105+
{
106+
$node = BaseNode::buildNode($branch, $obj);
107+
array_push($nodes, $node);
108+
}
109+
110+
return $nodes;
111+
}
112+
}

src/Branch.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function readNode($nodeId)
2828
try
2929
{
3030
$res = $this->client->get($uri);
31-
$node = new Node($this, $res);
31+
$node = BaseNode::buildNode($this, $res);
3232
}
3333
catch (\GuzzleHttp\Exception\ClientException $ex)
3434
{
@@ -38,12 +38,28 @@ public function readNode($nodeId)
3838
return $node;
3939
}
4040

41+
public function rootNode()
42+
{
43+
return $this->readNode("root");
44+
}
45+
4146
public function queryNodes($query, $pagination = array())
4247
{
4348
$uri = $this->uri() . "/nodes/query";
4449
$res = $this->client->post($uri, $pagination, $query);
4550

46-
$nodeList = Node::nodeList($this, $res["rows"]);
51+
$nodeList = BaseNode::nodeList($this, $res["rows"]);
52+
return $nodeList;
53+
}
54+
55+
public function searchNodes($text, $pagination = array())
56+
{
57+
$uri = $this->uri() . "/nodes/search";
58+
$params = $pagination;
59+
$params['text'] = $text;
60+
61+
$res = $this->client->get($uri, $params);
62+
$nodeList = BaseNode::nodeList($this, $res["rows"]);
4763
return $nodeList;
4864
}
4965

@@ -52,11 +68,11 @@ public function findNodes($config, $pagination = array())
5268
$uri = $this->uri() . "/nodes/find";
5369
$res = $this->client->post($uri, $pagination, $config);
5470

55-
$nodeList = Node::nodeList($this, $res["rows"]);
71+
$nodeList = BaseNode::nodeList($this, $res["rows"]);
5672
return $nodeList;
5773
}
5874

59-
public function createNode($obj, $options = array())
75+
public function createNode($obj = array(), $options = array())
6076
{
6177
$uri = $this->uri() . "/nodes";
6278

src/CloudCMS.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function connect($config)
2525
$missingKeys = array_diff(self::$requiredConfig, $configKeys);
2626
if (!empty($missingKeys))
2727
{
28-
throw new InvalidArgumentException("Missing required config keys: " . implode(", ", $missingKeys));
28+
throw new \InvalidArgumentException("Missing required config keys: " . implode(", ", $missingKeys));
2929
}
3030
$this->baseURL = $config["baseURL"];
3131

@@ -45,7 +45,7 @@ public function connect($config)
4545
return $this->readPlatform();
4646
}
4747

48-
public function request($method, $uri, $params = array(), $data = array())
48+
public function request($method, $uri, $params = array(), $data = array(), $useJson = true)
4949
{
5050
// Refresh token if expired
5151
if ($this->token->hasExpired())
@@ -80,14 +80,26 @@ public function request($method, $uri, $params = array(), $data = array())
8080
}
8181
else
8282
{
83-
$request = $this->provider->getAuthenticatedRequest($method, $url, $this->token, [
84-
"body" => json_encode((object)$data)
85-
]);
83+
$payload = $data;
84+
if ($useJson)
85+
{
86+
$payload = array(
87+
"body" => json_encode((object)$data)
88+
);
89+
}
90+
91+
$request = $this->provider->getAuthenticatedRequest($method, $url, $this->token, $payload);
8692
}
8793

8894
$response = $this->provider->getResponse($request);
8995

90-
return json_decode($response->getBody(), true);
96+
$result = $response->getBody();
97+
if ($useJson)
98+
{
99+
$result = json_decode($result, true);
100+
}
101+
102+
return $result;
91103
}
92104

93105
public function get($uri, $params = array())
@@ -109,6 +121,24 @@ public function delete($uri, $params = array())
109121
{
110122
return $this->request("DELETE", $uri, $params);
111123
}
124+
125+
public function download($uri, $params = array())
126+
{
127+
$response = $this->request("GET", $uri, $params, null, false);
128+
return $response->getContents();
129+
}
130+
131+
public function upload($uri, $name, $file, $mimetype, $params = array())
132+
{
133+
$data = array(
134+
"body" => $file,
135+
"headers" => ["Content-Type" => $mimetype]
136+
);
137+
138+
$params["filename"] = $name;
139+
140+
return $this->request("POST", $uri, $params, $data, false);
141+
}
112142

113143
public function readPlatform()
114144
{

0 commit comments

Comments
 (0)