Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractTotals.php
Go to the documentation of this file.
1 <?php
8 
14 {
22  protected $_columns = [];
23 
31  protected $_totals = [];
32 
38  protected $_factory;
39 
45  protected $_parser;
46 
51  public function __construct(
52  \Magento\Framework\DataObject\Factory $factory,
53  \Magento\Backend\Model\Widget\Grid\Parser $parser
54  ) {
55  $this->_factory = $factory;
56  $this->_parser = $parser;
57  }
58 
67  abstract protected function _countSum($index, $collection);
68 
77  abstract protected function _countAverage($index, $collection);
78 
87  protected function _count($index, $expr, $collection)
88  {
89  switch ($expr) {
90  case 'sum':
92  break;
93  case 'avg':
95  break;
96  default:
97  $result = $this->_countExpr($expr, $collection);
98  break;
99  }
100  $this->_totals[$index] = $result;
101 
102  return $result;
103  }
104 
112  protected function _countExpr($expr, $collection)
113  {
114  $parsedExpression = $this->_parser->parseExpression($expr);
115  $result = $tmpResult = 0;
116  $firstOperand = $secondOperand = null;
117  foreach ($parsedExpression as $operand) {
118  if ($this->_parser->isOperation($operand)) {
119  $this->_checkOperandsSet($firstOperand, $secondOperand, $tmpResult, $result);
120  $result = $this->_operate($firstOperand, $secondOperand, $operand, $tmpResult, $result);
121  $firstOperand = $secondOperand = null;
122  } else {
123  if (null === $firstOperand) {
124  $firstOperand = $this->_checkOperand($operand, $collection);
125  } elseif (null === $secondOperand) {
126  $secondOperand = $this->_checkOperand($operand, $collection);
127  }
128  }
129  }
130  return $result;
131  }
132 
142  protected function _checkOperandsSet(&$firstOperand, &$secondOperand, &$tmpResult, $result)
143  {
144  if (null === $firstOperand && null === $secondOperand) {
145  $firstOperand = $tmpResult;
146  $secondOperand = $result;
147  } elseif (null !== $firstOperand && null === $secondOperand) {
148  $secondOperand = $result;
149  } elseif (null !== $firstOperand && null !== $secondOperand) {
150  $tmpResult = $result;
151  }
152  }
153 
162  protected function _operate($firstOperand, $secondOperand, $operation)
163  {
164  $result = 0;
165  switch ($operation) {
166  case '+':
167  $result = $firstOperand + $secondOperand;
168  break;
169  case '-':
170  $result = $firstOperand - $secondOperand;
171  break;
172  case '*':
173  $result = $firstOperand * $secondOperand;
174  break;
175  case '/':
176  $result = $secondOperand ? $firstOperand / $secondOperand : $secondOperand;
177  break;
178  }
179  return $result;
180  }
181 
189  protected function _checkOperand($operand, $collection)
190  {
191  if (!is_numeric($operand)) {
192  if (isset($this->_totals[$operand])) {
193  $operand = $this->_totals[$operand];
194  } else {
195  $operand = $this->_count($operand, $this->_columns[$operand], $collection);
196  }
197  } else {
198  $operand *= 1;
199  }
200  return $operand;
201  }
202 
210  public function setColumn($index, $totalExpr)
211  {
212  $this->_columns[$index] = $totalExpr;
213  return $this;
214  }
215 
221  public function getColumns()
222  {
223  return $this->_columns;
224  }
225 
232  public function countTotals($collection)
233  {
234  foreach ($this->_columns as $index => $expr) {
235  $this->_count($index, $expr, $collection);
236  }
237 
238  return $this->getTotals();
239  }
240 
246  public function getTotals()
247  {
248  return $this->_factory->create($this->_totals);
249  }
250 
257  public function reset($isFullReset = false)
258  {
259  if ($isFullReset) {
260  $this->_columns = [];
261  }
262 
263  $this->_totals = [];
264  }
265 }
_checkOperandsSet(&$firstOperand, &$secondOperand, &$tmpResult, $result)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Framework\DataObject\Factory $factory, \Magento\Backend\Model\Widget\Grid\Parser $parser)
_operate($firstOperand, $secondOperand, $operation)
$index
Definition: list.phtml:44