1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD\Unicode\Codepoint; |
4
|
|
|
|
5
|
|
|
use UCD\Unicode\Codepoint; |
6
|
|
|
use UCD\Exception\InvalidRangeException; |
7
|
|
|
use UCD\Unicode\TransformationFormat; |
8
|
|
|
|
9
|
|
|
class Range |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Codepoint |
13
|
|
|
*/ |
14
|
|
|
private $start; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Codepoint |
18
|
|
|
*/ |
19
|
|
|
private $end; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Codepoint $start |
23
|
|
|
* @param Codepoint $end |
24
|
|
|
* @throws InvalidRangeException |
25
|
|
|
*/ |
26
|
|
|
protected function __construct(Codepoint $start, Codepoint $end) |
27
|
|
|
{ |
28
|
|
|
if ($start->getValue() > $end->getValue()) { |
29
|
|
|
throw new InvalidRangeException(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->start = $start; |
33
|
|
|
$this->end = $end; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Codepoint $start |
38
|
|
|
* @param Codepoint $end |
39
|
|
|
* @return self |
40
|
|
|
*/ |
41
|
|
|
public static function between(Codepoint $start, Codepoint $end) |
42
|
|
|
{ |
43
|
|
|
return new self($start, $end); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $start |
48
|
|
|
* @param string $end |
49
|
|
|
* @param TransformationFormat $encoding |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
|
|
public static function betweenEncodedCharacters($start, $end, TransformationFormat $encoding) |
53
|
|
|
{ |
54
|
|
|
return self::between( |
55
|
|
|
Codepoint::fromEncodedCharacter($start, $encoding), |
56
|
|
|
Codepoint::fromEncodedCharacter($end, $encoding) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return Codepoint |
62
|
|
|
*/ |
63
|
|
|
public function getStart() |
64
|
|
|
{ |
65
|
|
|
return $this->start; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return Codepoint |
70
|
|
|
*/ |
71
|
|
|
public function getEnd() |
72
|
|
|
{ |
73
|
|
|
return $this->end; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function representsSingleCodepoint() |
80
|
|
|
{ |
81
|
|
|
return $this->start->equals($this->end); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return Codepoint[]|Collection |
86
|
|
|
*/ |
87
|
|
|
public function expand() |
88
|
|
|
{ |
89
|
|
|
return new Collection( |
90
|
|
|
$this->yieldCodepoints() |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return \Generator |
96
|
|
|
*/ |
97
|
|
|
private function yieldCodepoints() |
98
|
|
|
{ |
99
|
|
|
$start = $this->getStart()->getValue(); |
100
|
|
|
$end = $this->getEnd()->getValue(); |
101
|
|
|
|
102
|
|
|
for ($i = $start; $i <= $end; $i++) { |
103
|
|
|
yield Codepoint::fromInt($i); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |