diff --git a/src/Contracts/DeleteTasksQuery.php b/src/Contracts/DeleteTasksQuery.php index 19b08923..f175355d 100644 --- a/src/Contracts/DeleteTasksQuery.php +++ b/src/Contracts/DeleteTasksQuery.php @@ -10,9 +10,12 @@ class DeleteTasksQuery { use TasksQueryTrait; - private array $canceledBy; + private ?array $canceledBy = null; - public function setCanceledBy(array $canceledBy) + /** + * @return $this + */ + public function setCanceledBy(array $canceledBy): self { $this->canceledBy = $canceledBy; @@ -24,8 +27,8 @@ public function toArray(): array return array_filter( array_merge( $this->baseArray(), - ['canceledBy' => $this->formatArray($this->canceledBy ?? null)] - ), function ($item) { return null != $item || is_numeric($item); } + ['canceledBy' => $this->formatArray($this->canceledBy)] + ), static function ($item) { return null !== $item; } ); } } diff --git a/src/Contracts/DocumentsQuery.php b/src/Contracts/DocumentsQuery.php index 223986ba..9b3e33d0 100644 --- a/src/Contracts/DocumentsQuery.php +++ b/src/Contracts/DocumentsQuery.php @@ -6,27 +6,36 @@ class DocumentsQuery { - private int $offset; - private int $limit; - private array $fields; - private array $filter; + private ?int $offset = null; + private ?int $limit = null; + private ?array $fields = null; + private ?array $filter = null; private ?bool $retrieveVectors = null; - public function setOffset(int $offset): DocumentsQuery + /** + * @return $this + */ + public function setOffset(int $offset): self { $this->offset = $offset; return $this; } - public function setLimit(int $limit): DocumentsQuery + /** + * @return $this + */ + public function setLimit(int $limit): self { $this->limit = $limit; return $this; } - public function setFields(array $fields): DocumentsQuery + /** + * @return $this + */ + public function setFields(array $fields): self { $this->fields = $fields; @@ -38,9 +47,9 @@ public function setFields(array $fields): DocumentsQuery * * @param list> $filter a filter expression written as an array of strings * - * @return DocumentsQuery the updated DocumentsQuery instance + * @return $this the updated DocumentsQuery instance */ - public function setFilter(array $filter): DocumentsQuery + public function setFilter(array $filter): self { $this->filter = $filter; @@ -49,8 +58,10 @@ public function setFilter(array $filter): DocumentsQuery /** * @param bool|null $retrieveVectors boolean value to show _vector details + * + * @return $this */ - public function setRetrieveVectors(?bool $retrieveVectors): DocumentsQuery + public function setRetrieveVectors(?bool $retrieveVectors): self { $this->retrieveVectors = $retrieveVectors; @@ -67,6 +78,17 @@ public function hasFilter(): bool return isset($this->filter); } + public function toArray(): array + { + return array_filter([ + 'offset' => $this->offset, + 'limit' => $this->limit, + 'filter' => $this->filter, + 'fields' => $this->getFields(), + 'retrieveVectors' => (null !== $this->retrieveVectors ? ($this->retrieveVectors ? 'true' : 'false') : null), + ], static function ($item) { return null !== $item; }); + } + /** * Prepares fields for request * Fix for 1.2 document/fetch. @@ -75,7 +97,7 @@ public function hasFilter(): bool * * @return array|string|null */ - private function fields() + private function getFields() { if (!isset($this->fields)) { return null; @@ -83,19 +105,8 @@ private function fields() if ($this->hasFilter()) { return $this->fields; - } else { - return implode(',', $this->fields); } - } - public function toArray(): array - { - return array_filter([ - 'offset' => $this->offset ?? null, - 'limit' => $this->limit ?? null, - 'filter' => $this->filter ?? null, - 'fields' => $this->fields(), - 'retrieveVectors' => (null !== $this->retrieveVectors ? ($this->retrieveVectors ? 'true' : 'false') : null), - ], function ($item) { return null !== $item; }); + return implode(',', $this->fields); } } diff --git a/src/Contracts/FacetSearchQuery.php b/src/Contracts/FacetSearchQuery.php index c9a1e87a..56ce04a8 100644 --- a/src/Contracts/FacetSearchQuery.php +++ b/src/Contracts/FacetSearchQuery.php @@ -12,35 +12,50 @@ class FacetSearchQuery private ?string $facetQuery = null; private ?string $facetName = null; - public function setQuery(string $q): FacetSearchQuery + /** + * @return $this + */ + public function setQuery(string $q): self { $this->q = $q; return $this; } - public function setMatchingStrategy(string $matchingStrategy): FacetSearchQuery + /** + * @return $this + */ + public function setMatchingStrategy(string $matchingStrategy): self { $this->matchingStrategy = $matchingStrategy; return $this; } - public function setFilter(array $filter): FacetSearchQuery + /** + * @return $this + */ + public function setFilter(array $filter): self { $this->filter = $filter; return $this; } - public function setFacetQuery(string $facetQuery): FacetSearchQuery + /** + * @return $this + */ + public function setFacetQuery(string $facetQuery): self { $this->facetQuery = $facetQuery; return $this; } - public function setFacetName(string $facetName): FacetSearchQuery + /** + * @return $this + */ + public function setFacetName(string $facetName): self { $this->facetName = $facetName; @@ -55,6 +70,6 @@ public function toArray(): array 'filter' => $this->filter, 'facetQuery' => $this->facetQuery, 'facetName' => $this->facetName, - ], function ($item) { return null !== $item; }); + ], static function ($item) { return null !== $item; }); } } diff --git a/src/Contracts/FederationOptions.php b/src/Contracts/FederationOptions.php index 770e484f..fde16228 100644 --- a/src/Contracts/FederationOptions.php +++ b/src/Contracts/FederationOptions.php @@ -8,7 +8,10 @@ class FederationOptions { private ?float $weight = null; - public function setWeight(float $weight): FederationOptions + /** + * @return $this + */ + public function setWeight(float $weight): self { $this->weight = $weight; diff --git a/src/Contracts/HybridSearchOptions.php b/src/Contracts/HybridSearchOptions.php index 654b7535..affe8458 100644 --- a/src/Contracts/HybridSearchOptions.php +++ b/src/Contracts/HybridSearchOptions.php @@ -13,7 +13,10 @@ class HybridSearchOptions */ private ?string $embedder = null; - public function setSemanticRatio(float $ratio): HybridSearchOptions + /** + * @return $this + */ + public function setSemanticRatio(float $ratio): self { $this->semanticRatio = $ratio; @@ -22,8 +25,10 @@ public function setSemanticRatio(float $ratio): HybridSearchOptions /** * @param non-empty-string $embedder + * + * @return $this */ - public function setEmbedder(string $embedder): HybridSearchOptions + public function setEmbedder(string $embedder): self { $this->embedder = $embedder; diff --git a/src/Contracts/IndexesQuery.php b/src/Contracts/IndexesQuery.php index e4fd08c3..edfa08b2 100644 --- a/src/Contracts/IndexesQuery.php +++ b/src/Contracts/IndexesQuery.php @@ -6,17 +6,23 @@ class IndexesQuery { - private int $offset; - private int $limit; + private ?int $offset = null; + private ?int $limit = null; - public function setOffset(int $offset): IndexesQuery + /** + * @return $this + */ + public function setOffset(int $offset): self { $this->offset = $offset; return $this; } - public function setLimit(int $limit): IndexesQuery + /** + * @return $this + */ + public function setLimit(int $limit): self { $this->limit = $limit; @@ -26,8 +32,8 @@ public function setLimit(int $limit): IndexesQuery public function toArray(): array { return array_filter([ - 'offset' => $this->offset ?? null, - 'limit' => $this->limit ?? null, - ], function ($item) { return null != $item || is_numeric($item); }); + 'offset' => $this->offset, + 'limit' => $this->limit, + ], static function ($item) { return null !== $item; }); } } diff --git a/src/Contracts/KeysQuery.php b/src/Contracts/KeysQuery.php index 8fff6acb..d24bc014 100644 --- a/src/Contracts/KeysQuery.php +++ b/src/Contracts/KeysQuery.php @@ -6,17 +6,23 @@ class KeysQuery { - private int $offset; - private int $limit; + private ?int $offset = null; + private ?int $limit = null; - public function setOffset(int $offset): KeysQuery + /** + * @return $this + */ + public function setOffset(int $offset): self { $this->offset = $offset; return $this; } - public function setLimit(int $limit): KeysQuery + /** + * @return $this + */ + public function setLimit(int $limit): self { $this->limit = $limit; @@ -26,8 +32,8 @@ public function setLimit(int $limit): KeysQuery public function toArray(): array { return array_filter([ - 'offset' => $this->offset ?? null, - 'limit' => $this->limit ?? null, - ], function ($item) { return null != $item || is_numeric($item); }); + 'offset' => $this->offset, + 'limit' => $this->limit, + ], static function ($item) { return null !== $item; }); } } diff --git a/src/Contracts/MultiSearchFederation.php b/src/Contracts/MultiSearchFederation.php index 8ab60de5..2ada5d55 100644 --- a/src/Contracts/MultiSearchFederation.php +++ b/src/Contracts/MultiSearchFederation.php @@ -9,14 +9,20 @@ class MultiSearchFederation private ?int $limit = null; private ?int $offset = null; - public function setLimit(int $limit): MultiSearchFederation + /** + * @return $this + */ + public function setLimit(int $limit): self { $this->limit = $limit; return $this; } - public function setOffset(int $offset): MultiSearchFederation + /** + * @return $this + */ + public function setOffset(int $offset): self { $this->offset = $offset; diff --git a/src/Contracts/SearchQuery.php b/src/Contracts/SearchQuery.php index 79db1575..931db486 100644 --- a/src/Contracts/SearchQuery.php +++ b/src/Contracts/SearchQuery.php @@ -6,27 +6,27 @@ class SearchQuery { - private string $indexUid; - - private string $q; - private array $filter; - private array $locales; - private array $attributesToRetrieve; - private array $attributesToCrop; - private ?int $cropLength; - private array $attributesToHighlight; - private string $cropMarker; - private string $highlightPreTag; - private string $highlightPostTag; - private array $facets; - private ?bool $showMatchesPosition; - private array $sort; - private string $matchingStrategy; - private ?int $offset; - private ?int $limit; - private ?int $hitsPerPage; - private ?int $page; - private ?array $vector; + private ?string $indexUid = null; + + private ?string $q = null; + private ?array $filter = null; + private ?array $locales = null; + private ?array $attributesToRetrieve = null; + private ?array $attributesToCrop = null; + private ?int $cropLength = null; + private ?array $attributesToHighlight = null; + private ?string $cropMarker = null; + private ?string $highlightPreTag = null; + private ?string $highlightPostTag = null; + private ?array $facets = null; + private ?bool $showMatchesPosition = null; + private ?array $sort = null; + private ?string $matchingStrategy = null; + private ?int $offset = null; + private ?int $limit = null; + private ?int $hitsPerPage = null; + private ?int $page = null; + private ?array $vector = null; private ?HybridSearchOptions $hybrid = null; private ?array $attributesToSearchOn = null; private ?bool $showRankingScore = null; @@ -35,14 +35,20 @@ class SearchQuery private ?string $distinct = null; private ?FederationOptions $federationOptions = null; - public function setQuery(string $q): SearchQuery + /** + * @return $this + */ + public function setQuery(string $q): self { $this->q = $q; return $this; } - public function setFilter(array $filter): SearchQuery + /** + * @return $this + */ + public function setFilter(array $filter): self { $this->filter = $filter; @@ -51,78 +57,110 @@ public function setFilter(array $filter): SearchQuery /** * @param list $locales + * + * @return $this */ - public function setLocales(array $locales): SearchQuery + public function setLocales(array $locales): self { $this->locales = $locales; return $this; } - public function setAttributesToRetrieve(array $attributesToRetrieve): SearchQuery + /** + * @return $this + */ + public function setAttributesToRetrieve(array $attributesToRetrieve): self { $this->attributesToRetrieve = $attributesToRetrieve; return $this; } - public function setAttributesToCrop(array $attributesToCrop): SearchQuery + /** + * @return $this + */ + public function setAttributesToCrop(array $attributesToCrop): self { $this->attributesToCrop = $attributesToCrop; return $this; } - public function setCropLength(?int $cropLength): SearchQuery + /** + * @return $this + */ + public function setCropLength(?int $cropLength): self { $this->cropLength = $cropLength; return $this; } - public function setAttributesToHighlight(array $attributesToHighlight): SearchQuery + /** + * @return $this + */ + public function setAttributesToHighlight(array $attributesToHighlight): self { $this->attributesToHighlight = $attributesToHighlight; return $this; } - public function setCropMarker(string $cropMarker): SearchQuery + /** + * @return $this + */ + public function setCropMarker(string $cropMarker): self { $this->cropMarker = $cropMarker; return $this; } - public function setHighlightPreTag(string $highlightPreTag): SearchQuery + /** + * @return $this + */ + public function setHighlightPreTag(string $highlightPreTag): self { $this->highlightPreTag = $highlightPreTag; return $this; } - public function setHighlightPostTag(string $highlightPostTag): SearchQuery + /** + * @return $this + */ + public function setHighlightPostTag(string $highlightPostTag): self { $this->highlightPostTag = $highlightPostTag; return $this; } - public function setFacets(array $facets): SearchQuery + /** + * @return $this + */ + public function setFacets(array $facets): self { $this->facets = $facets; return $this; } - public function setShowMatchesPosition(?bool $showMatchesPosition): SearchQuery + /** + * @return $this + */ + public function setShowMatchesPosition(?bool $showMatchesPosition): self { $this->showMatchesPosition = $showMatchesPosition; return $this; } - public function setShowRankingScore(?bool $showRankingScore): SearchQuery + /** + * @return $this + */ + public function setShowRankingScore(?bool $showRankingScore): self { $this->showRankingScore = $showRankingScore; @@ -137,15 +175,20 @@ public function setShowRankingScore(?bool $showRankingScore): SearchQuery * More info: https://www.meilisearch.com/docs/reference/api/experimental-features * * @param bool $showRankingScoreDetails whether the feature is enabled or not + * + * @return $this */ - public function setShowRankingScoreDetails(?bool $showRankingScoreDetails): SearchQuery + public function setShowRankingScoreDetails(?bool $showRankingScoreDetails): self { $this->showRankingScoreDetails = $showRankingScoreDetails; return $this; } - public function setRankingScoreThreshold(?float $rankingScoreThreshold): SearchQuery + /** + * @return $this + */ + public function setRankingScoreThreshold(?float $rankingScoreThreshold): self { $this->rankingScoreThreshold = $rankingScoreThreshold; @@ -154,57 +197,80 @@ public function setRankingScoreThreshold(?float $rankingScoreThreshold): SearchQ /** * @param non-empty-string|null $distinct + * + * @return $this */ - public function setDistinct(?string $distinct): SearchQuery + public function setDistinct(?string $distinct): self { $this->distinct = $distinct; return $this; } - public function setSort(array $sort): SearchQuery + /** + * @return $this + */ + public function setSort(array $sort): self { $this->sort = $sort; return $this; } - public function setMatchingStrategy(string $matchingStrategy): SearchQuery + /** + * @return $this + */ + public function setMatchingStrategy(string $matchingStrategy): self { $this->matchingStrategy = $matchingStrategy; return $this; } - public function setOffset(?int $offset): SearchQuery + /** + * @return $this + */ + public function setOffset(?int $offset): self { $this->offset = $offset; return $this; } - public function setLimit(?int $limit): SearchQuery + /** + * @return $this + */ + public function setLimit(?int $limit): self { $this->limit = $limit; return $this; } - public function setHitsPerPage(?int $hitsPerPage): SearchQuery + /** + * @return $this + */ + public function setHitsPerPage(?int $hitsPerPage): self { $this->hitsPerPage = $hitsPerPage; return $this; } - public function setPage(?int $page): SearchQuery + /** + * @return $this + */ + public function setPage(?int $page): self { $this->page = $page; return $this; } - public function setIndexUid(string $uid): SearchQuery + /** + * @return $this + */ + public function setIndexUid(string $uid): self { $this->indexUid = $uid; @@ -214,8 +280,10 @@ public function setIndexUid(string $uid): SearchQuery /** * This option is only available while doing a federated search. * If used in another context an error will be returned by Meilisearch. + * + * @return $this */ - public function setFederationOptions(FederationOptions $federationOptions): SearchQuery + public function setFederationOptions(FederationOptions $federationOptions): self { $this->federationOptions = $federationOptions; @@ -230,8 +298,10 @@ public function setFederationOptions(FederationOptions $federationOptions): Sear * More info: https://www.meilisearch.com/docs/reference/api/experimental-features * * @param list> $vector a multi-level array floats + * + * @return $this */ - public function setVector(array $vector): SearchQuery + public function setVector(array $vector): self { $this->vector = $vector; @@ -245,8 +315,10 @@ public function setVector(array $vector): SearchQuery * (new HybridSearchOptions()) * ->setSemanticRatio(0.8) * ->setEmbedder('manual'); + * + * @return $this */ - public function setHybrid(HybridSearchOptions $hybridOptions): SearchQuery + public function setHybrid(HybridSearchOptions $hybridOptions): self { $this->hybrid = $hybridOptions; @@ -255,8 +327,10 @@ public function setHybrid(HybridSearchOptions $hybridOptions): SearchQuery /** * @param list $attributesToSearchOn + * + * @return $this */ - public function setAttributesToSearchOn(array $attributesToSearchOn): SearchQuery + public function setAttributesToSearchOn(array $attributesToSearchOn): self { $this->attributesToSearchOn = $attributesToSearchOn; @@ -266,26 +340,26 @@ public function setAttributesToSearchOn(array $attributesToSearchOn): SearchQuer public function toArray(): array { return array_filter([ - 'indexUid' => $this->indexUid ?? null, - 'q' => $this->q ?? null, - 'filter' => $this->filter ?? null, - 'locales' => $this->locales ?? null, - 'attributesToRetrieve' => $this->attributesToRetrieve ?? null, - 'attributesToCrop' => $this->attributesToCrop ?? null, - 'cropLength' => $this->cropLength ?? null, - 'attributesToHighlight' => $this->attributesToHighlight ?? null, - 'cropMarker' => $this->cropMarker ?? null, - 'highlightPreTag' => $this->highlightPreTag ?? null, - 'highlightPostTag' => $this->highlightPostTag ?? null, - 'facets' => $this->facets ?? null, - 'showMatchesPosition' => $this->showMatchesPosition ?? null, - 'sort' => $this->sort ?? null, - 'matchingStrategy' => $this->matchingStrategy ?? null, - 'offset' => $this->offset ?? null, - 'limit' => $this->limit ?? null, - 'hitsPerPage' => $this->hitsPerPage ?? null, - 'page' => $this->page ?? null, - 'vector' => $this->vector ?? null, + 'indexUid' => $this->indexUid, + 'q' => $this->q, + 'filter' => $this->filter, + 'locales' => $this->locales, + 'attributesToRetrieve' => $this->attributesToRetrieve, + 'attributesToCrop' => $this->attributesToCrop, + 'cropLength' => $this->cropLength, + 'attributesToHighlight' => $this->attributesToHighlight, + 'cropMarker' => $this->cropMarker, + 'highlightPreTag' => $this->highlightPreTag, + 'highlightPostTag' => $this->highlightPostTag, + 'facets' => $this->facets, + 'showMatchesPosition' => $this->showMatchesPosition, + 'sort' => $this->sort, + 'matchingStrategy' => $this->matchingStrategy, + 'offset' => $this->offset, + 'limit' => $this->limit, + 'hitsPerPage' => $this->hitsPerPage, + 'page' => $this->page, + 'vector' => $this->vector, 'hybrid' => null !== $this->hybrid ? $this->hybrid->toArray() : null, 'attributesToSearchOn' => $this->attributesToSearchOn, 'showRankingScore' => $this->showRankingScore, @@ -293,6 +367,6 @@ public function toArray(): array 'rankingScoreThreshold' => $this->rankingScoreThreshold, 'distinct' => $this->distinct, 'federationOptions' => null !== $this->federationOptions ? $this->federationOptions->toArray() : null, - ], function ($item) { return null !== $item; }); + ], static function ($item) { return null !== $item; }); } } diff --git a/src/Contracts/SimilarDocumentsQuery.php b/src/Contracts/SimilarDocumentsQuery.php index 531610c8..ab6dc95b 100644 --- a/src/Contracts/SimilarDocumentsQuery.php +++ b/src/Contracts/SimilarDocumentsQuery.php @@ -57,8 +57,10 @@ public function __construct($id) /** * @param non-negative-int|null $offset + * + * @return $this */ - public function setOffset(?int $offset): SimilarDocumentsQuery + public function setOffset(?int $offset): self { $this->offset = $offset; @@ -67,8 +69,10 @@ public function setOffset(?int $offset): SimilarDocumentsQuery /** * @param positive-int|null $limit + * + * @return $this */ - public function setLimit(?int $limit): SimilarDocumentsQuery + public function setLimit(?int $limit): self { $this->limit = $limit; @@ -77,8 +81,10 @@ public function setLimit(?int $limit): SimilarDocumentsQuery /** * @param array|string> $filter an array of arrays representing filter conditions + * + * @return $this */ - public function setFilter(array $filter): SimilarDocumentsQuery + public function setFilter(array $filter): self { $this->filter = $filter; @@ -87,8 +93,10 @@ public function setFilter(array $filter): SimilarDocumentsQuery /** * @param non-empty-string $embedder + * + * @return $this */ - public function setEmbedder(string $embedder): SimilarDocumentsQuery + public function setEmbedder(string $embedder): self { $this->embedder = $embedder; @@ -97,8 +105,10 @@ public function setEmbedder(string $embedder): SimilarDocumentsQuery /** * @param list $attributesToRetrieve an array of attribute names to retrieve + * + * @return $this */ - public function setAttributesToRetrieve(array $attributesToRetrieve): SimilarDocumentsQuery + public function setAttributesToRetrieve(array $attributesToRetrieve): self { $this->attributesToRetrieve = $attributesToRetrieve; @@ -107,8 +117,10 @@ public function setAttributesToRetrieve(array $attributesToRetrieve): SimilarDoc /** * @param bool|null $showRankingScore boolean value to show ranking score + * + * @return $this */ - public function setShowRankingScore(?bool $showRankingScore): SimilarDocumentsQuery + public function setShowRankingScore(?bool $showRankingScore): self { $this->showRankingScore = $showRankingScore; @@ -117,8 +129,10 @@ public function setShowRankingScore(?bool $showRankingScore): SimilarDocumentsQu /** * @param bool|null $showRankingScoreDetails boolean value to show ranking score details + * + * @return $this */ - public function setShowRankingScoreDetails(?bool $showRankingScoreDetails): SimilarDocumentsQuery + public function setShowRankingScoreDetails(?bool $showRankingScoreDetails): self { $this->showRankingScoreDetails = $showRankingScoreDetails; @@ -127,8 +141,10 @@ public function setShowRankingScoreDetails(?bool $showRankingScoreDetails): Simi /** * @param bool|null $retrieveVectors boolean value to show _vector details + * + * @return $this */ - public function setRetrieveVectors(?bool $retrieveVectors): SimilarDocumentsQuery + public function setRetrieveVectors(?bool $retrieveVectors): self { $this->retrieveVectors = $retrieveVectors; @@ -137,8 +153,10 @@ public function setRetrieveVectors(?bool $retrieveVectors): SimilarDocumentsQuer /** * @param int|float|null $rankingScoreThreshold + * + * @return $this */ - public function setRankingScoreThreshold($rankingScoreThreshold): SimilarDocumentsQuery + public function setRankingScoreThreshold($rankingScoreThreshold): self { $this->rankingScoreThreshold = $rankingScoreThreshold; @@ -172,8 +190,6 @@ public function toArray(): array 'showRankingScoreDetails' => $this->showRankingScoreDetails, 'retrieveVectors' => $this->retrieveVectors, 'rankingScoreThreshold' => $this->rankingScoreThreshold, - ], static function ($item) { - return null !== $item; - }); + ], static function ($item) {return null !== $item; }); } } diff --git a/src/Contracts/TasksQuery.php b/src/Contracts/TasksQuery.php index 6baa6e76..07be8291 100644 --- a/src/Contracts/TasksQuery.php +++ b/src/Contracts/TasksQuery.php @@ -10,25 +10,34 @@ class TasksQuery { use TasksQueryTrait; - private int $from; - private int $limit; - private array $canceledBy; - - public function setFrom(int $from): TasksQuery + private ?int $from = null; + private ?int $limit = null; + private ?array $canceledBy = null; + + /** + * @return $this + */ + public function setFrom(int $from): self { $this->from = $from; return $this; } - public function setCanceledBy(array $canceledBy): TasksQuery + /** + * @return $this + */ + public function setCanceledBy(array $canceledBy): self { $this->canceledBy = $canceledBy; return $this; } - public function setLimit(int $limit): TasksQuery + /** + * @return $this + */ + public function setLimit(int $limit): self { $this->limit = $limit; @@ -41,11 +50,11 @@ public function toArray(): array array_merge( $this->baseArray(), [ - 'from' => $this->from ?? null, - 'limit' => $this->limit ?? null, - 'canceledBy' => $this->formatArray($this->canceledBy ?? null), + 'from' => $this->from, + 'limit' => $this->limit, + 'canceledBy' => $this->formatArray($this->canceledBy), ] - ), function ($item) { return null != $item || is_numeric($item); } + ), static function ($item) { return null !== $item; } ); } } diff --git a/src/Contracts/TasksResults.php b/src/Contracts/TasksResults.php index a77cb8bd..97b7ac8e 100644 --- a/src/Contracts/TasksResults.php +++ b/src/Contracts/TasksResults.php @@ -7,19 +7,22 @@ class TasksResults extends Data { /** - * @var int<0, max> + * @var non-negative-int */ private int $next; + /** - * @var int<0, max> + * @var non-negative-int */ private int $limit; + /** - * @var int<0, max> + * @var non-negative-int */ private int $from; + /** - * @var int<0, max> + * @var non-negative-int */ private int $total; @@ -41,21 +44,33 @@ public function getResults(): array return $this->data; } + /** + * @return non-negative-int + */ public function getNext(): int { return $this->next; } + /** + * @return non-negative-int + */ public function getLimit(): int { return $this->limit; } + /** + * @return non-negative-int + */ public function getFrom(): int { return $this->from; } + /** + * @return non-negative-int + */ public function getTotal(): int { return $this->total; diff --git a/src/Endpoints/Delegates/HandlesDocuments.php b/src/Endpoints/Delegates/HandlesDocuments.php index 085a0f5d..f461d2bf 100644 --- a/src/Endpoints/Delegates/HandlesDocuments.php +++ b/src/Endpoints/Delegates/HandlesDocuments.php @@ -61,6 +61,7 @@ public function addDocumentsNdjson(string $documents, ?string $primaryKey = null public function addDocumentsInBatches(array $documents, ?int $batchSize = 1000, ?string $primaryKey = null) { $promises = []; + foreach (self::batch($documents, $batchSize) as $batch) { $promises[] = $this->addDocuments($batch, $primaryKey); } @@ -82,6 +83,7 @@ public function addDocumentsCsvInBatches(string $documents, ?int $batchSize = 10 public function addDocumentsNdjsonInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null) { $promises = []; + foreach (self::batchNdjsonString($documents, $batchSize) as $batch) { $promises[] = $this->addDocumentsNdjson($batch, $primaryKey); } @@ -112,6 +114,7 @@ public function updateDocumentsNdjson(string $documents, ?string $primaryKey = n public function updateDocumentsInBatches(array $documents, ?int $batchSize = 1000, ?string $primaryKey = null) { $promises = []; + foreach (self::batch($documents, $batchSize) as $batch) { $promises[] = $this->updateDocuments($batch, $primaryKey); } @@ -122,6 +125,7 @@ public function updateDocumentsInBatches(array $documents, ?int $batchSize = 100 public function updateDocumentsCsvInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null, ?string $delimiter = null) { $promises = []; + foreach (self::batchCsvString($documents, $batchSize) as $batch) { $promises[] = $this->updateDocumentsCsv($batch, $primaryKey, $delimiter); } @@ -132,6 +136,7 @@ public function updateDocumentsCsvInBatches(string $documents, ?int $batchSize = public function updateDocumentsNdjsonInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null) { $promises = []; + foreach (self::batchNdjsonString($documents, $batchSize) as $batch) { $promises[] = $this->updateDocumentsNdjson($batch, $primaryKey); } @@ -194,10 +199,10 @@ private function assertValidDocumentId($documentId): void private static function batchCsvString(string $documents, int $batchSize): \Generator { - $documents = preg_split("/\r\n|\n|\r/", trim($documents)); - $csvHeader = $documents[0]; - array_shift($documents); - $batches = array_chunk($documents, $batchSize); + $parsedDocuments = preg_split("/\r\n|\n|\r/", trim($documents)); + $csvHeader = $parsedDocuments[0]; + array_shift($parsedDocuments); + $batches = array_chunk($parsedDocuments, $batchSize); /** @var array $batch */ foreach ($batches as $batch) { @@ -210,8 +215,8 @@ private static function batchCsvString(string $documents, int $batchSize): \Gene private static function batchNdjsonString(string $documents, int $batchSize): \Generator { - $documents = preg_split("/\r\n|\n|\r/", trim($documents)); - $batches = array_chunk($documents, $batchSize); + $parsedDocuments = preg_split("/\r\n|\n|\r/", trim($documents)); + $batches = array_chunk($parsedDocuments, $batchSize); /** @var array $batch */ foreach ($batches as $batch) { @@ -224,6 +229,7 @@ private static function batchNdjsonString(string $documents, int $batchSize): \G private static function batch(array $documents, int $batchSize): \Generator { $batches = array_chunk($documents, $batchSize); + foreach ($batches as $batch) { yield $batch; } diff --git a/src/Endpoints/Delegates/TasksQueryTrait.php b/src/Endpoints/Delegates/TasksQueryTrait.php index 1c87d823..5af66f6a 100644 --- a/src/Endpoints/Delegates/TasksQueryTrait.php +++ b/src/Endpoints/Delegates/TasksQueryTrait.php @@ -17,21 +17,30 @@ trait TasksQueryTrait private \DateTime $beforeFinishedAt; private \DateTime $afterFinishedAt; - public function setTypes(array $types) + /** + * @return $this + */ + public function setTypes(array $types): self { $this->types = $types; return $this; } - public function setStatuses(array $statuses) + /** + * @return $this + */ + public function setStatuses(array $statuses): self { $this->statuses = $statuses; return $this; } - public function setIndexUids(array $indexUids) + /** + * @return $this + */ + public function setIndexUids(array $indexUids): self { $this->indexUids = $indexUids; @@ -43,49 +52,70 @@ public function getIndexUids(): array return $this->indexUids ?? []; } - public function setUids(array $uids) + /** + * @return $this + */ + public function setUids(array $uids): self { $this->uids = $uids; return $this; } - public function setBeforeEnqueuedAt(\DateTime $date) + /** + * @return $this + */ + public function setBeforeEnqueuedAt(\DateTime $date): self { $this->beforeEnqueuedAt = $date; return $this; } - public function setAfterEnqueuedAt(\DateTime $date) + /** + * @return $this + */ + public function setAfterEnqueuedAt(\DateTime $date): self { $this->afterEnqueuedAt = $date; return $this; } - public function setBeforeStartedAt(\DateTime $date) + /** + * @return $this + */ + public function setBeforeStartedAt(\DateTime $date): self { $this->beforeStartedAt = $date; return $this; } - public function setAfterStartedAt(\DateTime $date) + /** + * @return $this + */ + public function setAfterStartedAt(\DateTime $date): self { $this->afterStartedAt = $date; return $this; } - public function setBeforeFinishedAt(\DateTime $date) + /** + * @return $this + */ + public function setBeforeFinishedAt(\DateTime $date): self { $this->beforeFinishedAt = $date; return $this; } - public function setAfterFinishedAt(\DateTime $date) + /** + * @return $this + */ + public function setAfterFinishedAt(\DateTime $date): self { $this->afterFinishedAt = $date; @@ -96,7 +126,7 @@ public function toArray(): array { return array_filter( $this->baseArray(), - function ($item) { return null != $item || is_numeric($item); } + static function ($item) { return null !== $item; } ); } @@ -116,12 +146,12 @@ protected function baseArray(): array ]; } - private function formatDate(?\DateTime $date) + private function formatDate(?\DateTimeInterface $date): ?string { - return isset($date) ? $date->format(\DateTime::RFC3339) : null; + return isset($date) ? $date->format(\DateTimeInterface::RFC3339) : null; } - private function formatArray(?array $arr) + private function formatArray(?array $arr): ?string { return isset($arr) ? implode(',', $arr) : null; } diff --git a/src/Endpoints/Tasks.php b/src/Endpoints/Tasks.php index 30b4a920..1d6d2b3f 100644 --- a/src/Endpoints/Tasks.php +++ b/src/Endpoints/Tasks.php @@ -26,17 +26,15 @@ public function all(array $query = []): array public function cancelTasks(?CancelTasksQuery $options): array { $options = $options ?? new CancelTasksQuery(); - $response = $this->http->post('/tasks/cancel', null, $options->toArray()); - return $response; + return $this->http->post('/tasks/cancel', null, $options->toArray()); } public function deleteTasks(?DeleteTasksQuery $options): array { $options = $options ?? new DeleteTasksQuery(); - $response = $this->http->delete(self::PATH, $options->toArray()); - return $response; + return $this->http->delete(self::PATH, $options->toArray()); } /** @@ -44,15 +42,19 @@ public function deleteTasks(?DeleteTasksQuery $options): array */ public function waitTask($taskUid, int $timeoutInMs, int $intervalInMs): array { - $timeout_temp = 0; - while ($timeoutInMs > $timeout_temp) { + $timeoutTemp = 0; + + while ($timeoutInMs > $timeoutTemp) { $res = $this->get($taskUid); - if ('enqueued' != $res['status'] && 'processing' != $res['status']) { + + if ('enqueued' !== $res['status'] && 'processing' !== $res['status']) { return $res; } - $timeout_temp += $intervalInMs; + + $timeoutTemp += $intervalInMs; usleep(1000 * $intervalInMs); } + throw new TimeOutException(); } @@ -62,6 +64,7 @@ public function waitTask($taskUid, int $timeoutInMs, int $intervalInMs): array public function waitTasks(array $taskUids, int $timeoutInMs, int $intervalInMs): array { $tasks = []; + foreach ($taskUids as $taskUid) { $tasks[] = $this->waitTask($taskUid, $timeoutInMs, $intervalInMs); } diff --git a/src/Endpoints/TenantToken.php b/src/Endpoints/TenantToken.php index d52b7951..5cb0d990 100644 --- a/src/Endpoints/TenantToken.php +++ b/src/Endpoints/TenantToken.php @@ -4,7 +4,6 @@ namespace Meilisearch\Endpoints; -use DateTime; use Meilisearch\Contracts\Endpoint; use Meilisearch\Exceptions\InvalidArgumentException; use Meilisearch\Http\Serialize\Json; diff --git a/src/Http/Client.php b/src/Http/Client.php index a1e0c2c7..afffc154 100644 --- a/src/Http/Client.php +++ b/src/Http/Client.php @@ -196,9 +196,9 @@ private function parseResponse(ResponseInterface $response) * * @return bool true if any header value contains 'application/json', otherwise false */ - private function isJSONResponse(array $headerValues) + private function isJSONResponse(array $headerValues): bool { - $filteredHeaders = array_filter($headerValues, function (string $headerValue) { + $filteredHeaders = array_filter($headerValues, static function (string $headerValue) { return false !== strpos($headerValue, 'application/json'); });