77use Meilisearch \Contracts \DocumentsQuery ;
88use Meilisearch \Contracts \DocumentsResults ;
99use Meilisearch \Exceptions \ApiException ;
10- use Meilisearch \Exceptions \InvalidArgumentException ;
1110use Meilisearch \Exceptions \InvalidResponseBodyException ;
1211
1312trait HandlesDocuments
1413{
15- public function getDocument ($ documentId , ?array $ fields = null )
14+ public function getDocument (string | int $ documentId , ?array $ fields = null ): array
1615 {
17- $ this ->assertValidDocumentId ($ documentId );
1816 $ query = isset ($ fields ) ? ['fields ' => implode (', ' , $ fields )] : [];
1917
2018 return $ this ->http ->get (self ::PATH .'/ ' .$ this ->uid .'/documents/ ' .$ documentId , $ query );
@@ -38,27 +36,27 @@ public function getDocuments(?DocumentsQuery $options = null): DocumentsResults
3836 }
3937 }
4038
41- public function addDocuments (array $ documents , ?string $ primaryKey = null )
39+ public function addDocuments (array $ documents , ?string $ primaryKey = null ): array
4240 {
4341 return $ this ->http ->post (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey ]);
4442 }
4543
46- public function addDocumentsJson (string $ documents , ?string $ primaryKey = null )
44+ public function addDocumentsJson (string $ documents , ?string $ primaryKey = null ): array
4745 {
4846 return $ this ->http ->post (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey ], 'application/json ' );
4947 }
5048
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
5250 {
5351 return $ this ->http ->post (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey , 'csvDelimiter ' => $ delimiter ], 'text/csv ' );
5452 }
5553
56- public function addDocumentsNdjson (string $ documents , ?string $ primaryKey = null )
54+ public function addDocumentsNdjson (string $ documents , ?string $ primaryKey = null ): array
5755 {
5856 return $ this ->http ->post (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey ], 'application/x-ndjson ' );
5957 }
6058
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
6260 {
6361 $ promises = [];
6462
@@ -69,7 +67,7 @@ public function addDocumentsInBatches(array $documents, ?int $batchSize = 1000,
6967 return $ promises ;
7068 }
7169
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
7371 {
7472 $ promises = [];
7573
@@ -80,7 +78,7 @@ public function addDocumentsCsvInBatches(string $documents, ?int $batchSize = 10
8078 return $ promises ;
8179 }
8280
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
8482 {
8583 $ promises = [];
8684
@@ -91,27 +89,27 @@ public function addDocumentsNdjsonInBatches(string $documents, ?int $batchSize =
9189 return $ promises ;
9290 }
9391
94- public function updateDocuments (array $ documents , ?string $ primaryKey = null )
92+ public function updateDocuments (array $ documents , ?string $ primaryKey = null ): array
9593 {
9694 return $ this ->http ->put (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey ]);
9795 }
9896
99- public function updateDocumentsJson (string $ documents , ?string $ primaryKey = null )
97+ public function updateDocumentsJson (string $ documents , ?string $ primaryKey = null ): array
10098 {
10199 return $ this ->http ->put (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey ], 'application/json ' );
102100 }
103101
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
105103 {
106104 return $ this ->http ->put (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey , 'csvDelimiter ' => $ delimiter ], 'text/csv ' );
107105 }
108106
109- public function updateDocumentsNdjson (string $ documents , ?string $ primaryKey = null )
107+ public function updateDocumentsNdjson (string $ documents , ?string $ primaryKey = null ): array
110108 {
111109 return $ this ->http ->put (self ::PATH .'/ ' .$ this ->uid .'/documents ' , $ documents , ['primaryKey ' => $ primaryKey ], 'application/x-ndjson ' );
112110 }
113111
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
115113 {
116114 $ promises = [];
117115
@@ -122,7 +120,7 @@ public function updateDocumentsInBatches(array $documents, ?int $batchSize = 100
122120 return $ promises ;
123121 }
124122
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
126124 {
127125 $ promises = [];
128126
@@ -133,7 +131,7 @@ public function updateDocumentsCsvInBatches(string $documents, ?int $batchSize =
133131 return $ promises ;
134132 }
135133
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
137135 {
138136 $ promises = [];
139137
@@ -154,7 +152,7 @@ public function updateDocumentsNdjsonInBatches(string $documents, ?int $batchSiz
154152 * @param non-empty-string $function
155153 * @param array{filter?: non-empty-string|list<non-empty-string>|null, context?: array<non-empty-string, mixed>} $options
156154 */
157- public function updateDocumentsByFunction (string $ function , array $ options = [])
155+ public function updateDocumentsByFunction (string $ function , array $ options = []): array
158156 {
159157 return $ this ->http ->post (self ::PATH .'/ ' .$ this ->uid .'/documents/edit ' , array_merge (['function ' => $ function ], $ options ));
160158 }
@@ -164,10 +162,8 @@ public function deleteAllDocuments(): array
164162 return $ this ->http ->delete (self ::PATH .'/ ' .$ this ->uid .'/documents ' );
165163 }
166164
167- public function deleteDocument ($ documentId ): array
165+ public function deleteDocument (string | int $ documentId ): array
168166 {
169- $ this ->assertValidDocumentId ($ documentId );
170-
171167 return $ this ->http ->delete (self ::PATH .'/ ' .$ this ->uid .'/documents/ ' .$ documentId );
172168 }
173169
@@ -186,17 +182,6 @@ public function deleteDocuments(array $options): array
186182 }
187183 }
188184
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-
200185 private static function batchCsvString (string $ documents , int $ batchSize ): \Generator
201186 {
202187 $ parsedDocuments = preg_split ("/ \r\n| \n| \r/ " , trim ($ documents ));
0 commit comments