Skip to content

Commit cb02bc1

Browse files
committed
feat(php): rest of the guides
1 parent af736bd commit cb02bc1

13 files changed

+293
-4
lines changed

templates/php/guides/search/saveObjectsChunks.mustache

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,22 @@ try {
77
{{> snippets/init}}
88

99
$path = dirname(__FILE__) . DIRECTORY_SEPARATOR . "actors.json";
10-
$response = file_get_contents($url);
11-
$records = json_decode($response ?: "[]", true);
10+
11+
if (!file_exists($path)) {
12+
throw new Exception("File not found: $path");
13+
}
14+
15+
$data = file_get_contents($path);
16+
17+
if ($data === false) {
18+
throw new Exception("Failed to read file: $path");
19+
}
20+
21+
$records = json_decode($data, true);
22+
23+
if (json_last_error() !== JSON_ERROR_NONE) {
24+
throw new Exception("JSON decode error: " . json_last_error_msg());
25+
}
1226

1327
$chunkSize = 10000;
1428

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+
6+
$getAllAppIDConfigurations = function () {
7+
return [ /* A list of your MCM AppID/ApiKey pairs */ ];
8+
};
9+
10+
$playlists = [ /* Your records */ ];
11+
12+
// Fetch from your own data storage and with your own code
13+
// the list of application IDs and API keys to target each cluster
14+
$configurations = $getAllAppIDConfigurations();
15+
16+
// Send the records to each cluster
17+
foreach ($configurations as [$appID, $apiKey]) {
18+
try {
19+
$client = SearchClient::create($appID, $apiKey);
20+
21+
{{#dynamicSnippet}}saveObjectsPlaylists{{/dynamicSnippet}};
22+
} catch (Exception $e) {
23+
echo $e->getMessage() . PHP_EOL;
24+
}
25+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
require __DIR__.'/../vendor/autoload.php';
4+
{{> snippets/import}}
5+
6+
try {
7+
{{> snippets/init}}
8+
9+
$path = dirname(__FILE__) . DIRECTORY_SEPARATOR . "products.json";
10+
11+
if (!file_exists($path)) {
12+
throw new Exception("File not found: $path");
13+
}
14+
15+
$data = file_get_contents($path);
16+
17+
if ($data === false) {
18+
throw new Exception("Failed to read file: $path");
19+
}
20+
21+
$products = json_decode($data, true);
22+
23+
if (json_last_error() !== JSON_ERROR_NONE) {
24+
throw new Exception("JSON decode error: " . json_last_error_msg());
25+
}
26+
27+
$records = array_map(function ($product) {
28+
$reference = $product['product_reference'];
29+
$suffixes = [];
30+
31+
while (strlen($reference) > 1) {
32+
$reference = substr($reference, 1);
33+
$suffixes[] = $reference;
34+
}
35+
36+
$product['product_reference_suffixes'] = $suffixes;
37+
return $product;
38+
}, $products);
39+
40+
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}};
41+
} catch (Exception $e) {
42+
echo $e->getMessage() . PHP_EOL;
43+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
require __DIR__.'/../vendor/autoload.php';
4+
{{> snippets/import}}
5+
6+
$playlists = [ /* Your records */ ];
7+
8+
try {
9+
{{> snippets/init}}
10+
11+
{{#dynamicSnippet}}saveObjectsPlaylistsWithUserIDPublic{{/dynamicSnippet}};
12+
} catch (Exception $e) {
13+
echo $e->getMessage() . PHP_EOL;
14+
}
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+
6+
try {
7+
{{> snippets/init}}
8+
9+
$results = $client->browseObjects('<YOUR_INDEX_NAME>');
10+
11+
$records = [];
12+
foreach ($results as $hit) {
13+
$records[] = [
14+
'twitterHandle' => $hit['twitterHandle'],
15+
'nbFollowers' => $hit['nbFollowers'],
16+
'isPopular' => $hit['nbFollowers'] > 1000000,
17+
];
18+
}
19+
20+
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}};
21+
} catch (Exception $e) {
22+
echo $e->getMessage() . PHP_EOL;
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
require __DIR__.'/../vendor/autoload.php';
4+
{{> snippets/import}}
5+
6+
try {
7+
{{> snippets/init}}
8+
9+
$dateTimestamp = time();
10+
$searchParams = [
11+
'query' => '<YOUR_SEARCH_QUERY>',
12+
'filters' => "date_timestamp > $dateTimestamp",
13+
];
14+
15+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
16+
} catch (Exception $e) {
17+
echo $e->getMessage() . PHP_EOL;
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
require __DIR__.'/../vendor/autoload.php';
4+
{{> snippets/import}}
5+
6+
$getGoogleAnalyticsUserIdFromBrowserCookie = function (string $cookieName): string {
7+
// Implement your logic here
8+
return '';
9+
};
10+
11+
try {
12+
{{> snippets/init}}
13+
14+
$userToken = $getGoogleAnalyticsUserIdFromBrowserCookie('_ga');
15+
$searchParams = [
16+
'query' => '<YOUR_SEARCH_QUERY>',
17+
'userToken' => $userToken,
18+
];
19+
20+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
21+
22+
/** @var string|null $loggedInUser */
23+
$loggedInUser = null;
24+
25+
$searchParams['userToken'] = $loggedInUser ?? $userToken;
26+
27+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
28+
} catch (Exception $e) {
29+
echo $e->getMessage() . PHP_EOL;
30+
}
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+
6+
$labels = []; // A list of labels
7+
$reduceLabelsToFilters = function (array $labels): array {
8+
// Implement your logic here
9+
return [];
10+
};
11+
12+
try {
13+
{{> snippets/init}}
14+
15+
$optionalFilters = $reduceLabelsToFilters($labels);
16+
$searchParams = [
17+
'query' => '<YOUR_SEARCH_QUERY>',
18+
'optionalFilters' => $optionalFilters,
19+
];
20+
21+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
22+
} catch (Exception $e) {
23+
echo $e->getMessage() . PHP_EOL;
24+
}
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+
6+
$getBuyerAccountId = function (): string {
7+
// Implement your logic here
8+
return '';
9+
};
10+
11+
try {
12+
{{> snippets/init}}
13+
14+
// get the buyer account information
15+
$buyer = $getBuyerAccountId();
16+
$searchParams = [
17+
'query' => '<YOUR_SEARCH_QUERY>',
18+
'ruleContexts' => [$buyer],
19+
];
20+
21+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
22+
} catch (Exception $e) {
23+
echo $e->getMessage() . PHP_EOL;
24+
}
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+
6+
$getPlatformTag = function (): string {
7+
// Implement your logic here
8+
return '';
9+
};
10+
11+
try {
12+
{{> snippets/init}}
13+
14+
// get the buyer account information
15+
$platformTag = $getPlatformTag();
16+
$searchParams = [
17+
'query' => '<YOUR_SEARCH_QUERY>',
18+
'ruleContexts' => [$platformTag],
19+
];
20+
21+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}};
22+
} catch (Exception $e) {
23+
echo $e->getMessage() . PHP_EOL;
24+
}

0 commit comments

Comments
 (0)