Skip to content

Commit da5142d

Browse files
committed
feat(php): clement's review
1 parent d2411d4 commit da5142d

File tree

8 files changed

+66
-85
lines changed

8 files changed

+66
-85
lines changed

clients/algoliasearch-client-php/lib/Iterators/AbstractAlgoliaIterator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ abstract protected function fetchNextPage();
5252
*/
5353
abstract protected function formatHit(array $hit);
5454

55-
public function __construct($indexName, SearchClient $searchClient, $requestOptions = [])
56-
{
55+
public function __construct(
56+
$indexName,
57+
SearchClient $searchClient,
58+
$requestOptions = []
59+
) {
5760
$this->indexName = $indexName;
5861
$this->searchClient = $searchClient;
5962
$this->requestOptions = $requestOptions + [

clients/algoliasearch-client-php/lib/Iterators/ObjectIterator.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ final class ObjectIterator extends AbstractAlgoliaIterator
66
{
77
public function getCursor()
88
{
9-
return isset($this->response['cursor']) ? $this->response['cursor'] : null;
9+
return isset($this->response['cursor'])
10+
? $this->response['cursor']
11+
: null;
1012
}
1113

1214
/**
@@ -33,10 +35,7 @@ protected function fetchNextPage()
3335

3436
$this->response = $this->searchClient->browse(
3537
$this->indexName,
36-
array_merge(
37-
$this->requestOptions,
38-
$cursor
39-
)
38+
array_merge($this->requestOptions, $cursor)
4039
);
4140

4241
$this->batchKey = 0;

clients/algoliasearch-client-php/lib/Iterators/RuleIterator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ protected function formatHit(array $hit)
1313

1414
protected function fetchNextPage()
1515
{
16-
if (is_array($this->response) && $this->key >= $this->response['nbHits']) {
16+
if (
17+
is_array($this->response) &&
18+
$this->key >= $this->response['nbHits']
19+
) {
1720
return;
1821
}
1922

2023
$this->response = $this->searchClient->searchRules(
2124
$this->indexName,
22-
array_merge(
23-
$this->requestOptions,
24-
['page' => $this->page]
25-
)
25+
array_merge($this->requestOptions, ['page' => $this->page])
2626
);
2727

2828
$this->batchKey = 0;

clients/algoliasearch-client-php/lib/Iterators/SynonymIterator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ protected function formatHit(array $hit)
1313

1414
protected function fetchNextPage()
1515
{
16-
if (is_array($this->response) && $this->key >= $this->response['nbHits']) {
16+
if (
17+
is_array($this->response) &&
18+
$this->key >= $this->response['nbHits']
19+
) {
1720
return;
1821
}
1922

clients/algoliasearch-client-php/lib/Support/Helpers.php

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
use Algolia\AlgoliaSearch\Api\SearchClient;
66
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
77
use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
8-
use Algolia\AlgoliaSearch\Iterators\ObjectIterator;
9-
use Algolia\AlgoliaSearch\Iterators\RuleIterator;
10-
use Algolia\AlgoliaSearch\Iterators\SynonymIterator;
118

129
final class Helpers
1310
{
@@ -224,46 +221,4 @@ private static function isKeyUpdated($key, $keyParams)
224221

225222
return $upToDate;
226223
}
227-
228-
/**
229-
* Helper: Iterate on the `browse` method of the client to allow aggregating objects of an index.
230-
*
231-
* @param string $indexName Index name
232-
* @param SearchClient $searchClient Search client
233-
* @param array $requestOptions Request options
234-
*
235-
* @return ObjectIterator
236-
*/
237-
public static function browseObjects($indexName, $searchClient, $requestOptions = [])
238-
{
239-
return new ObjectIterator($indexName, $searchClient, $requestOptions);
240-
}
241-
242-
/**
243-
* Helper: Iterate on the `searchRules` method of the client to allow aggregating rules of an index.
244-
*
245-
* @param string $indexName Index name
246-
* @param SearchClient $searchClient Search client
247-
* @param array $requestOptions Request options
248-
*
249-
* @return RuleIterator
250-
*/
251-
public static function browseRules($indexName, $searchClient, $requestOptions = [])
252-
{
253-
return new RuleIterator($indexName, $searchClient, $requestOptions);
254-
}
255-
256-
/**
257-
* Helper: Iterate on the `searchSynonyms` method of the client to allow aggregating rules of an index.
258-
*
259-
* @param string $indexName Index name
260-
* @param SearchClient $searchClient Search client
261-
* @param array $requestOptions Request options
262-
*
263-
* @return SynonymIterator
264-
*/
265-
public static function browseSynonyms($indexName, $searchClient, $requestOptions = [])
266-
{
267-
return new SynonymIterator($indexName, $searchClient, $requestOptions);
268-
}
269224
}

playground/php/src/search.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
$env = require_once('../loadEnv.php');
44

55
use Algolia\AlgoliaSearch\Api\SearchClient;
6-
use Algolia\AlgoliaSearch\Support\Helpers;
76

87
$client = SearchClient::create(
98
$env['ALGOLIA_APPLICATION_ID'],
@@ -30,10 +29,7 @@
3029
);
3130

3231
// browse records
33-
$results = Helpers::browseObjects(
34-
$indexName,
35-
$client,
36-
);
32+
$results = $client->browseObjects($indexName);
3733

3834
$objects = [];
3935
foreach ($results as $object) {
@@ -42,10 +38,7 @@
4238
var_dump($objects);
4339

4440
// browse synonyms
45-
$results = Helpers::browseSynonyms(
46-
$indexName,
47-
$client,
48-
);
41+
$results = $client->browseSynonyms($indexName);
4942

5043
$synonyms = [];
5144
foreach ($results as $synonym) {
@@ -54,10 +47,7 @@
5447
var_dump($synonyms);
5548

5649
// browse rules
57-
$results = Helpers::browseRules(
58-
$indexName,
59-
$client,
60-
);
50+
$results = $client->browseRules($indexName);
6151

6252
$rules = [];
6353
foreach ($results as $rule) {

templates/php/api.mustache

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use {{invokerPackage}}\Algolia;
77
use {{invokerPackage}}\ApiException;
88
use {{invokerPackage}}\Configuration\{{configClassname}};
99
use {{invokerPackage}}\Exceptions\ExceededRetriesException;
10+
use {{invokerPackage}}\Iterators\ObjectIterator;
11+
use {{invokerPackage}}\Iterators\RuleIterator;
12+
use {{invokerPackage}}\Iterators\SynonymIterator;
1013
use {{invokerPackage}}\ObjectSerializer;
1114
use {{invokerPackage}}\RetryStrategy\ApiWrapper;
1215
use {{invokerPackage}}\RetryStrategy\ApiWrapperInterface;
@@ -363,6 +366,45 @@ use {{invokerPackage}}\Support\Helpers;
363366
$requestOptions
364367
);
365368
}
369+
370+
/**
371+
* Helper: Iterate on the `browse` method of the client to allow aggregating objects of an index.
372+
*
373+
* @param string $indexName Index name
374+
* @param array $requestOptions Request options
375+
*
376+
* @return ObjectIterator
377+
*/
378+
public function browseObjects($indexName, $requestOptions = [])
379+
{
380+
return new ObjectIterator($indexName, $this, $requestOptions);
381+
}
382+
383+
/**
384+
* Helper: Iterate on the `searchRules` method of the client to allow aggregating rules of an index.
385+
*
386+
* @param string $indexName Index name
387+
* @param array $requestOptions Request options
388+
*
389+
* @return RuleIterator
390+
*/
391+
public function browseRules($indexName, $requestOptions = [])
392+
{
393+
return new RuleIterator($indexName, $this, $requestOptions);
394+
}
395+
396+
/**
397+
* Helper: Iterate on the `searchSynonyms` method of the client to allow aggregating synonyms of an index.
398+
*
399+
* @param string $indexName Index name
400+
* @param array $requestOptions Request options
401+
*
402+
* @return SynonymIterator
403+
*/
404+
public function browseSynonyms($indexName, $requestOptions = [])
405+
{
406+
return new SynonymIterator($indexName, $this, $requestOptions);
407+
}
366408
{{/isSearchClient}}
367409

368410
private function sendRequest($method, $resourcePath, $headers, $queryParameters, $httpBody, $requestOptions, $useReadTransporter = false)

website/docs/clients/migration-guides/index.mdx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -480,13 +480,8 @@ console.log(synonyms, synonyms.length);
480480

481481
```php
482482

483-
use Algolia\AlgoliaSearch\Support\Helpers;
484-
485483
// browse records
486-
$results = Helpers::browseObjects(
487-
$indexName,
488-
$client,
489-
);
484+
$results = $client->browseObjects($indexName);
490485

491486
$objects = [];
492487
foreach ($results as $object) {
@@ -495,10 +490,7 @@ foreach ($results as $object) {
495490
var_dump($objects);
496491

497492
// browse synonyms
498-
$results = Helpers::browseSynonyms(
499-
$indexName,
500-
$client,
501-
);
493+
$results = $client->browseSynonyms($indexName);
502494

503495
$synonyms = [];
504496
foreach ($results as $synonym) {
@@ -507,10 +499,7 @@ foreach ($results as $synonym) {
507499
var_dump($synonyms);
508500

509501
// browse rules
510-
$results = Helpers::browseRules(
511-
$indexName,
512-
$client,
513-
);
502+
$results = $client->browseRules($indexName);
514503

515504
$rules = [];
516505
foreach ($results as $rule) {

0 commit comments

Comments
 (0)