1 | <?php |
||
20 | class PageTotalsRow implements PartInterface, ViewComponentInterface |
||
21 | { |
||
22 | use PartTrait { |
||
23 | PartTrait::attachToCompound as attachToCompoundInternal; |
||
24 | } |
||
25 | use ChildNodeTrait; |
||
26 | use ViewTrait; |
||
27 | |||
28 | const ID = 'page_totals_row'; |
||
29 | |||
30 | const OPERATION_SUM = 'sum'; |
||
31 | const OPERATION_AVG = 'avg'; |
||
32 | const OPERATION_COUNT = 'count'; |
||
33 | const OPERATION_IGNORE = 'ignore'; |
||
34 | |||
35 | protected $valuePrefixes = [ |
||
36 | self::OPERATION_SUM => 'Σ', |
||
37 | self::OPERATION_AVG => 'Avg.', |
||
38 | self::OPERATION_COUNT => 'Count:' |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * Keys are column id's and values are operations (see PageTotalsRow::OPERATION_* constants). |
||
43 | * |
||
44 | * @var string[]|array |
||
45 | */ |
||
46 | protected $operations; |
||
47 | |||
48 | protected $totalData; |
||
49 | |||
50 | protected $cellObserver; |
||
51 | |||
52 | protected $rowsProcessed = 0; |
||
53 | |||
54 | private $stopDataCollecting = false; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | private $defaultOperation; |
||
60 | |||
61 | /** |
||
62 | * PageTotalsRow constructor. |
||
63 | * |
||
64 | * Operations passed to first argument ($operations) may contain values |
||
65 | * of PageTotalsRow::OPERATIN_* constants or Closure or null. Keys must be equal to target column id's |
||
66 | * If $operations has no value for column, default operation will be used for that column. |
||
67 | * |
||
68 | * @param array|string[] $operations (optional) keys are column id's and values are operations |
||
69 | * (see PageTotalsRow::OPERATION_* constants) or closures. |
||
70 | * @param string|null $defaultOperation operation that will be used for column |
||
71 | * if operation isn't specified for this column in first argument. |
||
72 | */ |
||
73 | public function __construct(array $operations = [], $defaultOperation = null) |
||
74 | { |
||
75 | $this->id = static::ID; |
||
76 | $this->destinationParentId = ManagedList::LIST_CONTAINER_ID; |
||
77 | $this->operations = $operations; |
||
78 | if ($defaultOperation === null) { |
||
79 | $defaultOperation = empty($operations) ? self::OPERATION_SUM : self::OPERATION_IGNORE; |
||
80 | } |
||
81 | $this->defaultOperation = $defaultOperation; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param Compound|Grid $root |
||
86 | * @param bool $prepend |
||
87 | */ |
||
88 | public function attachToCompound(Compound $root, $prepend = false) |
||
89 | { |
||
90 | $this->attachToCompoundInternal($root, $prepend); |
||
91 | $this->replaceGridDataInjector($root); |
||
|
|||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return array |
||
96 | */ |
||
97 | public function getValuePrefixes() |
||
98 | { |
||
99 | return $this->valuePrefixes; |
||
100 | } |
||
101 | |||
102 | public function setValuePrefixes(array $valuePrefixes) |
||
103 | { |
||
104 | $this->valuePrefixes = $valuePrefixes; |
||
105 | return $this; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Renders tag and returns output. |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public function render() |
||
114 | { |
||
115 | /** @var Grid $grid */ |
||
116 | $grid = $this->root; |
||
117 | $this->stopDataCollecting = true; |
||
118 | $tr = $grid->getRecordView(); |
||
119 | |||
120 | // set total_row as current grid row |
||
121 | $lastRow = $grid->getCurrentRow(); |
||
122 | $grid->setCurrentRow($this->totalData); |
||
123 | |||
124 | // modify columns |
||
125 | $valueCalculators = []; |
||
126 | $valueFormatters = []; |
||
127 | foreach ($grid->getColumns() as $column) { |
||
128 | $valueCalculators[$column->getId()] = $column->getValueCalculator(); |
||
129 | $valueFormatters[$column->getId()] = $prevFormatter = $column->getValueFormatter(); |
||
130 | $column->setValueCalculator(null); |
||
131 | $column->setValueFormatter(function ($value) use ($prevFormatter, $column) { |
||
132 | if ($prevFormatter) { |
||
133 | $value = call_user_func($prevFormatter, $value); |
||
134 | } |
||
135 | $operation = $this->getOperation($column->getId()); |
||
136 | if ($value !== null && is_string($operation) && array_key_exists($operation, $this->valuePrefixes)) { |
||
137 | $value = $this->valuePrefixes[$operation] . ' ' . $value; |
||
138 | } |
||
139 | return $value; |
||
140 | }); |
||
141 | } |
||
142 | |||
143 | $output = $tr->render(); |
||
144 | |||
145 | // restore column value calculators & formatters |
||
146 | foreach ($grid->getColumns() as $column) { |
||
147 | $column->setValueCalculator($valueCalculators[$column->getId()]); |
||
148 | $column->setValueFormatter($valueFormatters[$column->getId()]); |
||
149 | } |
||
150 | // restore last data row |
||
151 | $grid->setCurrentRow($lastRow); |
||
152 | return $output; |
||
153 | } |
||
154 | |||
155 | protected function pushData($field, $value) |
||
198 | |||
199 | /** |
||
200 | * @param string $columnName |
||
201 | * @return string|Closure|null |
||
202 | */ |
||
203 | protected function getOperation($columnName) |
||
209 | |||
210 | protected function processCurrentRow() |
||
211 | { |
||
212 | if ($this->stopDataCollecting) { |
||
213 | return; |
||
214 | } |
||
222 | |||
223 | protected function replaceGridDataInjector(Grid $grid) |
||
231 | } |
||
232 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.