|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dwo\Aggregator\Collector; |
|
4
|
|
|
|
|
5
|
|
|
use Dwo\Aggregator\Dumper\DeepDumper; |
|
6
|
|
|
use Dwo\Aggregator\Exception\AggregatorException; |
|
7
|
|
|
use Dwo\Aggregator\Model\EntriesTrait; |
|
8
|
|
|
use Dwo\Aggregator\Model\GroupKey; |
|
9
|
|
|
use Dwo\Aggregator\Model\GroupSet; |
|
10
|
|
|
use Dwo\Aggregator\Model\PreAggregate; |
|
11
|
|
|
use Dwo\Aggregator\Model\PreAggregateGroup; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Collector |
|
15
|
|
|
* |
|
16
|
|
|
* @author Dave Www <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class Collector implements \Iterator |
|
19
|
|
|
{ |
|
20
|
|
|
use EntriesTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $groupKeys; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var GroupSet[]|null |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $groupSets; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var bool |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $unique; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param array $groupKeys |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(array $groupKeys) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->groupKeys = $groupKeys; |
|
43
|
|
|
$this->unique = false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getGroupKeys() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->groupKeys; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param boolean $unique |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setUnique($unique) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->unique = $unique; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $key |
|
64
|
|
|
* @param GroupSet $groupSets |
|
65
|
|
|
*/ |
|
66
|
|
|
public function addGroupSet($key, GroupSet $groupSets) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->groupSets[$key] = $groupSets; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* :TODO: maybe count key instead of $inc |
|
73
|
|
|
* |
|
74
|
|
|
* @param PreAggregate|mixed $entry |
|
75
|
|
|
* @param int $inc |
|
76
|
|
|
*/ |
|
77
|
|
|
public function addEntry($entry, $inc = 1) |
|
78
|
|
|
{ |
|
79
|
|
|
if (!$entry instanceof PreAggregate) { |
|
80
|
|
|
$entry = new PreAggregate($entry, null, $inc); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (null !== $this->groupSets) { |
|
84
|
|
|
$data = $entry->getData(); |
|
85
|
|
|
foreach ($this->groupSets as $key => $groupSet) { |
|
86
|
|
|
if (isset($data[$key])) { |
|
87
|
|
|
$data[$key] = $groupSet->getValue($data[$key]); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
$entry->setData($data); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$group = GroupKey::create($entry->getData(), $this->getGroupKeys()); |
|
94
|
|
|
$key = (string) $group; |
|
95
|
|
|
|
|
96
|
|
|
if (!isset($this->entries[$key])) { |
|
97
|
|
|
$this->entries[$key] = new PreAggregateGroup($group); |
|
98
|
|
|
} elseif ($this->unique) { |
|
99
|
|
|
throw new AggregatorException(sprintf('there is already a entry for this group: %s', $key)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->entries[$key]->addEntry($entry); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
public function toArray() |
|
109
|
|
|
{ |
|
110
|
|
|
return DeepDumper::toArray($this); |
|
111
|
|
|
} |
|
112
|
|
|
} |