|
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
|
|
|
* @param Factory $aggregatorFactory |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(KeyGenerator $keyGenerator, Factory $aggregatorFactory) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->keyGenerator = $keyGenerator; |
|
30
|
|
|
$this->aggregatorFactory = $aggregatorFactory; |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param CodepointAssigned $entity |
|
35
|
|
|
* @throws InvalidArgumentException |
|
36
|
|
|
*/ |
|
37
|
|
|
public function add(CodepointAssigned $entity) |
|
38
|
|
|
{ |
|
39
|
|
|
$aggregator = $this->getAggregatorFor($entity); |
|
40
|
|
|
$aggregator->addCodepoint($entity->getCodepoint()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param CodepointAssigned[] $entities |
|
45
|
|
|
*/ |
|
46
|
|
|
public function addMany(array $entities) |
|
47
|
|
|
{ |
|
48
|
|
|
foreach ($entities as $entity) { |
|
49
|
|
|
$this->add($entity); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param CodepointAssigned $entity |
|
55
|
|
|
* @return Aggregator |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function getAggregatorFor(CodepointAssigned $entity) |
|
58
|
|
|
{ |
|
59
|
|
|
$key = $this->keyGenerator->generateFor($entity); |
|
60
|
|
|
|
|
61
|
|
|
if (!array_key_exists($key, $this->aggregators)) { |
|
62
|
|
|
$this->aggregators[$key] = $this->aggregatorFactory->create(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->aggregators[$key]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return Aggregator[] |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getAll() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->aggregators; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return Range\Collection[] |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getAllRanges() |
|
80
|
|
|
{ |
|
81
|
|
|
$ranges = []; |
|
82
|
|
|
|
|
83
|
|
|
foreach ($this->aggregators as $key => $aggregator) { |
|
84
|
|
|
$ranges[$key] = $aggregator->getAggregated(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $ranges; |
|
88
|
|
|
} |
|
89
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: