-
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
4 changed files
with
125 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
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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Usox\TalI18nExtract\Extractors; | ||
|
||
use DOMNode; | ||
use DOMNodeList; | ||
use DOMXPath; | ||
use Generator; | ||
|
||
final class I18nAttributeExtractor implements ExtractorInterface | ||
{ | ||
/** | ||
* @return Generator<string> | ||
*/ | ||
public function extract(DOMXPath $xpath): Generator | ||
{ | ||
$result = $xpath->query('//*[@i18n:attributes]'); | ||
|
||
if ($result instanceof DOMNodeList) { | ||
/** @var DOMNode $item */ | ||
foreach ($result as $item) { | ||
$node = $item->attributes?->getNamedItemNS('http://xml.zope.org/namespaces/i18n', 'attributes'); | ||
|
||
if ($node === null) { | ||
continue; | ||
} | ||
|
||
$tuples = preg_split('/;/', (string) $node->nodeValue); | ||
if ($tuples === false) { | ||
continue; | ||
} | ||
|
||
foreach ($tuples as $tuple) { | ||
$tuple = trim($tuple); | ||
$attribute = preg_split('/\s/', $tuple); | ||
|
||
if ($attribute === false) { | ||
continue; | ||
} | ||
|
||
if (count($attribute) === 2) { | ||
[$_, $translationKey] = $attribute; | ||
|
||
yield $translationKey; | ||
} else { | ||
yield (string) $item->attributes?->getNamedItem($tuple)?->nodeValue; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Usox\TalI18nExtract\Extractors; | ||
|
||
use DOMDocument; | ||
use DOMXPath; | ||
use Generator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class I18nAttributeExtractorTest extends TestCase | ||
{ | ||
private I18nAttributeExtractor $subject; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->subject = new I18nAttributeExtractor(); | ||
} | ||
|
||
#[DataProvider(methodName: 'translationKeyDataProvider')] | ||
public function testExtractExtractsKeys(string $html, array $expectedKeys): void | ||
{ | ||
$body = <<<HTML | ||
<html xmlns:i18n="http://xml.zope.org/namespaces/i18n">%s</html> | ||
HTML; | ||
|
||
$dom = new DOMDocument(); | ||
$dom->loadXML(sprintf($body, $html)); | ||
|
||
$xpath = new DOMXPath($dom); | ||
$xpath->registerNamespace('i18n', 'http://xml.zope.org/namespaces/i18n'); | ||
|
||
static::assertSame( | ||
$expectedKeys, | ||
iterator_to_array( | ||
$this->subject->extract($xpath) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @return Generator<array{string, list<string>}> | ||
*/ | ||
public static function translationKeyDataProvider(): Generator | ||
{ | ||
yield [ | ||
'<div i18n:translate="">snafu</div>', | ||
[], | ||
]; | ||
|
||
yield [ | ||
'<div i18n:attributes="alt snafu">foobar</div>', | ||
['snafu'], | ||
]; | ||
|
||
yield [ | ||
'<img i18n:attributes="alt" alt="snafu">foobar</img>', | ||
['snafu'], | ||
]; | ||
|
||
yield [ | ||
'<img i18n:attributes="alt; title" title="bar" alt="baz">foobar</img>', | ||
['baz', 'bar'], | ||
]; | ||
} | ||
} |