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\Repository\BlockNotFoundException; |
9
|
|
|
use UCD\Unicode\Character\Repository\GeneralCategoryNotFoundException; |
10
|
|
|
use UCD\Unicode\Codepoint\Aggregator; |
11
|
|
|
use UCD\Unicode\Codepoint\Range; |
12
|
|
|
use UCD\Unicode\CodepointAssigned; |
13
|
|
|
|
14
|
|
|
trait PropertySearchByIteration |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritDoc} |
18
|
|
|
*/ |
19
|
|
|
public function getCodepointsByBlock(Block $block) |
20
|
|
|
{ |
21
|
|
|
$comparator = function (CodepointAssigned $item) use ($block) { |
22
|
|
|
return $item->getGeneralProperties() |
23
|
|
|
->getBlock() |
24
|
|
|
->equals($block); |
25
|
|
|
}; |
26
|
|
|
|
27
|
|
|
return $this->aggregatePropertiesWith( |
28
|
|
|
$comparator, |
29
|
|
|
BlockNotFoundException::withBlock($block) |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritDoc} |
35
|
|
|
*/ |
36
|
|
|
public function getCodepointsByCategory(GeneralCategory $category) |
37
|
|
|
{ |
38
|
|
|
$comparator = function (CodepointAssigned $item) use ($category) { |
39
|
|
|
return $item->getGeneralProperties() |
40
|
|
|
->getGeneralCategory() |
41
|
|
|
->equals($category); |
42
|
|
|
}; |
43
|
|
|
|
44
|
|
|
return $this->aggregatePropertiesWith( |
45
|
|
|
$comparator, |
46
|
|
|
GeneralCategoryNotFoundException::withCategory($category) |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param callable $comparator |
52
|
|
|
* @param Exception $notFoundException |
53
|
|
|
* @return Range[]|Range\Collection |
54
|
|
|
* @throws Exception |
55
|
|
|
*/ |
56
|
|
|
private function aggregatePropertiesWith(callable $comparator, Exception $notFoundException) |
57
|
|
|
{ |
58
|
|
|
$aggregator = new Aggregator(); |
59
|
|
|
|
60
|
|
|
foreach ($this->getAll() as $item) { |
61
|
|
|
if (call_user_func($comparator, $item) === true) { |
62
|
|
|
$codepoint = $item->getCodepoint(); |
63
|
|
|
$aggregator->addCodepoint($codepoint); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($aggregator->hasAggregated() !== true) { |
68
|
|
|
throw $notFoundException; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $aggregator->getAggregated(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return CodepointAssigned[] |
76
|
|
|
*/ |
77
|
|
|
abstract public function getAll(); |
78
|
|
|
} |