1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD\Infrastructure\Repository\CharacterRepository\XMLRepository\ElementParser\Properties; |
4
|
|
|
|
5
|
|
|
use UCD\Unicode\Codepoint; |
6
|
|
|
use UCD\Exception\UnexpectedValueException; |
7
|
|
|
|
8
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\XMLRepository\ElementParser; |
9
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\XMLRepository\ElementParser\CodepointAwareParser; |
10
|
|
|
|
11
|
|
|
abstract class BaseParser implements CodepointAwareParser |
12
|
|
|
{ |
13
|
|
|
const ATTRIBUTE_VALUE_YES = 'Y'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \DOMElement |
17
|
|
|
*/ |
18
|
|
|
protected $element; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Codepoint |
22
|
|
|
*/ |
23
|
|
|
protected $codepoint; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return mixed |
27
|
|
|
*/ |
28
|
|
|
abstract protected function parse(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param \DOMElement $element |
32
|
|
|
* @param Codepoint $codepoint |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
|
|
public function parseElement(\DOMElement $element, Codepoint $codepoint) |
36
|
|
|
{ |
37
|
|
|
$this->element = $element; |
38
|
|
|
$this->codepoint = $codepoint; |
39
|
|
|
|
40
|
|
|
return $this->parse(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $name |
45
|
|
|
* @return string|null |
46
|
|
|
*/ |
47
|
|
|
protected function getOptionalAttribute($name) |
48
|
|
|
{ |
49
|
|
|
$attribute = $this->element->getAttribute($name); |
50
|
|
|
|
51
|
|
|
if ($attribute === '') { |
52
|
|
|
return null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $attribute; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $name |
60
|
|
|
* @return string |
61
|
|
|
* @throws UnexpectedValueException |
62
|
|
|
*/ |
63
|
|
|
protected function getAttribute($name) |
64
|
|
|
{ |
65
|
|
|
$attribute = $this->element->getAttribute($name); |
66
|
|
|
|
67
|
|
|
if ($attribute === '') { |
68
|
|
|
throw new UnexpectedValueException(sprintf('Missing attribute "%s"', $name)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $attribute; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $name |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
protected function getBoolAttribute($name) |
79
|
|
|
{ |
80
|
|
|
return $this->getAttribute($name) === self::ATTRIBUTE_VALUE_YES; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $value |
85
|
|
|
* @param Codepoint $codepoint |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
protected function parsePlaceholders($value, Codepoint $codepoint) |
89
|
|
|
{ |
90
|
|
|
if ($value === null) { |
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$hexCodepoint = sprintf('%X', $codepoint->getValue()); |
95
|
|
|
|
96
|
|
|
return str_replace('#', $hexCodepoint, $value); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $list |
101
|
|
|
* @return Codepoint[] |
102
|
|
|
*/ |
103
|
|
|
protected function parseCodepointList($list) |
104
|
|
|
{ |
105
|
|
|
$mapper = function ($codepointValue) { |
106
|
|
|
return Codepoint::fromHex($codepointValue); |
107
|
|
|
}; |
108
|
|
|
|
109
|
|
|
return array_map($mapper, explode(' ', $list)); |
110
|
|
|
} |
111
|
|
|
} |