1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UCD\Unicode\Codepoint; |
4
|
|
|
|
5
|
|
|
use UCD\Exception\InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
use UCD\Unicode\Codepoint\Aggregator\Factory; |
8
|
|
|
use UCD\Unicode\Codepoint\AggregatorRelay\KeyGenerator; |
9
|
|
|
use UCD\Unicode\CodepointAssigned; |
10
|
|
|
|
11
|
|
|
class AggregatorRelay |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var KeyGenerator |
15
|
|
|
*/ |
16
|
|
|
private $keyGenerator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Aggregator[] |
20
|
|
|
*/ |
21
|
|
|
private $aggregators = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param KeyGenerator $keyGenerator |
25
|
|
|
*/ |
26
|
|
|
public function __construct(KeyGenerator $keyGenerator) |
27
|
|
|
{ |
28
|
|
|
$this->keyGenerator = $keyGenerator; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param CodepointAssigned $entity |
33
|
|
|
* @throws InvalidArgumentException |
34
|
|
|
*/ |
35
|
|
|
public function add(CodepointAssigned $entity) |
36
|
|
|
{ |
37
|
|
|
$aggregator = $this->getAggregatorFor($entity); |
38
|
|
|
$aggregator->addCodepoint($entity->getCodepoint()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param CodepointAssigned[] $entities |
43
|
|
|
*/ |
44
|
|
|
public function addMany(array $entities) |
45
|
|
|
{ |
46
|
|
|
foreach ($entities as $entity) { |
47
|
|
|
$this->add($entity); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param CodepointAssigned $entity |
53
|
|
|
* @return Aggregator |
54
|
|
|
*/ |
55
|
|
|
protected function getAggregatorFor(CodepointAssigned $entity) |
56
|
|
|
{ |
57
|
|
|
$key = $this->keyGenerator->generateFor($entity); |
58
|
|
|
|
59
|
|
|
if (!array_key_exists($key, $this->aggregators)) { |
60
|
|
|
$this->aggregators[$key] = new Aggregator(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->aggregators[$key]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return Range\Collection[] |
68
|
|
|
*/ |
69
|
|
|
public function getAllRanges() |
70
|
|
|
{ |
71
|
|
|
$ranges = []; |
72
|
|
|
|
73
|
|
|
foreach ($this->aggregators as $key => $aggregator) { |
74
|
|
|
$ranges[$key] = $aggregator->getAggregated(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $ranges; |
78
|
|
|
} |
79
|
|
|
} |