Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,9 @@ patch_dynamic_search_rule_1: |-
]);
delete_dynamic_search_rule_1: |-
$client->deleteDynamicSearchRule('RULE_UID');
render_template_1: |-
$client->renderTemplate(
(new TemplateRenderQuery())
->setTemplate('inlineDocumentTemplate', '{{ doc.breed }} called {{ doc.name }}')
->setInput('inlineDocument', ['breed' => 'Jack Russell', 'name' => 'Iko'])
);
Comment thread
Strift marked this conversation as resolved.
4 changes: 4 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Meilisearch\Endpoints\Delegates\HandlesSnapshots;
use Meilisearch\Endpoints\Delegates\HandlesSystem;
use Meilisearch\Endpoints\Delegates\HandlesTasks;
use Meilisearch\Endpoints\Delegates\HandlesTemplates;
use Meilisearch\Endpoints\Dumps;
use Meilisearch\Endpoints\DynamicSearchRules;
use Meilisearch\Endpoints\Health;
Expand All @@ -26,6 +27,7 @@
use Meilisearch\Endpoints\Snapshots;
use Meilisearch\Endpoints\Stats;
use Meilisearch\Endpoints\Tasks;
use Meilisearch\Endpoints\Templates;
use Meilisearch\Endpoints\TenantToken;
use Meilisearch\Endpoints\Version;
use Meilisearch\Http\Client as MeilisearchClientAdapter;
Expand All @@ -46,6 +48,7 @@ class Client
use HandlesBatches;
use HandlesNetwork;
use HandlesDynamicSearchRules;
use HandlesTemplates;

/**
* @param array<int, string> $clientAgents
Expand All @@ -72,5 +75,6 @@ public function __construct(
$this->tenantToken = new TenantToken($this->http, $apiKey);
$this->network = new Network($this->http);
$this->dynamicSearchRules = new DynamicSearchRules($this->http);
$this->templates = new Templates($this->http);
}
}
55 changes: 55 additions & 0 deletions src/Contracts/TemplateRenderQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Contracts;

class TemplateRenderQuery
{
/**
* @var array{kind: string, inline: string}
*/
private array $template = ['kind' => '', 'inline' => ''];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure that it's useful to initialize this query with a wrong state, because kind is required right?
i think this query should accept both params via constructor


/**
* @var array{kind: string, inline: mixed}
*/
private array $input = ['kind' => '', 'inline' => null];
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

/**
* Set the template to render.
*
* @param string $kind e.g. 'inlineDocumentTemplate'
* @param string $inline the template string
*/
public function setTemplate(string $kind, string $inline): self
{
$this->template = ['kind' => $kind, 'inline' => $inline];

return $this;
}

/**
* Set the input document for template rendering.
*
* @param string $kind e.g. 'inlineDocument'
* @param array|object|null $inline the document data
*/
public function setInput(string $kind, mixed $inline): self
{
$this->input = ['kind' => $kind, 'inline' => $inline];

return $this;
}

/**
* @return array{template: array{kind: string, inline: string}, input: array{kind: string, inline: mixed}}
*/
public function toArray(): array
{
return [
'template' => $this->template,
'input' => $this->input,
];
}
}
30 changes: 30 additions & 0 deletions src/Contracts/TemplateRenderResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Contracts;

class TemplateRenderResult
{
private string $template;
private ?string $rendered;

/**
* @param array{template: string, rendered: string|null} $data
*/
public function __construct(array $data)
{
$this->template = $data['template'];
$this->rendered = $data['rendered'];
}

public function getTemplate(): string
{
return $this->template;
}

public function getRendered(): ?string
{
return $this->rendered;
}
}
19 changes: 19 additions & 0 deletions src/Endpoints/Delegates/HandlesTemplates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Endpoints\Delegates;

use Meilisearch\Contracts\TemplateRenderQuery;
use Meilisearch\Contracts\TemplateRenderResult;
use Meilisearch\Endpoints\Templates;

trait HandlesTemplates
{
protected Templates $templates;

public function renderTemplate(TemplateRenderQuery $query): TemplateRenderResult
{
return $this->templates->render($query);
}
}
21 changes: 21 additions & 0 deletions src/Endpoints/Templates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Meilisearch\Endpoints;

use Meilisearch\Contracts\Endpoint;
use Meilisearch\Contracts\TemplateRenderQuery;
use Meilisearch\Contracts\TemplateRenderResult;

final class Templates extends Endpoint
{
protected const PATH = '/render-template';

public function render(TemplateRenderQuery $query): TemplateRenderResult
{
$response = $this->http->post(self::PATH, $query->toArray());

return new TemplateRenderResult($response);
}
}
32 changes: 32 additions & 0 deletions tests/Endpoints/TemplatesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Tests\Endpoints;

use Meilisearch\Contracts\TemplateRenderQuery;
use Meilisearch\Http\Client;
use Tests\TestCase;

final class TemplatesTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

$http = new Client($this->host, getenv('MEILISEARCH_API_KEY'));
$http->patch('/experimental-features', ['renderRoute' => true]);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

public function testCanRenderInlineTemplate(): void
{
$query = (new TemplateRenderQuery())
->setTemplate('inlineDocumentTemplate', '{{ doc.breed }} called {{ doc.name }}')
->setInput('inlineDocument', ['breed' => 'Jack Russell', 'name' => 'Iko']);

$response = $this->client->renderTemplate($query);

self::assertSame('{{ doc.breed }} called {{ doc.name }}', $response->getTemplate());
self::assertSame('Jack Russell called Iko', $response->getRendered());
}
}
Loading