Skip to content

Commit 7cbddbc

Browse files
Improved validation for collection key options (#15)
* improve Collection 'keyOptions' attribute validation * refactor 'KeyType' class * fix exception attribute type * drop support for PHP 8.2
1 parent e82b6ce commit 7cbddbc

File tree

17 files changed

+190
-32
lines changed

17 files changed

+190
-32
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
strategy:
2525
fail-fast: false
2626
matrix:
27-
php-versions: ["8.2", "8.3", "8.4"]
27+
php-versions: ["8.3", "8.4"]
2828

2929
steps:
3030
- name: Setup PHP

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
}
2020
],
2121
"require": {
22-
"php": ">=8.2",
22+
"php": ">=8.3",
2323
"ext-json": "*",
2424
"guzzlehttp/guzzle": "^7.10",
2525
"symfony/service-contracts": "^3.6",

composer.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Collection/Collection.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ class Collection implements JsonSerializable
136136
'type' => 2,
137137
'keyOptions' => [
138138
'allowUserKeys' => true,
139-
'type' => 'traditional',
140-
'lastValue' => 0
139+
'type' => KeyType::TRADITIONAL,
141140
],
142141
];
143142

@@ -214,7 +213,7 @@ public function __set(string $name, $value)
214213
* @return CursorInterface|bool Cursor if collection exists on database. False otherwise.
215214
* @throws GuzzleException|InvalidParameterException|CursorException
216215
*/
217-
public function all()
216+
public function all(): bool|CollectionCursor
218217
{
219218
if (!$this->isNew()) {
220219
return new CollectionCursor($this);
@@ -272,7 +271,7 @@ public function getName(): string
272271
*
273272
* @return string|null String if collection exists on database. Null if not.
274273
*/
275-
public function getId()
274+
public function getId(): ?string
276275
{
277276
return ($this->attributes['objectId'] === null) ? $this->attributes['id'] : $this->attributes['objectId'];
278277
}

src/Collection/KeyType.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace ArangoDB\Collection;
4+
5+
/**
6+
* Groups the available key generations for new collections
7+
*
8+
* @package ArangoDB\Collection
9+
* @author Lucas S. Vieira
10+
*/
11+
class KeyType
12+
{
13+
public const string TRADITIONAL = 'traditional';
14+
public const string AUTOINCREMENT = 'autoincrement';
15+
public const string PADDED = 'padded';
16+
public const string UUID = 'uuid';
17+
}

src/Cursor/CollectionCursor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CollectionCursor extends Cursor
2525
*
2626
* @var Collection
2727
*/
28-
protected $collection;
28+
protected Collection $collection;
2929

3030
/**
3131
* CollectionCursor constructor.

src/DataStructures/ArrayList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function last()
7171
/**
7272
* Get a value by its key
7373
*
74-
* @param int|string $key Key to verify on list.
74+
* @param int|string $key KeyType to verify on list.
7575
* @return mixed
7676
*/
7777
public function get($key)
@@ -96,7 +96,7 @@ public function push($value): void
9696
/**
9797
* Put a object into list on given key
9898
*
99-
* @param string|integer $key Key for manage the value.
99+
* @param string|integer $key KeyType for manage the value.
100100
* @param mixed $value Value to add.
101101
*/
102102
public function put($key, $value): void

src/DataStructures/Contracts/ListInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function push($value): void;
4444
/**
4545
* Put a object into list on given key
4646
*
47-
* @param string|integer $key Key for manage the value.
47+
* @param string|integer $key KeyType for manage the value.
4848
* @param mixed $value Value to add.
4949
*/
5050
public function put($key, $value): void;

src/Validation/Admin/Task/TaskValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function rules(): array
5959
*
6060
* @return \Closure
6161
*/
62-
private function validateParamsCallback()
62+
private function validateParamsCallback(): \Closure
6363
{
6464
return function (array $params) {
6565
$validator = Rules::isPrimitive();

src/Validation/Collection/CollectionValidator.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace ArangoDB\Validation\Collection;
66

7+
use ArangoDB\Collection\KeyType;
78
use ArangoDB\Validation\Validator;
89
use ArangoDB\Validation\Rules\Rules;
10+
use ArangoDB\Validation\Exceptions\InvalidKeyOptionException;
911

1012
/**
1113
* Validate the collection options values. <br>
@@ -48,7 +50,33 @@ public function rules(): array
4850
'numberOfShards' => Rules::equalsOrGreaterThan(1),
4951
'isSystem' => Rules::boolean(),
5052
'type' => Rules::in([2, 3]),
51-
'keyOptions' => Rules::arr(),
53+
'keyOptions' => Rules::callbackValidation(self::validateKeyOptions()),
5254
];
5355
}
56+
57+
/**
58+
* Validate key options for collection creation
59+
*
60+
* @return \Closure
61+
*/
62+
private static function validateKeyOptions(): \Closure
63+
{
64+
/**
65+
* 'offset' and 'increment' options are only allowed when used with type 'autoincrement'
66+
*
67+
* @return bool
68+
* @throws InvalidKeyOptionException
69+
*/
70+
return function (array $keyOptions) {
71+
if (array_key_exists('offset', $keyOptions) && $keyOptions['type'] != KeyType::AUTOINCREMENT) {
72+
throw new InvalidKeyOptionException("offset", $keyOptions['type']);
73+
}
74+
75+
if (array_key_exists('increment', $keyOptions) && $keyOptions['type'] != KeyType::AUTOINCREMENT) {
76+
throw new InvalidKeyOptionException("increment", $keyOptions['type']);
77+
}
78+
79+
return true;
80+
};
81+
}
5482
}

0 commit comments

Comments
 (0)