LaravelFreelancerNL /
laravel-arangodb
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LaravelFreelancerNL\Aranguent\Schema; |
||
| 4 | |||
| 5 | use Closure; |
||
| 6 | use Illuminate\Support\Fluent; |
||
| 7 | use LaravelFreelancerNL\Aranguent\Connection; |
||
| 8 | |||
| 9 | class Builder |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The database connection instance. |
||
| 13 | * |
||
| 14 | * @var \Illuminate\Database\Connection |
||
| 15 | */ |
||
| 16 | protected $connection; |
||
| 17 | |||
| 18 | protected $collectionHandler; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The schema grammar instance. |
||
| 22 | * |
||
| 23 | * @var Grammar |
||
| 24 | */ |
||
| 25 | protected $grammar; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The Blueprint resolver callback. |
||
| 29 | * |
||
| 30 | * @var \Closure |
||
| 31 | */ |
||
| 32 | protected $resolver; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Create a new database Schema manager. |
||
| 36 | * |
||
| 37 | * Builder constructor. |
||
| 38 | * @param Connection $connection |
||
| 39 | */ |
||
| 40 | public function __construct(Connection $connection) |
||
| 41 | { |
||
| 42 | $this->connection = $connection; |
||
| 43 | |||
| 44 | $this->grammar = $connection->getSchemaGrammar(); |
||
| 45 | |||
| 46 | $this->collectionHandler = $connection->getCollectionHandler(); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Create a new collection on the schema. |
||
| 51 | * |
||
| 52 | * @param string $collection |
||
| 53 | * @param \Closure $callback |
||
| 54 | * @param array $config |
||
| 55 | * @return void |
||
| 56 | */ |
||
| 57 | public function create($collection, Closure $callback, $config = []) |
||
| 58 | { |
||
| 59 | $this->build(tap($this->createBlueprint($collection), function ($blueprint) use ($callback, $config) { |
||
| 60 | $blueprint->create($config); |
||
| 61 | |||
| 62 | $callback($blueprint); |
||
| 63 | })); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Create a new command set with a Closure. |
||
| 68 | * |
||
| 69 | * @param string $collection |
||
| 70 | * @param \Closure|null $callback |
||
| 71 | * @return \LaravelFreelancerNL\Aranguent\Schema\Blueprint |
||
| 72 | */ |
||
| 73 | protected function createBlueprint($collection, Closure $callback = null) |
||
| 74 | { |
||
| 75 | //Prefixes are unnamed in ArangoDB |
||
| 76 | $prefix = null; |
||
| 77 | |||
| 78 | if (isset($this->resolver)) { |
||
| 79 | return call_user_func($this->resolver, $collection, $callback, $prefix); |
||
| 80 | } |
||
| 81 | |||
| 82 | return new Blueprint($collection, $this->collectionHandler, $callback, $prefix); |
||
| 83 | } |
||
| 84 | |||
| 85 | protected function build(Blueprint $blueprint) |
||
| 86 | { |
||
| 87 | $blueprint->build($this->connection, $this->grammar); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Set the Schema Blueprint resolver callback. |
||
| 92 | * |
||
| 93 | * @param \Closure $resolver |
||
| 94 | * @return void |
||
| 95 | */ |
||
| 96 | public function blueprintResolver(Closure $resolver) |
||
| 97 | { |
||
| 98 | $this->resolver = $resolver; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Modify a collection's schema. |
||
| 103 | * |
||
| 104 | * @param string $collection |
||
| 105 | * @param \Closure $callback |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | public function collection($collection, Closure $callback) |
||
| 109 | { |
||
| 110 | $this->build($this->createBlueprint($collection, $callback)); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Alias for collection. |
||
| 115 | * |
||
| 116 | * @param string $table |
||
| 117 | * @param \Closure $callback |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | public function table($table, Closure $callback) |
||
| 121 | { |
||
| 122 | $this->collection($table, $callback); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Drop a collection from the schema. |
||
| 127 | * |
||
| 128 | * @param string $collection |
||
| 129 | * @return void |
||
| 130 | * @throws \ArangoDBClient\Exception |
||
| 131 | */ |
||
| 132 | public function drop($collection) |
||
| 133 | { |
||
| 134 | $this->collectionHandler->drop($collection); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function dropAllCollections() |
||
| 138 | { |
||
| 139 | $collections = $this->getAllCollections(); |
||
| 140 | |||
| 141 | foreach ($collections as $key => $name) { |
||
| 142 | $this->collectionHandler->drop($name); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Alias for dropAllCollections. |
||
| 148 | * |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | public function dropAllTables() |
||
| 152 | { |
||
| 153 | $this->dropAllCollections(); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param $collection |
||
| 158 | * @throws \ArangoDBClient\Exception |
||
| 159 | */ |
||
| 160 | public function dropIfExists($collection) |
||
| 161 | { |
||
| 162 | if ($collections = $this->hasCollection($collection)) { |
||
| 163 | $this->drop($collection); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get all of the collection names for the database. |
||
| 169 | * |
||
| 170 | * @param array $options |
||
| 171 | * @return array |
||
| 172 | * @throws \ArangoDBClient\ClientException |
||
| 173 | * @throws \ArangoDBClient\Exception |
||
| 174 | */ |
||
| 175 | protected function getAllCollections(array $options = []) |
||
| 176 | { |
||
| 177 | if (! isset($options['excludeSystem'])) { |
||
| 178 | $options['excludeSystem'] = true; |
||
| 179 | } |
||
| 180 | |||
| 181 | return $this->collectionHandler->getAllCollections($options); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get all of the table names for the database. |
||
| 186 | * Alias for getAllCollections(). |
||
| 187 | * |
||
| 188 | * @param array $options |
||
| 189 | * @return array |
||
| 190 | * @throws \ArangoDBClient\ClientException |
||
| 191 | * @throws \ArangoDBClient\Exception |
||
| 192 | */ |
||
| 193 | protected function getAllTables(array $options = []) |
||
| 194 | { |
||
| 195 | return $this->getAllCollections($options); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $collection |
||
| 200 | * @return \ArangoDBClient\Collection |
||
| 201 | * @throws \ArangoDBClient\Exception |
||
| 202 | */ |
||
| 203 | protected function getCollection($collection) |
||
| 204 | { |
||
| 205 | return $this->collectionHandler->get($collection); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Rename a collection. |
||
| 210 | * |
||
| 211 | * @param $from |
||
| 212 | * @param $to |
||
| 213 | * @return bool |
||
| 214 | * @throws \ArangoDBClient\Exception |
||
| 215 | */ |
||
| 216 | public function rename($from, $to) |
||
| 217 | { |
||
| 218 | return $this->collectionHandler->rename($from, $to); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Determine if the given collection exists. |
||
| 223 | * |
||
| 224 | * @param string $collection |
||
| 225 | * @return bool |
||
| 226 | * @throws \ArangoDBClient\Exception |
||
| 227 | */ |
||
| 228 | public function hasCollection(string $collection) |
||
| 229 | { |
||
| 230 | return $this->collectionHandler->has($collection); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Alias for hasCollection. |
||
| 235 | * |
||
| 236 | * @param string $table |
||
| 237 | * @return bool |
||
| 238 | * @throws \ArangoDBClient\Exception |
||
| 239 | */ |
||
| 240 | public function hasTable($table) |
||
| 241 | { |
||
| 242 | return $this->hasCollection($table); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Check if any document in the collection has the attribute. |
||
| 247 | * |
||
| 248 | * @param string $collection |
||
| 249 | * @param string $attribute |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | public function hasAttribute($collection, $attribute) |
||
| 253 | { |
||
| 254 | if (is_string($attribute)) { |
||
| 255 | $attribute = [$attribute]; |
||
| 256 | } |
||
| 257 | |||
| 258 | return $this->hasAttributes($collection, $attribute); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Check if any document in the collection has the attribute. |
||
| 263 | * |
||
| 264 | * @param string $collection |
||
| 265 | * @param array $attributes |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | public function hasAttributes($collection, $attributes) |
||
| 269 | { |
||
| 270 | $parameters['name'] = 'hasAttribute'; |
||
|
0 ignored issues
–
show
|
|||
| 271 | $parameters['handler'] = 'aql'; |
||
| 272 | $parameters['attribute'] = $attributes; |
||
| 273 | |||
| 274 | $command = new Fluent($parameters); |
||
| 275 | $compilation = $this->grammar->compileHasAttribute($collection, $command); |
||
| 276 | |||
| 277 | return $this->connection->statement($compilation['aql']); |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Alias for hasAttribute. |
||
| 282 | * |
||
| 283 | * @param string $table |
||
| 284 | * @param string $column |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | public function hasColumn($table, $column) |
||
| 288 | { |
||
| 289 | return $this->hasAttribute($table, $column); |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Alias for hasAttributes. |
||
| 294 | * |
||
| 295 | * @param string $table |
||
| 296 | * @param array $columns |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | public function hasColumns($table, $columns) |
||
| 300 | { |
||
| 301 | return $this->hasAttributes($table, $columns); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * get information about the collection. |
||
| 306 | * |
||
| 307 | * @param $collection |
||
| 308 | * @return mixed |
||
| 309 | */ |
||
| 310 | public function getCollectionInfo($collection) |
||
| 311 | { |
||
| 312 | return $this->figures($collection); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get the database connection instance. |
||
| 317 | * |
||
| 318 | * @return Connection |
||
| 319 | */ |
||
| 320 | public function getConnection() |
||
| 321 | { |
||
| 322 | return $this->connection; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Silently catch the use of unsupported builder methods. |
||
| 327 | * |
||
| 328 | * @param $method |
||
| 329 | * @param $args |
||
| 330 | * @return null |
||
| 331 | */ |
||
| 332 | public function __call($method, $args) |
||
| 333 | { |
||
| 334 | error_log("The Aranguent Schema Builder doesn't support method '$method'\n"); |
||
| 335 | } |
||
| 336 | } |
||
| 337 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.