Skip to content

Commit 16e3f28

Browse files
author
codeliner
committed
Add handling of named indices
1 parent f486e12 commit 16e3f28

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

src/DocumentStore.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function hasCollection(string $collectionName): bool;
3838
/**
3939
* @param string $collectionName
4040
* @param Index[] ...$indices
41+
* @throws RuntimeException if adding did not succeed
4142
*/
4243
public function addCollection(string $collectionName, Index ...$indices): void;
4344

@@ -47,6 +48,22 @@ public function addCollection(string $collectionName, Index ...$indices): void;
4748
*/
4849
public function dropCollection(string $collectionName): void;
4950

51+
public function hasCollectionIndex(string $collectionName, string $indexName): bool;
52+
53+
/**
54+
* @param string $collectionName
55+
* @param Index $index
56+
* @throws RuntimeException if adding did not succeed
57+
*/
58+
public function addCollectionIndex(string $collectionName, Index $index): void;
59+
60+
/**
61+
* @param string $collectionName
62+
* @param string $indexName
63+
* @throws RuntimeException if dropping did not succeed
64+
*/
65+
public function dropCollectionIndex(string $collectionName, string $indexName): void;
66+
5067
/**
5168
* @param string $collectionName
5269
* @param string $docId

src/FieldIndex.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ final class FieldIndex implements Index
2828
*/
2929
private $sort;
3030

31+
/**
32+
* @var string|null
33+
*/
34+
private $name;
35+
3136
public static function forFieldInMultiFieldIndex(string $field): self
3237
{
3338
return self::forField($field);
@@ -38,19 +43,26 @@ public static function forField(string $field, int $sort = self::SORT_ASC, bool
3843
return new self($field, $sort, $unique);
3944
}
4045

46+
public static function namedIndexForField(string $idxName, string $field, int $sort = self::SORT_ASC, bool $unique = false): self
47+
{
48+
return new self($field, $sort, $unique, $idxName);
49+
}
50+
4151
public static function fromArray(array $data): Index
4252
{
4353
return new self(
4454
$data['field'] ?? '',
4555
$data['sort'] ?? self::SORT_ASC,
46-
$data['unique'] ?? false
56+
$data['unique'] ?? false,
57+
$data['name'] ?? null
4758
);
4859
}
4960

5061
private function __construct(
5162
string $field,
5263
int $sort,
53-
bool $unique
64+
bool $unique,
65+
string $name = null
5466
) {
5567
if (\mb_strlen($field) === 0) {
5668
throw new \InvalidArgumentException('Field must not be empty');
@@ -63,6 +75,7 @@ private function __construct(
6375
$this->field = $field;
6476
$this->sort = $sort;
6577
$this->unique = $unique;
78+
$this->name = $name;
6679
}
6780

6881
/**
@@ -89,12 +102,18 @@ public function unique(): bool
89102
return $this->unique;
90103
}
91104

105+
public function name(): ?string
106+
{
107+
return $this->name;
108+
}
109+
92110
public function toArray(): array
93111
{
94112
return [
95113
'field' => $this->field,
96114
'sort' => $this->sort,
97115
'unique' => $this->unique,
116+
'name' => $this->name,
98117
];
99118
}
100119

src/InMemoryDocumentStore.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,32 @@ public function dropCollection(string $collectionName): void
8181
}
8282
}
8383

84+
public function hasCollectionIndex(string $collectionName, string $indexName): bool
85+
{
86+
//InMemoryDocumentStore ignores indices
87+
return false;
88+
}
89+
90+
/**
91+
* @param string $collectionName
92+
* @param Index $index
93+
* @throws RuntimeException if adding did not succeed
94+
*/
95+
public function addCollectionIndex(string $collectionName, Index $index): void
96+
{
97+
//InMemoryDocumentStore ignores indices
98+
}
99+
100+
/**
101+
* @param string $collectionName
102+
* @param string $indexName
103+
* @throws RuntimeException if dropping did not succeed
104+
*/
105+
public function dropCollectionIndex(string $collectionName, string $indexName): void
106+
{
107+
//InMemoryDocumentStore ignores indices
108+
}
109+
84110
/**
85111
* @param string $collectionName
86112
* @param string $docId

src/MultiFieldIndex.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ final class MultiFieldIndex implements Index
2323
*/
2424
private $unique;
2525

26+
/**
27+
* @var string|null
28+
*/
29+
private $name;
30+
2631
public static function forFields(array $fieldNames, bool $unique = false): self
2732
{
2833
return self::fromArray([
@@ -31,6 +36,15 @@ public static function forFields(array $fieldNames, bool $unique = false): self
3136
]);
3237
}
3338

39+
public static function namedIndexForFields(string $idxName, array $fieldNames, bool $unique = false): self
40+
{
41+
return self::fromArray([
42+
'name' => $idxName,
43+
'fields' => $fieldNames,
44+
'unique' => $unique,
45+
]);
46+
}
47+
3448
public static function fromArray(array $data): Index
3549
{
3650
$fields = \array_map(function (string $field): FieldIndex {
@@ -39,18 +53,20 @@ public static function fromArray(array $data): Index
3953

4054
return new self(
4155
$data['unique'] ?? false,
56+
$data['name'] ?? null,
4257
...$fields
4358
);
4459
}
4560

46-
private function __construct(bool $unique, FieldIndex ...$fields)
61+
private function __construct(bool $unique,string $name = null, FieldIndex ...$fields)
4762
{
4863
if (\count($fields) <= 1) {
4964
throw new \InvalidArgumentException('MultiFieldIndex should contain at least two fields');
5065
}
5166

5267
$this->fields = $fields;
5368
$this->unique = $unique;
69+
$this->name = $name;
5470
}
5571

5672
/**
@@ -69,13 +85,19 @@ public function unique(): bool
6985
return $this->unique;
7086
}
7187

88+
public function name(): ?string
89+
{
90+
return $this->name;
91+
}
92+
7293
public function toArray(): array
7394
{
7495
return [
7596
'fields' => \array_map(function (FieldIndex $field): string {
7697
return $field->field();
7798
}, $this->fields),
7899
'unique' => $this->unique,
100+
'name' => $this->name,
79101
];
80102
}
81103

0 commit comments

Comments
 (0)