|
4 | 4 |
|
5 | 5 | namespace ArangoDB\Validation\Collection; |
6 | 6 |
|
| 7 | +use ArangoDB\Collection\KeyType; |
7 | 8 | use ArangoDB\Validation\Validator; |
8 | 9 | use ArangoDB\Validation\Rules\Rules; |
| 10 | +use ArangoDB\Validation\Exceptions\InvalidKeyOptionException; |
9 | 11 |
|
10 | 12 | /** |
11 | 13 | * Validate the collection options values. <br> |
@@ -48,7 +50,33 @@ public function rules(): array |
48 | 50 | 'numberOfShards' => Rules::equalsOrGreaterThan(1), |
49 | 51 | 'isSystem' => Rules::boolean(), |
50 | 52 | 'type' => Rules::in([2, 3]), |
51 | | - 'keyOptions' => Rules::arr(), |
| 53 | + 'keyOptions' => Rules::callbackValidation(self::validateKeyOptions()), |
52 | 54 | ]; |
53 | 55 | } |
| 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 | + } |
54 | 82 | } |
0 commit comments