Skip to content

Commit 623dc6a

Browse files
committed
feat(php): more guides + better types
1 parent 21603a3 commit 623dc6a

12 files changed

+157
-28
lines changed

templates/php/guides/search/deleteMultipleIndices.mustache

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require __DIR__.'/../vendor/autoload.php';
44
{{> snippets/import}}
5+
use Algolia\AlgoliaSearch\Model\Search\Action;
6+
use Algolia\AlgoliaSearch\Model\Search\MultipleBatchRequest;
57

68
try {
79
// You need an API key with `deleteIndex`
@@ -22,10 +24,9 @@ try {
2224
// Delete primary indices first
2325
if (!empty($primaryIndices)) {
2426
$requests = array_map(function ($index) {
25-
return [
26-
'action' => 'delete',
27-
'indexName' => $index['name'],
28-
];
27+
return (new MultipleBatchRequest())
28+
->setAction((new Action())::DELETE)
29+
->setIndexName($index['name']);
2930
}, $primaryIndices);
3031

3132
{{#dynamicSnippet}}deleteMultipleIndicesPrimary{{/dynamicSnippet}};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
use Algolia\AlgoliaSearch\Model\Search\Anchoring;
6+
use Algolia\AlgoliaSearch\Model\Search\Condition;
7+
use Algolia\AlgoliaSearch\Model\Search\Consequence;
8+
use Algolia\AlgoliaSearch\Model\Search\Rule;
9+
10+
$condition = (new Condition())
11+
->setAnchoring((new Anchoring())::IS)
12+
->setPattern('{facet:brand}');
13+
14+
$consequence = (new Consequence())
15+
->setFilterPromotes(true);
16+
17+
$rule = (new Rule())
18+
->setObjectID('rule_with_filterPromotes')
19+
->setConditions([$condition])
20+
->setConsequence($consequence);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
{{> snippets/import}}
5+
use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
6+
7+
try {
8+
{{> snippets/init}}
9+
10+
$query = 'query';
11+
12+
// 1. Change the sort dynamically based on the UI events
13+
$sortByPrice = false;
14+
15+
// 2. Get the index name based on sortByPrice
16+
$indexName = $sortByPrice ? 'products_price_desc' : 'products';
17+
18+
// 3. Search on dynamic index name (primary or replica)
19+
{{#dynamicSnippet}}searchWithIndexNameVar{{/dynamicSnippet}};
20+
} catch (Exception $e) {
21+
echo $e->getMessage() . PHP_EOL;
22+
}
23+

templates/php/guides/search/searchRecentlyPublishedBooks.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
require __DIR__.'/../vendor/autoload.php';
44
{{> snippets/import}}
5+
use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
56

67
try {
78
{{> snippets/init}}
89

910
$dateTimestamp = time();
10-
$searchParams = [
11-
'query' => '<YOUR_SEARCH_QUERY>',
12-
'filters' => "date_timestamp > $dateTimestamp",
13-
];
11+
$searchParams = (new SearchParamsObject())
12+
->setQuery('<YOUR_SEARCH_QUERY>')
13+
->setFilters("date_timestamp > " . $dateTimestamp);
1414

1515
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
1616
} catch (Exception $e) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
{{> snippets/import}}
5+
use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
6+
7+
try {
8+
{{> snippets/init}}
9+
10+
/*
11+
'94.228.178.246' should be replaced with your user's IP address.
12+
Depending on your stack there are multiple ways to get this information.
13+
*/
14+
$ip = '94.228.178.246';
15+
$query = 'query';
16+
17+
$searchParams = (new SearchParamsObject())
18+
->setQuery($query)
19+
->setAnalytics(true);
20+
21+
{{#dynamicSnippet}}searchWithSearchParamsAndForwardedHeader{{/dynamicSnippet}};
22+
} catch (Exception $e) {
23+
echo $e->getMessage() . PHP_EOL;
24+
}
25+

templates/php/guides/search/searchWithGAToken.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require __DIR__.'/../vendor/autoload.php';
44
{{> snippets/import}}
5+
use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
56

67
$getGoogleAnalyticsUserIdFromBrowserCookie = function (string $cookieName): string {
78
// Implement your logic here
@@ -12,17 +13,16 @@ try {
1213
{{> snippets/init}}
1314

1415
$userToken = $getGoogleAnalyticsUserIdFromBrowserCookie('_ga');
15-
$searchParams = [
16-
'query' => '<YOUR_SEARCH_QUERY>',
17-
'userToken' => $userToken,
18-
];
16+
$searchParams = (new SearchParamsObject())
17+
->setQuery('<YOUR_SEARCH_QUERY>')
18+
->setUserToken($userToken);
1919

2020
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
2121

2222
/** @var string|null $loggedInUser */
2323
$loggedInUser = null;
2424

25-
$searchParams['userToken'] = $loggedInUser ?? $userToken;
25+
$searchParams->setUserToken($loggedInUser ?? $userToken);
2626

2727
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
2828
} catch (Exception $e) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
{{> snippets/import}}
5+
use Algolia\AlgoliaSearch\Model\Search\OptionalWords;
6+
use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
7+
8+
try {
9+
{{> snippets/init}}
10+
11+
$query = 'the query';
12+
13+
$optionalWords = new OptionalWords();
14+
$optionalWords[] = $query;
15+
16+
$searchParams = (new SearchParamsObject())
17+
->setQuery($query)
18+
->setOptionalWords($optionalWords);
19+
20+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
21+
} catch (Exception $e) {
22+
echo $e->getMessage() . PHP_EOL;
23+
}
24+

templates/php/guides/search/searchWithOptionalFilters.mustache

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22

33
require __DIR__.'/../vendor/autoload.php';
44
{{> snippets/import}}
5+
use Algolia\AlgoliaSearch\Model\Search\OptionalFilters;
6+
use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
57

68
$labels = []; // A list of labels
7-
$reduceLabelsToFilters = function (array $labels): array {
9+
$reduceLabelsToFilters = function (array $labels): OptionalFilters {
810
// Implement your logic here
9-
return [];
11+
return new OptionalFilters();
1012
};
1113

1214
try {
1315
{{> snippets/init}}
1416

1517
$optionalFilters = $reduceLabelsToFilters($labels);
16-
$searchParams = [
17-
'query' => '<YOUR_SEARCH_QUERY>',
18-
'optionalFilters' => $optionalFilters,
19-
];
18+
$searchParams = (new SearchParamsObject())
19+
->setQuery('<YOUR_SEARCH_QUERY>')
20+
->setOptionalFilters($optionalFilters);
2021

2122
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
2223
} catch (Exception $e) {

templates/php/guides/search/searchWithRuleContextBuyer.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require __DIR__.'/../vendor/autoload.php';
44
{{> snippets/import}}
5+
use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
56

67
$getBuyerAccountId = function (): string {
78
// Implement your logic here
@@ -13,10 +14,9 @@ try {
1314

1415
// get the buyer account information
1516
$buyer = $getBuyerAccountId();
16-
$searchParams = [
17-
'query' => '<YOUR_SEARCH_QUERY>',
18-
'ruleContexts' => [$buyer],
19-
];
17+
$searchParams = (new SearchParamsObject())
18+
->setQuery('<YOUR_SEARCH_QUERY>')
19+
->setRuleContexts([$buyer]);
2020

2121
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
2222
} catch (Exception $e) {

templates/php/guides/search/searchWithRuleContexts.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require __DIR__.'/../vendor/autoload.php';
44
{{> snippets/import}}
5+
use Algolia\AlgoliaSearch\Model\Search\SearchParamsObject;
56

67
$getPlatformTag = function (): string {
78
// Implement your logic here
@@ -13,10 +14,9 @@ try {
1314

1415
// get the buyer account information
1516
$platformTag = $getPlatformTag();
16-
$searchParams = [
17-
'query' => '<YOUR_SEARCH_QUERY>',
18-
'ruleContexts' => [$platformTag],
19-
];
17+
$searchParams = (new SearchParamsObject())
18+
->setQuery('<YOUR_SEARCH_QUERY>')
19+
->setRuleContexts([$platformTag]);
2020

2121
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
2222
} catch (Exception $e) {

0 commit comments

Comments
 (0)