1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD\Infrastructure\Repository\CharacterRepository\XMLRepository\ElementParser; |
4
|
|
|
|
5
|
|
|
use UCD\Unicode\Codepoint; |
6
|
|
|
use UCD\Unicode\CodepointAssigned; |
7
|
|
|
use UCD\Exception\RuntimeException; |
8
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\XMLRepository\ElementParser; |
9
|
|
|
|
10
|
|
|
class CodepointAssignedParser implements ElementParser |
11
|
|
|
{ |
12
|
|
|
const TAG_NAME_CHAR = 'char'; |
13
|
|
|
const TAG_NAME_NON_CHAR = 'noncharacter'; |
14
|
|
|
const TAG_NAME_SURROGATE = 'surrogate'; |
15
|
|
|
|
16
|
|
|
const ATTR_CODEPOINT = 'cp'; |
17
|
|
|
const ATTR_CODEPOINT_FIRST = 'first-cp'; |
18
|
|
|
const ATTR_CODEPOINT_LAST = 'last-cp'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var CharacterParser |
22
|
|
|
*/ |
23
|
|
|
private $characterParser; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var NonCharacterParser |
27
|
|
|
*/ |
28
|
|
|
private $nonCharacterParser; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var SurrogateParser |
32
|
|
|
*/ |
33
|
|
|
private $surrogateParser; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param CharacterParser $characterParser |
37
|
|
|
* @param NonCharacterParser $nonCharacterParser |
38
|
|
|
* @param SurrogateParser $surrogateParser |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
CharacterParser $characterParser, |
42
|
|
|
NonCharacterParser $nonCharacterParser, |
43
|
|
|
SurrogateParser $surrogateParser |
44
|
|
|
) { |
45
|
|
|
$this->characterParser = $characterParser; |
46
|
|
|
$this->nonCharacterParser = $nonCharacterParser; |
47
|
|
|
$this->surrogateParser = $surrogateParser; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \DOMElement $element |
52
|
|
|
* @return CodepointAssigned[] |
53
|
|
|
*/ |
54
|
|
|
public function parseElement(\DOMElement $element) |
55
|
|
|
{ |
56
|
|
|
$parser = $this->getParserForElement($element); |
57
|
|
|
$codepointValues = $this->extractCodepoints($element); |
58
|
|
|
|
59
|
|
|
foreach ($codepointValues as $codepointValue) { |
60
|
|
|
$codepoint = Codepoint::fromInt($codepointValue); |
61
|
|
|
yield $parser->parseElement($element, $codepoint); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \DOMElement $element |
67
|
|
|
* @return CodepointAwareParser |
68
|
|
|
* @throws RuntimeException |
69
|
|
|
*/ |
70
|
|
|
private function getParserForElement(\DOMElement $element) |
71
|
|
|
{ |
72
|
|
|
if ($element->tagName === self::TAG_NAME_CHAR) { |
73
|
|
|
return $this->characterParser; |
74
|
|
|
} elseif ($element->tagName === self::TAG_NAME_NON_CHAR) { |
75
|
|
|
return $this->nonCharacterParser; |
76
|
|
|
} elseif ($element->tagName === self::TAG_NAME_SURROGATE) { |
77
|
|
|
return $this->surrogateParser; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
throw new RuntimeException(sprintf('No parser found for %s', $element->tagName)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param \DOMElement $element |
85
|
|
|
* @return \int[] |
86
|
|
|
*/ |
87
|
|
|
private function extractCodepoints(\DOMElement $element) |
88
|
|
|
{ |
89
|
|
|
if ($element->hasAttribute(self::ATTR_CODEPOINT)) { |
90
|
|
|
$first = hexdec($element->getAttribute(self::ATTR_CODEPOINT)); |
91
|
|
|
$last = $first; |
92
|
|
|
} else { |
93
|
|
|
$first = hexdec($element->getAttribute(self::ATTR_CODEPOINT_FIRST)); |
94
|
|
|
$last = hexdec($element->getAttribute(self::ATTR_CODEPOINT_LAST)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return range($first, $last); |
98
|
|
|
} |
99
|
|
|
} |