Skip to content

Commit 8c86eee

Browse files
committed
Merge pull request #143 from Beldur/devel
Added 'all' Method which corresponds to /simple/all
2 parents 524c805 + b81c9e3 commit 8c86eee

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

lib/triagens/ArangoDb/CollectionHandler.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,52 @@ public function range($collectionId, $attribute, $left, $right, $options = array
12111211
return new Cursor($this->getConnection(), $response->getJson(), $options);
12121212
}
12131213

1214+
/**
1215+
* Returns all documents of a collection
1216+
*
1217+
* @param mixed $collectionId - collection id as string or number
1218+
* @param array $options - optional array of options.
1219+
* <p>Options are :<br>
1220+
* <li>'_sanitize' - True to remove _id and _rev attributes from result documents. Defaults to false.</li>
1221+
* <li>'sanitize' - Deprecated, please use '_sanitize'.</li>
1222+
* <li>'_hiddenAttributes' - Set an array of hidden attributes for created documents.
1223+
* <li>'hiddenAttributes' - Deprecated, please use '_hiddenAttributes'.</li>
1224+
* <p>
1225+
* This is actually the same as setting hidden attributes using setHiddenAttributes() on a document.<br>
1226+
* The difference is, that if you're returning a resultset of documents, the getAll() is already called<br>
1227+
* and the hidden attributes would not be applied to the attributes.<br>
1228+
* </p>
1229+
*
1230+
* <li>'batchSize' - can optionally be used to tell the server to limit the number of results to be transferred in one batch</li>
1231+
* <li>'skip' - Optional, The number of documents to skip in the query.</li>
1232+
* <li>'limit' - Optional, The maximal amount of documents to return. 'skip' is applied before the limit restriction.</li>
1233+
* </li>
1234+
* </p>
1235+
*
1236+
* @return Cursor - documents
1237+
*/
1238+
public function all($collectionId, $options = array())
1239+
{
1240+
$options = array_merge($options, $this->getCursorOptions($options));
1241+
1242+
$body = array(
1243+
self::OPTION_COLLECTION => $collectionId,
1244+
);
1245+
1246+
$body = $this->includeOptionsInBody(
1247+
$options,
1248+
$body,
1249+
array(
1250+
self::OPTION_LIMIT => null,
1251+
self::OPTION_SKIP => null,
1252+
)
1253+
);
1254+
1255+
$response = $this->getConnection()->put(Urls::URL_ALL, $this->json_encode_wrapper($body));
1256+
1257+
return new Cursor($this->getConnection(), $response->getJson(), $options);
1258+
}
1259+
12141260

12151261
/**
12161262
* Get document(s) by specifying near

lib/triagens/ArangoDb/Urls.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ abstract class Urls
117117
*/
118118
const URL_RANGE = '/_api/simple/range';
119119

120+
/**
121+
* base URL part for select-all
122+
*/
123+
const URL_ALL = '/_api/simple/all';
124+
120125
/**
121126
* base URL part for select-range
122127
*/

tests/CollectionExtendedTest.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,116 @@ public function testCreateGetAllIdsAndDeleteCollectionThroughCreateFromArray()
10931093
}
10941094

10951095

1096+
/**
1097+
* test for creation, all, and delete of a collection
1098+
*/
1099+
public function testCreateAndAllAndDeleteCollection()
1100+
{
1101+
$collectionHandler = $this->collectionHandler;
1102+
1103+
$collection = Collection::createFromArray(array('name' => 'ArangoDB_PHP_TestSuite_TestCollection_01'));
1104+
$collectionHandler->add($collection);
1105+
1106+
$documentHandler = $this->documentHandler;
1107+
1108+
$document = Document::createFromArray(
1109+
array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue')
1110+
);
1111+
$documentHandler->add($collection->getId(), $document);
1112+
1113+
$document = Document::createFromArray(
1114+
array('someAttribute' => 'someValue2', 'someOtherAttribute' => 'someOtherValue2')
1115+
);
1116+
$documentHandler->add($collection->getId(), $document);
1117+
1118+
$cursor = $collectionHandler->all($collection->getId());
1119+
1120+
$resultingDocument = null;
1121+
1122+
foreach ($cursor as $key => $value) {
1123+
$resultingDocument[$key] = $value;
1124+
}
1125+
1126+
$this->assertTrue(count($resultingDocument) == 2, 'Should be 2, was: ' . count($resultingDocument));
1127+
1128+
$response = $collectionHandler->delete($collection);
1129+
$this->assertTrue($response, 'Delete should return true!');
1130+
}
1131+
1132+
1133+
/**
1134+
* test for creation, all with limit, and delete of a collection
1135+
*/
1136+
public function testCreateAndAllWithLimitAndDeleteCollection()
1137+
{
1138+
$collectionHandler = $this->collectionHandler;
1139+
1140+
$collection = Collection::createFromArray(array('name' => 'ArangoDB_PHP_TestSuite_TestCollection_01'));
1141+
$collectionHandler->add($collection);
1142+
1143+
$documentHandler = $this->documentHandler;
1144+
1145+
$document = Document::createFromArray(
1146+
array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue')
1147+
);
1148+
$documentHandler->add($collection->getId(), $document);
1149+
1150+
$document = Document::createFromArray(
1151+
array('someAttribute' => 'someValue2', 'someOtherAttribute' => 'someOtherValue2')
1152+
);
1153+
$documentHandler->add($collection->getId(), $document);
1154+
1155+
$cursor = $collectionHandler->all($collection->getId(), array( 'limit' => 1 ));
1156+
1157+
$resultingDocument = null;
1158+
1159+
foreach ($cursor as $key => $value) {
1160+
$resultingDocument[$key] = $value;
1161+
}
1162+
1163+
// 2 Documents limited to 1, the result should be 1
1164+
$this->assertTrue(count($resultingDocument) == 1, 'Should be 1, was: ' . count($resultingDocument));
1165+
1166+
$response = $collectionHandler->delete($collection);
1167+
$this->assertTrue($response, 'Delete should return true!');
1168+
}
1169+
1170+
1171+
/**
1172+
* test for creation, all with skip, and delete of a collection
1173+
*/
1174+
public function testCreateAndAllWithSkipAndDeleteCollection()
1175+
{
1176+
$collectionHandler = $this->collectionHandler;
1177+
1178+
$collection = Collection::createFromArray(array('name' => 'ArangoDB_PHP_TestSuite_TestCollection_01'));
1179+
$collectionHandler->add($collection);
1180+
1181+
$documentHandler = $this->documentHandler;
1182+
1183+
for ($i = 0; $i < 3; $i++) {
1184+
$document = Document::createFromArray(
1185+
array('someAttribute' => 'someValue ' . $i, 'someOtherAttribute' => 'someValue ' . $i)
1186+
);
1187+
$documentHandler->add($collection->getId(), $document);
1188+
}
1189+
1190+
$cursor = $collectionHandler->all($collection->getId(), array( 'skip' => 1 ));
1191+
1192+
$resultingDocument = null;
1193+
1194+
foreach ($cursor as $key => $value) {
1195+
$resultingDocument[$key] = $value;
1196+
}
1197+
1198+
// With 3 Documents and skipping 1, the result should be 2
1199+
$this->assertTrue(count($resultingDocument) == 2, 'Should be 2, was: ' . count($resultingDocument));
1200+
1201+
$response = $collectionHandler->delete($collection);
1202+
$this->assertTrue($response, 'Delete should return true!');
1203+
}
1204+
1205+
10961206
/**
10971207
* test for creating, filling with documents and truncating the collection.
10981208
*/

0 commit comments

Comments
 (0)