7
7
use Meilisearch \Contracts \DocumentsQuery ;
8
8
use Meilisearch \Contracts \DocumentsResults ;
9
9
use Meilisearch \Exceptions \ApiException ;
10
- use Meilisearch \Exceptions \InvalidArgumentException ;
11
10
use Meilisearch \Exceptions \InvalidResponseBodyException ;
12
11
13
12
trait HandlesDocuments
14
13
{
15
- public function getDocument ($ documentId , ?array $ fields = null )
14
+ public function getDocument (string | int $ documentId , ?array $ fields = null ): array
16
15
{
17
- $ this ->assertValidDocumentId ($ documentId );
18
16
$ query = isset ($ fields ) ? ['fields ' => implode (', ' , $ fields )] : [];
19
17
20
18
return $ this ->http ->get (self ::PATH .'/ ' .$ this ->uid .'/documents/ ' .$ documentId , $ query );
@@ -38,27 +36,27 @@ public function getDocuments(?DocumentsQuery $options = null): DocumentsResults
38
36
}
39
37
}
40
38
41
- public function addDocuments (array $ documents , ?string $ primaryKey = null )
39
+ public function addDocuments (array $ documents , ?string $ primaryKey = null ): array
42
40
{
43
41
return $ this ->http ->post (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey ]);
44
42
}
45
43
46
- public function addDocumentsJson (string $ documents , ?string $ primaryKey = null )
44
+ public function addDocumentsJson (string $ documents , ?string $ primaryKey = null ): array
47
45
{
48
46
return $ this ->http ->post (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey ], 'application/json ' );
49
47
}
50
48
51
- public function addDocumentsCsv (string $ documents , ?string $ primaryKey = null , ?string $ delimiter = null )
49
+ public function addDocumentsCsv (string $ documents , ?string $ primaryKey = null , ?string $ delimiter = null ): array
52
50
{
53
51
return $ this ->http ->post (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey , 'csvDelimiter ' => $ delimiter ], 'text/csv ' );
54
52
}
55
53
56
- public function addDocumentsNdjson (string $ documents , ?string $ primaryKey = null )
54
+ public function addDocumentsNdjson (string $ documents , ?string $ primaryKey = null ): array
57
55
{
58
56
return $ this ->http ->post (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey ], 'application/x-ndjson ' );
59
57
}
60
58
61
- public function addDocumentsInBatches (array $ documents , ?int $ batchSize = 1000 , ?string $ primaryKey = null )
59
+ public function addDocumentsInBatches (array $ documents , ?int $ batchSize = 1000 , ?string $ primaryKey = null ): array
62
60
{
63
61
$ promises = [];
64
62
@@ -69,7 +67,7 @@ public function addDocumentsInBatches(array $documents, ?int $batchSize = 1000,
69
67
return $ promises ;
70
68
}
71
69
72
- public function addDocumentsCsvInBatches (string $ documents , ?int $ batchSize = 1000 , ?string $ primaryKey = null , ?string $ delimiter = null )
70
+ public function addDocumentsCsvInBatches (string $ documents , ?int $ batchSize = 1000 , ?string $ primaryKey = null , ?string $ delimiter = null ): array
73
71
{
74
72
$ promises = [];
75
73
@@ -80,7 +78,7 @@ public function addDocumentsCsvInBatches(string $documents, ?int $batchSize = 10
80
78
return $ promises ;
81
79
}
82
80
83
- public function addDocumentsNdjsonInBatches (string $ documents , ?int $ batchSize = 1000 , ?string $ primaryKey = null )
81
+ public function addDocumentsNdjsonInBatches (string $ documents , ?int $ batchSize = 1000 , ?string $ primaryKey = null ): array
84
82
{
85
83
$ promises = [];
86
84
@@ -91,27 +89,27 @@ public function addDocumentsNdjsonInBatches(string $documents, ?int $batchSize =
91
89
return $ promises ;
92
90
}
93
91
94
- public function updateDocuments (array $ documents , ?string $ primaryKey = null )
92
+ public function updateDocuments (array $ documents , ?string $ primaryKey = null ): array
95
93
{
96
94
return $ this ->http ->put (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey ]);
97
95
}
98
96
99
- public function updateDocumentsJson (string $ documents , ?string $ primaryKey = null )
97
+ public function updateDocumentsJson (string $ documents , ?string $ primaryKey = null ): array
100
98
{
101
99
return $ this ->http ->put (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey ], 'application/json ' );
102
100
}
103
101
104
- public function updateDocumentsCsv (string $ documents , ?string $ primaryKey = null , ?string $ delimiter = null )
102
+ public function updateDocumentsCsv (string $ documents , ?string $ primaryKey = null , ?string $ delimiter = null ): array
105
103
{
106
104
return $ this ->http ->put (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey , 'csvDelimiter ' => $ delimiter ], 'text/csv ' );
107
105
}
108
106
109
- public function updateDocumentsNdjson (string $ documents , ?string $ primaryKey = null )
107
+ public function updateDocumentsNdjson (string $ documents , ?string $ primaryKey = null ): array
110
108
{
111
109
return $ this ->http ->put (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey ], 'application/x-ndjson ' );
112
110
}
113
111
114
- public function updateDocumentsInBatches (array $ documents , ?int $ batchSize = 1000 , ?string $ primaryKey = null )
112
+ public function updateDocumentsInBatches (array $ documents , ?int $ batchSize = 1000 , ?string $ primaryKey = null ): array
115
113
{
116
114
$ promises = [];
117
115
@@ -122,7 +120,7 @@ public function updateDocumentsInBatches(array $documents, ?int $batchSize = 100
122
120
return $ promises ;
123
121
}
124
122
125
- public function updateDocumentsCsvInBatches (string $ documents , ?int $ batchSize = 1000 , ?string $ primaryKey = null , ?string $ delimiter = null )
123
+ public function updateDocumentsCsvInBatches (string $ documents , ?int $ batchSize = 1000 , ?string $ primaryKey = null , ?string $ delimiter = null ): array
126
124
{
127
125
$ promises = [];
128
126
@@ -133,7 +131,7 @@ public function updateDocumentsCsvInBatches(string $documents, ?int $batchSize =
133
131
return $ promises ;
134
132
}
135
133
136
- public function updateDocumentsNdjsonInBatches (string $ documents , ?int $ batchSize = 1000 , ?string $ primaryKey = null )
134
+ public function updateDocumentsNdjsonInBatches (string $ documents , ?int $ batchSize = 1000 , ?string $ primaryKey = null ): array
137
135
{
138
136
$ promises = [];
139
137
@@ -154,7 +152,7 @@ public function updateDocumentsNdjsonInBatches(string $documents, ?int $batchSiz
154
152
* @param non-empty-string $function
155
153
* @param array{filter?: non-empty-string|list<non-empty-string>|null, context?: array<non-empty-string, mixed>} $options
156
154
*/
157
- public function updateDocumentsByFunction (string $ function , array $ options = [])
155
+ public function updateDocumentsByFunction (string $ function , array $ options = []): array
158
156
{
159
157
return $ this ->http ->post (self ::PATH .'/ ' .$ this ->uid .'/documents/edit ' , array_merge (['function ' => $ function ], $ options ));
160
158
}
@@ -164,10 +162,8 @@ public function deleteAllDocuments(): array
164
162
return $ this ->http ->delete (self ::PATH .'/ ' .$ this ->uid .'/documents ' );
165
163
}
166
164
167
- public function deleteDocument ($ documentId ): array
165
+ public function deleteDocument (string | int $ documentId ): array
168
166
{
169
- $ this ->assertValidDocumentId ($ documentId );
170
-
171
167
return $ this ->http ->delete (self ::PATH .'/ ' .$ this ->uid .'/documents/ ' .$ documentId );
172
168
}
173
169
@@ -186,17 +182,6 @@ public function deleteDocuments(array $options): array
186
182
}
187
183
}
188
184
189
- private function assertValidDocumentId ($ documentId ): void
190
- {
191
- if (!\is_string ($ documentId ) && !\is_int ($ documentId )) {
192
- throw InvalidArgumentException::invalidType ('documentId ' , ['string ' , 'int ' ]);
193
- }
194
-
195
- if (\is_string ($ documentId ) && '' === trim ($ documentId )) {
196
- throw InvalidArgumentException::emptyArgument ('documentId ' );
197
- }
198
- }
199
-
200
185
private static function batchCsvString (string $ documents , int $ batchSize ): \Generator
201
186
{
202
187
$ parsedDocuments = preg_split ("/ \r\n| \n| \r/ " , trim ($ documents ));
0 commit comments