-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAggregator.php
88 lines (71 loc) · 2.87 KB
/
Aggregator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace BrunoHanai\DataAggregator;
use BrunoHanai\DataAggregator\Definition\ColumnDefinition;
use BrunoHanai\DataAggregator\Definition\Definition;
use BrunoHanai\DataAggregator\Definition\RowDefinition;
use BrunoHanai\DataAggregator\Definition\RowDefinitionFilter;
use BrunoHanai\DataAggregator\Filter\Evaluator\FilterEvaluator;
use Symfony\Component\PropertyAccess\PropertyAccessor;
class Aggregator
{
private $resultFactory;
private $dataAccessor;
private $definition;
public function __construct(
AggregatorResultFactory $result_factory,
PropertyAccessor $property_accessor,
Definition $definition = null
) {
$this->resultFactory = $result_factory;
$this->dataAccessor = $property_accessor;
$this->definition = $definition;
}
public function setDefinition(Definition $definition)
{
$this->definition = $definition;
return $this;
}
public function aggregate($data) // TODO: Por enquanto só aceita array()
{
if ($this->definition === null) {
throw new \LogicException('The "Definition" must be set.');
}
$result = $this->resultFactory->create();
foreach ($data as $item) {
/** @var $rowDefinition RowDefinition */
foreach ($this->definition->getRows() as $rowDefinition) {
if ($this->checkItem($item, $rowDefinition->getFilter()) === false) {
continue;
}
/** @var $columnDefinition ColumnDefinition */
foreach ($this->definition->getColumns() as $columnDefinition) {
// TODO: isso está ruim:
$sourceColumn = $rowDefinition->getSourceColumn();
$rowId = $sourceColumn === '[virtual]' ?
$rowDefinition->getLabel() :
$this->dataAccessor->getValue($item, $sourceColumn);
$result->appendValue(
$rowId,
$columnDefinition->getOperation(),
$this->dataAccessor->getValue($item, $columnDefinition->getSourceColumn()),
$columnDefinition->getNewColumnName()
);
}
}
}
return $result;
}
// TODO: Esse método deveria estar aqui? E ser dessa forma?
private function checkItem($item, RowDefinitionFilter $row_definition_filter = null)
{
if ($row_definition_filter === null) {
return true;
}
$evaluator = new FilterEvaluator();
foreach ($row_definition_filter->getColumns() as $column => $filter) {
$value = $this->dataAccessor->getValue($item, $column);
$evaluator->appendItem($filter, $value);
}
return $evaluator->evaluate($row_definition_filter->getEvaluatorStrategy());
}
}