1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD\Infrastructure\Repository\CharacterRepository; |
4
|
|
|
|
5
|
|
|
use UCD\Unicode\Character\Collection; |
6
|
|
|
use UCD\Unicode\Codepoint; |
7
|
|
|
use UCD\Unicode\Character\Repository; |
8
|
|
|
use UCD\Unicode\CodepointAssigned; |
9
|
|
|
|
10
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\XMLRepository\ElementParser; |
11
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\XMLRepository\ElementParser\CodepointAssignedParser; |
12
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\XMLRepository\ElementParser\CodepointCountParser; |
13
|
|
|
use UCD\Infrastructure\Repository\CharacterRepository\XMLRepository\CodepointElementReader; |
14
|
|
|
|
15
|
|
|
class XMLRepository implements Repository |
16
|
|
|
{ |
17
|
|
|
use Repository\Capability\CodepointSearchByIteration; |
18
|
|
|
use Repository\Capability\PropertySearchByIteration; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var CodepointElementReader |
22
|
|
|
*/ |
23
|
|
|
private $elementReader; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var CodepointAssignedParser |
27
|
|
|
*/ |
28
|
|
|
private $elementParser; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var CodepointCountParser |
32
|
|
|
*/ |
33
|
|
|
private $countParser; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param CodepointElementReader $characterReader |
37
|
|
|
* @param CodepointAssignedParser $characterParser |
38
|
|
|
* @param CodepointCountParser $codepointParser |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
CodepointElementReader $characterReader, |
42
|
|
|
CodepointAssignedParser $characterParser, |
43
|
|
|
CodepointCountParser $codepointParser |
44
|
|
|
) { |
45
|
|
|
$this->elementReader = $characterReader; |
46
|
|
|
$this->elementParser = $characterParser; |
47
|
|
|
$this->countParser = $codepointParser; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
*/ |
53
|
|
|
public function getAll() |
54
|
|
|
{ |
55
|
|
|
return new Collection( |
56
|
|
|
$this->readAll() |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return \Generator |
62
|
|
|
*/ |
63
|
|
|
private function readAll() |
64
|
|
|
{ |
65
|
|
|
foreach ($this->elementReader->read() as $element) { |
66
|
|
|
$characters = $this->parseElementForCharacters($element); |
67
|
|
|
|
68
|
|
|
foreach ($characters as $character) { |
69
|
|
|
$codepoint = $character->getCodepoint(); |
70
|
|
|
yield $codepoint->getValue() => $character; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param \DOMElement $element |
77
|
|
|
* @return CodepointAssigned[] |
78
|
|
|
*/ |
79
|
|
|
private function parseElementForCharacters(\DOMElement $element) |
80
|
|
|
{ |
81
|
|
|
return $this->elementParser->parseElement($element); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
public function count() |
88
|
|
|
{ |
89
|
|
|
$tally = 0; |
90
|
|
|
|
91
|
|
|
foreach ($this->elementReader->read() as $element) { |
92
|
|
|
$tally += $this->parseElementForCharacterCount($element); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $tally; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param \DOMElement $element |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
|
|
private function parseElementForCharacterCount(\DOMElement $element) |
103
|
|
|
{ |
104
|
|
|
return $this->countParser->parseElement($element); |
105
|
|
|
} |
106
|
|
|
} |