Skip to content

Commit 5e2d1af

Browse files
committed
added some view support
1 parent a9ba3ed commit 5e2d1af

File tree

8 files changed

+440
-3
lines changed

8 files changed

+440
-3
lines changed

lib/ArangoDBClient/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Document implements \JsonSerializable
3131
/**
3232
* The document key (might be NULL for new documents)
3333
*
34-
* @var string - document id
34+
* @var string - document key
3535
*/
3636
protected $_key;
3737

lib/ArangoDBClient/Urls.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ abstract class Urls
3737
* URL base part for all graph-related REST calls
3838
*/
3939
const URL_GRAPH = '/_api/gharial';
40+
41+
/**
42+
* URL base part for all view-related REST calls
43+
*/
44+
const URL_VIEW = '/_api/view';
4045

4146
/**
4247
* URL part vertex-related graph REST calls

lib/ArangoDBClient/View.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
/**
4+
* ArangoDB PHP client: View class
5+
*
6+
* @package ArangoDBClient
7+
* @author Jan Steemann
8+
* @copyright Copyright 2018, ArangoDB GmbH, Cologne, Germany
9+
*
10+
* @since 3.4
11+
*/
12+
13+
namespace ArangoDBClient;
14+
15+
/**
16+
* Value object representing a view
17+
*
18+
* <br>
19+
*
20+
* @package ArangoDBClient
21+
* @since 3.4
22+
*/
23+
class View
24+
{
25+
/**
26+
* The view id (might be NULL for new views)
27+
*
28+
* @var string - view id
29+
*/
30+
protected $_id;
31+
32+
/**
33+
* The view name
34+
*
35+
* @var string - view name
36+
*/
37+
protected $_name;
38+
39+
/**
40+
* View id index
41+
*/
42+
const ENTRY_ID = 'id';
43+
44+
/**
45+
* View name index
46+
*/
47+
const ENTRY_NAME = 'name';
48+
49+
/**
50+
* View type index
51+
*/
52+
const ENTRY_TYPE = 'type';
53+
54+
/**
55+
* Constructs an empty view
56+
*
57+
* @param array $name - name for view
58+
* @param string $type - view type
59+
*
60+
* @since 3.4
61+
*
62+
* @throws \ArangoDBClient\ClientException
63+
*/
64+
public function __construct($name, $type)
65+
{
66+
$this->_name = $name;
67+
$this->_type = $type;
68+
}
69+
70+
/**
71+
* Return the view id
72+
*
73+
* @return string - view id
74+
*/
75+
public function getId()
76+
{
77+
return $this->_id;
78+
}
79+
80+
/**
81+
* Set the view's id
82+
*
83+
* @param string - view id
84+
*
85+
* @return void
86+
*/
87+
public function setId($id)
88+
{
89+
$this->_id = $id;
90+
}
91+
92+
/**
93+
* Return the view name
94+
*
95+
* @return string - view name
96+
*/
97+
public function getName()
98+
{
99+
return $this->_name;
100+
}
101+
102+
/**
103+
* Return the view type
104+
*
105+
* @return string - view type
106+
*/
107+
public function getType()
108+
{
109+
return $this->_type;
110+
}
111+
112+
/**
113+
* Return the view as an array
114+
*
115+
* @return array - view data as an array
116+
*/
117+
public function getAll()
118+
{
119+
return [
120+
self::ENTRY_ID => $this->getId(),
121+
self::ENTRY_NAME => $this->getName(),
122+
self::ENTRY_TYPE => $this->getType(),
123+
];
124+
}
125+
}
126+
127+
class_alias(View::class, '\triagens\ArangoDb\View');

lib/ArangoDBClient/ViewHandler.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
/**
4+
* ArangoDB PHP client: view handler
5+
*
6+
* @package ArangoDBClient
7+
* @author Jan Steemann
8+
* @copyright Copyright 2019, ArangoDB GmbH, Cologne, Germany
9+
*
10+
* @since 3.4
11+
*/
12+
13+
namespace ArangoDBClient;
14+
15+
/**
16+
* A handler that manages views.
17+
*
18+
* @package ArangoDBClient
19+
* @since 3.4
20+
*/
21+
class ViewHandler extends Handler
22+
{
23+
/**
24+
* Create a view
25+
*
26+
* This will create a view using the given view object and return an array of the created view object's attributes.<br><br>
27+
*
28+
* @throws Exception
29+
*
30+
* @param View $view - The view object which holds the information of the view to be created
31+
*
32+
* @return array
33+
* @since 3.4
34+
*/
35+
public function createView(View $view)
36+
{
37+
$params = [
38+
View::ENTRY_NAME => $view->getName(),
39+
View::ENTRY_TYPE => $view->getType(),
40+
];
41+
$url = Urls::URL_VIEW;
42+
$response = $this->getConnection()->post($url, $this->json_encode_wrapper($params));
43+
$json = $response->getJson();
44+
$view->setId($json[View::ENTRY_ID]);
45+
46+
return $view->getAll();
47+
}
48+
49+
/**
50+
* Get a view
51+
*
52+
* This will get a view.<br><br>
53+
*
54+
* @param String $view - The name of the view
55+
*
56+
* @return View|false
57+
* @throws \ArangoDBClient\ClientException
58+
* @since 3.4
59+
*/
60+
public function getView($view)
61+
{
62+
$url = UrlHelper::buildUrl(Urls::URL_VIEW, [$view]);
63+
64+
$response = $this->getConnection()->get($url);
65+
$data = $response->getJson();
66+
67+
$result = new View($data[View::ENTRY_NAME], $data[View::ENTRY_TYPE]);
68+
$result->setId($data[View::ENTRY_ID]);
69+
70+
return $result;
71+
}
72+
73+
/**
74+
* Get a view's properties<br><br>
75+
*
76+
* @throws Exception
77+
*
78+
* @param mixed $view - view name as a string or instance of View
79+
*
80+
* @return array - Returns an array of attributes. Will throw if there is an error
81+
* @since 3.4
82+
*/
83+
public function properties($view)
84+
{
85+
if ($view instanceof View) {
86+
$view = $view->getName();
87+
}
88+
89+
$url = UrlHelper::buildUrl(Urls::URL_VIEW, [$view, 'properties']);
90+
91+
$result = $this->getConnection()->get($url);
92+
93+
return $result->getJson();
94+
}
95+
96+
/**
97+
* Set a view's properties<br><br>
98+
*
99+
* @throws Exception
100+
*
101+
* @param mixed $view - view name as a string or instance of View
102+
* @param array $properties - array with view properties
103+
*
104+
* @return array - Returns an array of attributes. Will throw if there is an error
105+
* @since 3.4
106+
*/
107+
public function setProperties($view, array $properties)
108+
{
109+
if ($view instanceof View) {
110+
$view = $view->getName();
111+
}
112+
113+
$url = UrlHelper::buildUrl(Urls::URL_VIEW, [$view, 'properties']);
114+
$response = $this->getConnection()->put($url, $this->json_encode_wrapper($properties));
115+
$json = $response->getJson();
116+
117+
return $json;
118+
}
119+
120+
121+
/**
122+
* Drop a view<br><br>
123+
*
124+
* @throws Exception
125+
*
126+
* @param mixed $view - view name as a string or instance of View
127+
*
128+
* @return bool - always true, will throw if there is an error
129+
* @since 3.4
130+
*/
131+
public function dropView($view)
132+
{
133+
if ($view instanceof View) {
134+
$view = $view->getName();
135+
}
136+
137+
$url = UrlHelper::buildUrl(Urls::URL_VIEW, [$view]);
138+
$this->getConnection()->delete($url);
139+
140+
return true;
141+
}
142+
}
143+
144+
class_alias(ViewHandler::class, '\triagens\ArangoDb\ViewHandler');

tests/DocumentBasicTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function testCreateAndDeleteDocumentWithoutCreatedCollection()
181181
try {
182182
$this->collectionHandler->drop('ArangoDB_PHP_TestSuite_TestCollection_01' . '_' . static::$testsTimestamp);
183183
} catch (\Exception $e) {
184-
#don't bother us, if it's already deleted.
184+
// don't bother us, if it's already deleted.
185185
}
186186

187187
$document->someAttribute = 'someValue';

tests/QueryCacheTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ public function testClear()
109109
*/
110110
public function testGetEntries()
111111
{
112+
if (isCluster($this->connection)) {
113+
// don't execute this test in a cluster
114+
$this->markTestSkipped("test is only meaningful in single server");
115+
return;
116+
}
112117
$this->setupCollection();
113118

114119
$this->cacheHandler->enable();

0 commit comments

Comments
 (0)