|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ArangoClient\Schema; |
| 4 | + |
| 5 | +use ArangoClient\Connector; |
| 6 | +use ArangoClient\Exceptions\ArangoException; |
| 7 | +use GuzzleHttp\Exception\GuzzleException; |
| 8 | + |
| 9 | +/* |
| 10 | + * @see https://www.arangodb.com/docs/stable/http/collection.html |
| 11 | + */ |
| 12 | +trait ManagesCollections |
| 13 | +{ |
| 14 | + protected Connector $connector; |
| 15 | + |
| 16 | + /** |
| 17 | + * |
| 18 | + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
| 19 | + * |
| 20 | + * @param bool $excludeSystemCollections |
| 21 | + * @return array<mixed> |
| 22 | + * @throws ArangoException |
| 23 | + * @throws GuzzleException |
| 24 | + */ |
| 25 | + public function getCollections(bool $excludeSystemCollections = false): array |
| 26 | + { |
| 27 | + return (array) $this->connector->request( |
| 28 | + 'get', |
| 29 | + '/_api/collection', |
| 30 | + [ |
| 31 | + 'query' => [ |
| 32 | + 'excludeSystem' => $excludeSystemCollections |
| 33 | + ] |
| 34 | + ] |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + /** |
| 40 | + * Check for collection existence in current DB. |
| 41 | + * |
| 42 | + * @param string $collection |
| 43 | + * @return bool |
| 44 | + * @throws ArangoException |
| 45 | + * @throws GuzzleException |
| 46 | + */ |
| 47 | + public function hasCollection(string $collection): bool |
| 48 | + { |
| 49 | + $collections = $this->getCollections(); |
| 50 | + return array_search($collection, array_column($collections, 'name'), true) !== false; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-information-about-a-collection |
| 55 | + * |
| 56 | + * @param string $collection |
| 57 | + * @return array<mixed> |
| 58 | + * @throws ArangoException |
| 59 | + * @throws GuzzleException |
| 60 | + */ |
| 61 | + public function getCollection(string $collection): array |
| 62 | + { |
| 63 | + $uri = '/_api/collection/' . $collection; |
| 64 | + return (array) $this->connector->request('get', $uri); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @see https://www.arangodb.com/docs/stable/http/collection-getting.html#read-properties-of-a-collection |
| 69 | + * |
| 70 | + * @param string $collection |
| 71 | + * @return array<mixed> |
| 72 | + * @throws ArangoException |
| 73 | + * @throws GuzzleException |
| 74 | + */ |
| 75 | + public function getCollectionProperties(string $collection): array |
| 76 | + { |
| 77 | + $uri = '/_api/collection/' . $collection . '/properties'; |
| 78 | + return (array) $this->connector->request('get', $uri); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-number-of-documents-in-a-collection |
| 83 | + * |
| 84 | + * @param string $collection |
| 85 | + * @return array<mixed> |
| 86 | + * @throws ArangoException |
| 87 | + * @throws GuzzleException |
| 88 | + */ |
| 89 | + public function getCollectionDocumentCount(string $collection): array |
| 90 | + { |
| 91 | + $uri = '/_api/collection/' . $collection . '/count'; |
| 92 | + return (array) $this->connector->request('get', $uri); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + /** |
| 97 | + * @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-statistics-for-a-collection |
| 98 | + * |
| 99 | + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
| 100 | + * |
| 101 | + * @param string $collection |
| 102 | + * @param bool $details |
| 103 | + * @return array<mixed> |
| 104 | + * @throws ArangoException |
| 105 | + * @throws GuzzleException |
| 106 | + */ |
| 107 | + public function getCollectionStatistics(string $collection, bool $details = false): array |
| 108 | + { |
| 109 | + $uri = '/_api/collection/' . $collection . '/figures'; |
| 110 | + return (array) $this->connector->request( |
| 111 | + 'get', |
| 112 | + $uri, |
| 113 | + [ |
| 114 | + 'query' => [ |
| 115 | + 'details' => $details |
| 116 | + ] |
| 117 | + ] |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param string $collection |
| 123 | + * @param array<mixed> $config |
| 124 | + * @param int|null $waitForSyncReplication |
| 125 | + * @param int|null $enforceReplicationFactor |
| 126 | + * @return bool |
| 127 | + * @throws ArangoException |
| 128 | + * @throws GuzzleException |
| 129 | + */ |
| 130 | + public function createCollection( |
| 131 | + string $collection, |
| 132 | + array $config = [], |
| 133 | + $waitForSyncReplication = null, |
| 134 | + $enforceReplicationFactor = null |
| 135 | + ): bool { |
| 136 | + $collection = json_encode((object) array_merge($config, ['name' => $collection])); |
| 137 | + |
| 138 | + $options = ['body' => $collection]; |
| 139 | + if (isset($waitForSyncReplication)) { |
| 140 | + $options['query']['waitForSyncReplication'] = $waitForSyncReplication; |
| 141 | + } |
| 142 | + if (isset($enforceReplicationFactor)) { |
| 143 | + $options['query']['enforceReplicationFactor'] = $enforceReplicationFactor; |
| 144 | + } |
| 145 | + |
| 146 | + return (bool) $this->connector->request('post', '/_api/collection', $options); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @param string $name |
| 151 | + * @param array<mixed> $config |
| 152 | + * @return array<mixed> |
| 153 | + * @throws ArangoException |
| 154 | + * @throws GuzzleException |
| 155 | + */ |
| 156 | + public function updateCollection(string $name, array $config = []): array |
| 157 | + { |
| 158 | + $uri = '/_api/collection/' . $name . '/properties'; |
| 159 | + |
| 160 | + $config = json_encode((object) $config); |
| 161 | + $options = ['body' => $config]; |
| 162 | + |
| 163 | + return (array) $this->connector->request('put', $uri, $options); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @param string $old |
| 168 | + * @param string $new |
| 169 | + * @return array<mixed> |
| 170 | + * @throws ArangoException |
| 171 | + * @throws GuzzleException |
| 172 | + */ |
| 173 | + public function renameCollection(string $old, string $new): array |
| 174 | + { |
| 175 | + $uri = '/_api/collection/' . $old . '/rename'; |
| 176 | + |
| 177 | + $newName = json_encode((object) ['name' => $new]); |
| 178 | + $options = ['body' => $newName]; |
| 179 | + |
| 180 | + return (array) $this->connector->request('put', $uri, $options); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * @param string $name |
| 185 | + * @return bool |
| 186 | + * @throws ArangoException |
| 187 | + * @throws GuzzleException |
| 188 | + */ |
| 189 | + public function deleteCollection(string $name): bool |
| 190 | + { |
| 191 | + $uri = '/_api/collection/' . $name; |
| 192 | + |
| 193 | + return (bool) $this->connector->request('delete', $uri); |
| 194 | + } |
| 195 | +} |
0 commit comments