1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD\Unicode\Codepoint; |
4
|
|
|
|
5
|
|
|
use UCD\Unicode\Codepoint; |
6
|
|
|
use UCD\Unicode\Codepoint\Range; |
7
|
|
|
use UCD\Exception\UnexpectedValueException; |
8
|
|
|
|
9
|
|
|
class Aggregator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Range[] |
13
|
|
|
*/ |
14
|
|
|
private $aggregated = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Codepoint|null |
18
|
|
|
*/ |
19
|
|
|
private $previous; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Codepoint|null |
23
|
|
|
*/ |
24
|
|
|
private $rangeStart; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Codepoint $codepoint |
28
|
|
|
*/ |
29
|
|
|
public function addCodepoint(Codepoint $codepoint) |
30
|
|
|
{ |
31
|
|
|
$shouldCloseCurrentRange = $this->shouldCloseCurrentRangeWith($codepoint); |
32
|
|
|
$shouldOpenNewRange = !$this->hasOpenRange() || $shouldCloseCurrentRange; |
33
|
|
|
|
34
|
|
|
if ($shouldCloseCurrentRange) { |
35
|
|
|
$this->closeCurrentRange(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($shouldOpenNewRange) { |
39
|
|
|
$this->openNewRangeWith($codepoint); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->previous = $codepoint; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
private function hasOpenRange() |
49
|
|
|
{ |
50
|
|
|
return $this->previous !== null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Codepoint $codepoint |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
private function shouldCloseCurrentRangeWith(Codepoint $codepoint) |
58
|
|
|
{ |
59
|
|
|
return $this->hasOpenRange() |
60
|
|
|
&& !$this->isPlusOneOfPrevious($codepoint); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Codepoint $codepoint |
65
|
|
|
* @return bool |
66
|
|
|
* @throws UnexpectedValueException |
67
|
|
|
*/ |
68
|
|
|
private function isPlusOneOfPrevious(Codepoint $codepoint) |
69
|
|
|
{ |
70
|
|
|
if ($this->previous === null) { |
71
|
|
|
throw new UnexpectedValueException('Previous cannot be NULL'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->previous->getValue() + 1 === $codepoint->getValue(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Codepoint $codepoint |
79
|
|
|
*/ |
80
|
|
|
private function openNewRangeWith(Codepoint $codepoint) |
81
|
|
|
{ |
82
|
|
|
$this->rangeStart = $codepoint; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param Range $range |
87
|
|
|
*/ |
88
|
|
|
private function addRange(Range $range) |
89
|
|
|
{ |
90
|
|
|
array_push($this->aggregated, $range); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function closeCurrentRange() |
94
|
|
|
{ |
95
|
|
|
$this->addRange($this->getCurrentRange()); |
96
|
|
|
$this->rangeStart = null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return Range |
101
|
|
|
* @throws UnexpectedValueException |
102
|
|
|
*/ |
103
|
|
|
private function getCurrentRange() |
104
|
|
|
{ |
105
|
|
|
if ($this->rangeStart === null || $this->previous === null) { |
106
|
|
|
throw new UnexpectedValueException('Range values cannot be NULL'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return Range::between($this->rangeStart, $this->previous); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return Range[]|Range\Collection |
114
|
|
|
*/ |
115
|
|
|
public function getAggregated() |
116
|
|
|
{ |
117
|
|
|
$aggregated = $this->aggregated; |
118
|
|
|
|
119
|
|
|
if ($this->hasOpenRange()) { |
120
|
|
|
array_push($aggregated, $this->getCurrentRange()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return new Range\Collection( |
124
|
|
|
new \ArrayIterator($aggregated) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function hasAggregated() |
132
|
|
|
{ |
133
|
|
|
return count($this->getAggregated()) > 0; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|