-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) + ' | ||
|
||
if ($messageText === '') { | ||
throw new \Inteve\Translator\InvalidStateException('Decoded string is empty.'); | ||
} | ||
|
||
return $messageText; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 " ' '  ')); | ||
}); | ||
|
||
|
||
test('With parameters', function () { | ||
$processor = new EntityProcessor(new ParametersProcessor); | ||
Assert::equal(new Ast\MessageText([ | ||
'{$test}', | ||
new Ast\Parameter('name'), | ||
]), $processor->processMessage('{$test}{$name}')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters