Skip to content

Commit

Permalink
Processors: added EntityProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed Apr 18, 2024
1 parent 0d49b5f commit c7f0b94
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/Processors/EntityProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Inteve\Translator\Processors;

use Inteve\Translator\Ast;
use Inteve\Translator\MessageProcessor;


class EntityProcessor implements MessageProcessor
{
/** @var MessageProcessor|NULL */
private $textProcessor;


public function __construct(MessageProcessor $textProcessor = NULL)
{
$this->textProcessor = $textProcessor;
}


public function processMessage($messageText)
{
if ($this->textProcessor === NULL) {
return new Ast\MessageText([$this->decodeEntities($messageText)]);
}

$messageText = $this->textProcessor->processMessage($messageText);
$parts = [];

foreach ($messageText->getChildren() as $child) {
if (is_string($child)) {
if ($child !== '') {
$parts[] = $this->decodeEntities($child);
}

} else {
$parts[] = $child;
}
}

return new Ast\MessageText($parts);
}


/**
* @param non-empty-string $messageText
* @return non-empty-string
*/
private function decodeEntities($messageText)
{
$messageText = html_entity_decode($messageText, ENT_QUOTES | ENT_SUBSTITUTE | ENT_XHTML, 'UTF-8');
// ENT_XHTML - allows 'all' entities (like ENT_XML) + &apos;

if ($messageText === '') {
throw new \Inteve\Translator\InvalidStateException('Decoded string is empty.');
}

return $messageText;
}
}
2 changes: 1 addition & 1 deletion src/Processors/TagProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
array $emptyElements = ['br']
)
{
$this->textPartsProcessor = $textPartsProcessor !== NULL ? $textPartsProcessor : new ParametersProcessor;
$this->textPartsProcessor = $textPartsProcessor !== NULL ? $textPartsProcessor : new EntityProcessor(new ParametersProcessor);
$this->emptyElements = [];

foreach ($emptyElements as $emptyElement) {
Expand Down
30 changes: 30 additions & 0 deletions tests/Translator/EntityProcessor.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Inteve\Translator\Ast;
use Inteve\Translator\Processors\EntityProcessor;
use Inteve\Translator\Processors\ParametersProcessor;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

test('Only text', function () {
$processor = new EntityProcessor;
Assert::equal(new Ast\MessageText(['lorem ipsum']), $processor->processMessage('lorem ipsum'));
});


test('Named & codeded entity', function () {
$processor = new EntityProcessor;
Assert::equal(new Ast\MessageText([
"lorem \xC2\xA0 \" ' ' \r \x20",
]), $processor->processMessage('lorem &nbsp; &#34; &#39; &apos; &#13; &#32;'));
});


test('With parameters', function () {
$processor = new EntityProcessor(new ParametersProcessor);
Assert::equal(new Ast\MessageText([
'{$test}',
new Ast\Parameter('name'),
]), $processor->processMessage('&#x7B;&#x24;test}{$name}'));
});
7 changes: 7 additions & 0 deletions tests/Translator/HtmlTranslator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function createTranslator($lang)
'homepage.hello' => 'Hello, <b>{$name}</b>!',
'homepage.unknow' => 'Hello, <muted>lorem <i>{$name|lower}!</i></muted>',
'homepage.br' => 'Hello<br>friends',
'homepage.entity' => 'A&nbsp;entity',
]),
]),
new Translator\Processors\TagProcessor
Expand Down Expand Up @@ -61,6 +62,12 @@ test('MessageId', function () {
});


test('Entities', function () {
$translator = createTranslator('en');
Assert::same("A\xC2\xA0entity", (string) $translator->translate('homepage.entity'));
});


test('Prefix', function () {
$translator = createTranslator('en')->prefix('homepage');
Assert::same('Hello, <b>John</b>!', (string) $translator->translate('hello', ['name' => 'John']));
Expand Down
7 changes: 7 additions & 0 deletions tests/Translator/TextTranslator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function createTranslator($lang)
'homepage.hello' => 'Hello!',
'homepage.hello2' => 'Hello, <b>{$name}</b>!',
'homepage.br' => 'Hello<br>friends',
'homepage.entity' => 'A&nbsp;entity',
]),
]),
new Translator\Processors\TagProcessor
Expand Down Expand Up @@ -76,6 +77,12 @@ test('MessageId', function () {
});


test('Entities', function () {
$translator = createTranslator('en');
Assert::same("A\xC2\xA0entity", (string) $translator->translate('homepage.entity'));
});


test('Prefix', function () {
$translator = createTranslator('en')->prefix('homepage');
Assert::same('Hello, John!', $translator->translate('hello2', ['name' => 'John']));
Expand Down

0 comments on commit c7f0b94

Please sign in to comment.