1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD\Unicode\Character\Repository\Capability; |
4
|
|
|
|
5
|
|
|
use UCD\Exception; |
6
|
|
|
use UCD\Unicode\Character\Properties\General\Block; |
7
|
|
|
use UCD\Unicode\Character\Properties\General\GeneralCategory; |
8
|
|
|
use UCD\Unicode\Character\Properties\General\Script; |
9
|
|
|
use UCD\Unicode\Character\Repository\BlockNotFoundException; |
10
|
|
|
use UCD\Unicode\Character\Repository\GeneralCategoryNotFoundException; |
11
|
|
|
use UCD\Unicode\Character\Repository\ScriptNotFoundException; |
12
|
|
|
use UCD\Unicode\Codepoint\Aggregator; |
13
|
|
|
use UCD\Unicode\Codepoint\Range; |
14
|
|
|
use UCD\Unicode\CodepointAssigned; |
15
|
|
|
|
16
|
|
|
trait PropertySearchByIteration |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritDoc} |
20
|
|
|
*/ |
21
|
|
|
public function getCodepointsByBlock(Block $block) |
22
|
|
|
{ |
23
|
|
|
$comparator = function (CodepointAssigned $item) use ($block) { |
24
|
|
|
return $item->getGeneralProperties() |
25
|
|
|
->getBlock() |
26
|
|
|
->equals($block); |
27
|
|
|
}; |
28
|
|
|
|
29
|
|
|
return $this->aggregatePropertiesWith( |
30
|
|
|
$comparator, |
31
|
|
|
BlockNotFoundException::withBlock($block) |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
*/ |
38
|
|
|
public function getCodepointsByCategory(GeneralCategory $category) |
39
|
|
|
{ |
40
|
|
|
$comparator = function (CodepointAssigned $item) use ($category) { |
41
|
|
|
return $item->getGeneralProperties() |
42
|
|
|
->getGeneralCategory() |
43
|
|
|
->equals($category); |
44
|
|
|
}; |
45
|
|
|
|
46
|
|
|
return $this->aggregatePropertiesWith( |
47
|
|
|
$comparator, |
48
|
|
|
GeneralCategoryNotFoundException::withCategory($category) |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function getCodepointsByScript(Script $script) |
56
|
|
|
{ |
57
|
|
|
$comparator = function (CodepointAssigned $item) use ($script) { |
58
|
|
|
return $item->getGeneralProperties() |
59
|
|
|
->getScript() |
60
|
|
|
->equals($script); |
61
|
|
|
}; |
62
|
|
|
|
63
|
|
|
return $this->aggregatePropertiesWith( |
64
|
|
|
$comparator, |
65
|
|
|
ScriptNotFoundException::withScript($script) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param callable $comparator |
71
|
|
|
* @param Exception $notFoundException |
72
|
|
|
* @return Range[]|Range\Collection |
73
|
|
|
* @throws Exception |
74
|
|
|
*/ |
75
|
|
|
private function aggregatePropertiesWith(callable $comparator, Exception $notFoundException) |
76
|
|
|
{ |
77
|
|
|
$aggregator = new Aggregator(); |
78
|
|
|
|
79
|
|
|
foreach ($this->getAll() as $item) { |
80
|
|
|
if (call_user_func($comparator, $item) === true) { |
81
|
|
|
$codepoint = $item->getCodepoint(); |
82
|
|
|
$aggregator->addCodepoint($codepoint); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($aggregator->hasAggregated() !== true) { |
87
|
|
|
throw $notFoundException; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $aggregator->getAggregated(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return CodepointAssigned[] |
95
|
|
|
*/ |
96
|
|
|
abstract public function getAll(); |
97
|
|
|
} |