-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggregatorResult.php
More file actions
73 lines (57 loc) · 1.85 KB
/
AggregatorResult.php
File metadata and controls
73 lines (57 loc) · 1.85 KB
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
<?php
namespace BrunoHanai\DataAggregator;
use BrunoHanai\DataAggregator\Operation\OperationInterface;
use Cocur\Slugify\Slugify;
class AggregatorResult
{
private $slugify;
private $items;
private $memoryColumns;
public function __construct(Slugify $slugify)
{
$this->slugify = $slugify;
$this->items = array();
$this->memoryColumns = array();
}
public function appendValue($row_label, OperationInterface $operation, $new_value, $column_name)
{
$rowId = $this->createRowId($row_label);
$columnName = $this->adjustColumnName($column_name);
$currentValue = $this->getItemValue($rowId, $columnName);
$currentValue = $currentValue === null ? 0 : $currentValue;
$context = (array)$this->getItem($rowId);
$this->items[$rowId]['_label'] = $row_label;
$this->items[$rowId][$columnName] = $operation->doOperation($currentValue, $new_value, $context);
}
public function getItem($id)
{
return isset($this->items[$id]) ? $this->items[$id] : null;
}
public function getItemValue($id, $column)
{
return isset($this->items[$id][$column]) ? $this->items[$id][$column] : null;
}
public function getArrayResult()
{
return array_values($this->items);
}
private function createRowId($row_id)
{
return base64_encode($row_id);
}
/**
* Saved in memory to avoid unnecessary slugify() calls
*
* @param $column_name
* @return mixed|string
*/
private function adjustColumnName($column_name)
{
if (isset($this->memoryColumns[$column_name])) {
return $this->memoryColumns[$column_name];
}
$columnName = $this->slugify->slugify($column_name);
$this->memoryColumns[$column_name] = $columnName;
return $columnName;
}
}