Skip to content

docs(php): Add remaining guides #699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions website/docs/clients/guides/copy-or-move-index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ const { taskID } = await client.operationIndex({
await client.waitForTask({ indexName: '<SOURCE_INDEX_NAME>', taskID });
```

</TabItem>
<TabItem value="php">

```php
$client = SearchClient::create(
'<YOUR_APP_ID>',
'<YOUR_APP_ID>'
);

$response = $client->operationIndex(
'<SOURCE_INDEX_NAME>',
[
'operation' => 'copy',
'destination' => '<DESTINATION_INDEX_NAME>',
]
);

// Poll the task status with defaults values
$client->waitForTask('<SOURCE_INDEX_NAME>', $response['taskID']);
```

</TabItem>
<TabItem value="java">

Expand Down Expand Up @@ -79,6 +100,27 @@ const { taskID } = await client.operationIndex({
await client.waitForTask({ indexName: '<SOURCE_INDEX_NAME>', taskID });
```

</TabItem>
<TabItem value="php">

```php
$client = SearchClient::create(
'<YOUR_APP_ID>',
'<YOUR_APP_ID>'
);

$response = $client->operationIndex(
'<SOURCE_INDEX_NAME>',
[
'operation' => 'move',
'destination' => '<DESTINATION_INDEX_NAME>',
]
);

// Poll the task status with defaults values
$client->waitForTask('<SOURCE_INDEX_NAME>', $response['taskID']);
```

</TabItem>
<TabItem value="java">

Expand Down Expand Up @@ -126,6 +168,28 @@ const { taskID } = await client.operationIndex({
await client.waitForTask({ indexName: '<SOURCE_INDEX_NAME>', taskID });
```

</TabItem>
<TabItem value="php">

```php
$client = SearchClient::create(
'<YOUR_APP_ID>',
'<YOUR_APP_ID>'
);

$response = $client->operationIndex(
'<SOURCE_INDEX_NAME>',
[
'operation' => 'move',
'destination' => '<DESTINATION_INDEX_NAME>',
'scope' => ['rules'],
]
);

// Poll the task status with defaults values
$client->waitForTask('<SOURCE_INDEX_NAME>', $response['taskID']);
```

</TabItem>
<TabItem value="java">

Expand Down
63 changes: 63 additions & 0 deletions website/docs/clients/guides/customized-client-usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ await client.search(
);
```

</TabItem>
<TabItem value="php">

```php
$client->search(
[
'requests' => [
[
'indexName' => '<YOUR_INDEX_NAME>',
'query' => '<YOUR_QUERY>',
'facets' => ['author', 'genre'],
],
]
],
[
// This header is added to the request
'headers' => ['additional-header' => 'hello'],
// As we re-define `hitsPerPage`, it will override the value defined in the method's parameters.
'queryParameters' => ['hitsPerPage' => 100],
]
);
```

</TabItem>
<TabItem value="java">

Expand Down Expand Up @@ -86,7 +109,23 @@ client.addAlgoliaAgent('my user agent', 'optional version');
```

</TabItem>
<TabItem value="php">

```php
$client = SearchClient::create(
'<YOUR_APP_ID>',
'<YOUR_APP_ID>'
);

// This will append `; my user agent (optional version)` to the current value of `x-algolia-agent` for every requests
Algolia\AlgoliaSearch\Support\AlgoliaAgent::addCustomAlgoliaAgent(
$client->getClientConfig()->getClientName(),
"my user agent",
"optional version"
);
```

</TabItem>
<TabItem value="java">

```java
Expand Down Expand Up @@ -124,7 +163,31 @@ const client = searchClient('<YOUR_APP_ID>', '<YOUR_API_KEY>', {
```

</TabItem>
<TabItem value="php">

> In PHP, the requests are handled by the HTTP Client (by default GuzzleHttpClient)

```php
use Algolia\AlgoliaSearch\Api\SearchClient;
use Algolia\AlgoliaSearch\Configuration\SearchConfig;
use Algolia\AlgoliaSearch\Http\HttpClientInterface;
use Algolia\AlgoliaSearch\RetryStrategy\ApiWrapper;
use Algolia\AlgoliaSearch\RetryStrategy\ClusterHosts;

// Create a config and a clusterhosts objects with your credentials
$config = SearchConfig::create('<YOUR_APP_ID>', '<YOUR_API_KEY>');
$clusterHosts = ClusterHosts::createFromAppId('<YOUR_APP_ID>');

// Instanciate a MyHttpClient object implementing HttpClientInterface (replacing GuzzleHttpClient)
$myHttpClient = new MyHttpClient(...);
// Now create you custom Api wrapper
$apiWrapper = new ApiWrapper($myHttpClient, $config, $clusterHosts);

// Instanciate your client using the constructor
$client = new SearchClient($apiWrapper, $config);
```

</TabItem>
<TabItem value="java">

> Create your own requester by creating a class that implements `com.algolia.utils.Requester`, and use it by passing it to the client constructor.
Expand Down
35 changes: 35 additions & 0 deletions website/docs/clients/guides/wait-for-a-task-to-finish.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,41 @@ await client.waitForTask({
});
```

</TabItem>
<TabItem value="php">

```php
$client = SearchClient::create(
'<YOUR_APP_ID>',
'<YOUR_APP_ID>'
);

$response = $client->saveObject(
'<YOUR_INDEX_NAME>',
['title' => "My Algolia Object"],
$requestOptions
);

// Poll the task status with defaults values
$client->waitForTask('<YOUR_INDEX_NAME>', $response['taskID']);

// Poll the task status with your options
Algolia\AlgoliaSearch\Support\Helpers::retryUntil(
$client,
'getTask',
['<YOUR_INDEX_NAME>', $response['taskID'], $requestOptions],
function ($res) {
return 'published' === $res['status'];
},
100, // Number of maximum retries to do
5000, // Default timeout
// Calculation of the time to wait between tries
function ($timeout, $retryCount) {
return min($retryCount * 200, $timeout) * 1000;
}
);
```

</TabItem>
<TabItem value="java">

Expand Down
39 changes: 39 additions & 0 deletions website/docs/clients/migration-guides/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ const { taskID } = await client.saveObject({
await client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID });
```

</TabItem>
<TabItem value="php">

```php
$client = SearchClient::create(
'<YOUR_APP_ID>',
'<YOUR_APP_ID>'
);

$response = $client->saveObject(
'<YOUR_INDEX_NAME>',
['title' => "My Algolia Object"],
);

// Poll the task status with defaults values
$client->waitForTask('<YOUR_INDEX_NAME>', $response['taskID']);
```

</TabItem>
<TabItem value="java">

Expand Down Expand Up @@ -190,6 +208,27 @@ const { taskID } = await client.operationIndex({
await client.waitForTask({ indexName: '<SOURCE_INDEX_NAME>', taskID });
```

</TabItem>
<TabItem value="php">

```php
$client = SearchClient::create(
'<YOUR_APP_ID>',
'<YOUR_APP_ID>'
);

$response = $client->operationIndex(
'<SOURCE_INDEX_NAME>',
[
'operation' => 'copy', // 'move'
'destination' => '<DESTINATION_INDEX_NAME>'
]
);

// Poll the task status until it's done
$client->waitForTask('<SOURCE_INDEX_NAME>', $response['taskID']);
```

</TabItem>
<TabItem value="java">

Expand Down
52 changes: 51 additions & 1 deletion website/docs/clients/migration-guides/php.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,54 @@
title: PHP
---

WIP
| Previous | Latest | Description |
| -------------------- | :------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `"algolia/algoliasearch-client-php": "^3.2"` | `"algolia/algoliasearch-client-php": "^4.0"` | **During the beta phase**, the clients are available under the package 4.x.x-alpha , you can find a full list [here](https://packagist.org/packages/algolia/algoliasearch-client-php). |
| `Algolia\AlgoliaSearch` | `Algolia\AlgoliaSearch\Api` | Exported clients have now the name suffixed by `Api`. |

### Usage

To get started, first uninstall the previously added clients.

```bash
composer remove algolia/algoliasearch-client-php
```

You can now install the `Algoliasearch` clients.

```bash
composer require algolia/algoliasearch-client-php "^4.0"
```

You can continue this guide on [our installation page](/docs/clients/installation).

### Methods targeting an `indexName`

Prior to the `initIndex` removal stated in the [common breaking changes](/docs/clients/migration-guides/#common-breaking-changes), all methods previously available at the `initIndex` level requires the `indexName` to be sent with the query.

```php
use Algolia\AlgoliaSearch\Api\SearchClient;

$client = SearchClient::create(
'<YOUR_APP_ID>',
'<YOUR_API_KEY>'
);

// only query string
$searchResults = $client->search([
'requests' => [
['indexName' => '<YOUR_INDEX_NAME>', 'query' =>'<YOUR_QUERY>'],
],
]);

$searchResults2 = $client->search([
'requests' => [
[
'indexName' => '<YOUR_INDEX_NAME>',
'query' => '<YOUR_QUERY>',
'attributesToRetrieve' => ['firstname', 'lastname'],
'hitsPerPage' => 50,
],
],
]);
```