Skip to content

Commit

Permalink
Use PlaceholdersBehavior to extract placeholders from translated fiel…
Browse files Browse the repository at this point in the history
…ds (#104)

* Use placeholders regex instead of its offest for translated fields

* Update placeholders test

* Update placeholders tests

* Fix placeholder test (again)

* Fix placeholder test (again again)

* Use PlaceholdersBehavior extractPlaceholders method

* Sort dependencies

* Update placeholders mock data
  • Loading branch information
edoardocavazza authored Nov 6, 2024
1 parent 82790bf commit 9863f5a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
"php": ">=8.1",
"ext-intl": "*",
"ext-json": "*",
"bedita/core": "^5.0.0",
"bedita/core": "^5.14.0",
"bedita/i18n": "^4.0",
"cakephp/cakephp": "^4.0",
"cakephp/authentication": "^2.0",
"cakephp/twig-view": "^1.3",
"chialab/ip": "^1.0"
},
"require-dev": {
"bedita/placeholders": "^2.0",
"cakephp/cakephp-codesniffer": "^5.0",
"cakephp/plugin-installer": "^1.0",
"phpunit/phpunit": "~8.5.0 || ^9.3"
},
"suggest": {
"bedita/placeholders": "To manage placeholders in templates",
"chialab/rna-cakephp": "To manage JavaScript and CSS bundles with RNA"
},
"autoload": {
Expand Down
19 changes: 14 additions & 5 deletions src/View/Helper/PlaceholdersHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
namespace Chialab\FrontendKit\View\Helper;

use BEdita\Core\Model\Table\ObjectTypesTable;
use BEdita\Placeholders\Model\Behavior\PlaceholdersBehavior;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\Utility\Hash;
use Cake\View\Helper;
use InvalidArgumentException;
use Iterator;
Expand Down Expand Up @@ -103,15 +103,24 @@ public static function defaultTemplater(EntityInterface $entity, string $field,
}

$deltas = [];
$extracted = PlaceholdersBehavior::extractPlaceholders($entity, [$field]);
$getInfo = function (array $extracted, int $id, string $field): array {
foreach ($extracted as $it) {
if ($it['id'] === $id) {
return $it['params'][$field] ?? [];
}
}

return [];
};

foreach ($placeholders as $placeholder) {
$info = Hash::get($placeholder, ['relation', 'params', $field], []);
$info = $getInfo($extracted, $placeholder['id'], $field);
foreach ($info as $i) {
$offset = $i['offset'];
$delta = array_sum(array_filter(
$deltas,
function (int $pos) use ($offset): bool {
return $pos < $offset;
},
fn (int $pos): bool => $pos < $offset,
ARRAY_FILTER_USE_KEY
));
$length = $i['length'];
Expand Down
14 changes: 6 additions & 8 deletions tests/TestCase/View/Helper/PlaceholdersHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function setUp(): void
'params' => [
'body' => [[
'offset' => 15,
'length' => 18,
'length' => 25,
]],
],
],
Expand All @@ -88,11 +88,9 @@ public function setUp(): void
'relation' => [
'params' => [
'body' => [[
'offset' => 37,
'length' => 18,
'params' => [
'class' => 'test',
],
'offset' => 44,
'length' => 25,
'params' => '{"class":"test"}',
]],
],
],
Expand All @@ -101,7 +99,7 @@ public function setUp(): void
$this->object = new ObjectEntity([
'id' => 1,
'type' => 'objects',
'body' => '<p>Hello World <!-- TEMPLATE1 --></p><!-- TEMPLATE2 -->',
'body' => '<p>Hello World <!-- BE-PLACEHOLDER.2 --></p><!-- BE-PLACEHOLDER.3.eyJjbGFzcyI6InRlc3QifQ== -->',
'placeholder' => [$this->image1, $this->image2],
]);
}
Expand Down Expand Up @@ -152,7 +150,7 @@ public function testGetTemplate()
*/
public function testDefaultTemplater()
{
$contents = $this->Placeholders::defaultTemplater($this->object, 'body', [$this->image1, $this->image2], fn ($entity, $params) => sprintf('%s %s', $entity->title, $params ? json_encode($params) : ''));
$contents = $this->Placeholders::defaultTemplater($this->object, 'body', [$this->image1, $this->image2], fn ($entity, $params) => sprintf('%s %s', $entity->title, $params ? $params : ''));
$this->assertSame('<p>Hello World image1 </p>image2 {"class":"test"}', $contents);
}

Expand Down

0 comments on commit 9863f5a

Please sign in to comment.