123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Backend\Model\Widget\Grid;
- /**
- * @api
- * @since 100.0.2
- */
- abstract class AbstractTotals implements \Magento\Backend\Model\Widget\Grid\TotalsInterface
- {
- /**
- * List of columns should be proceed with expression
- * 'key' => column index
- * 'value' => column expression
- *
- * @var array
- */
- protected $_columns = [];
- /**
- * Array of totals based on columns index
- * 'key' => column index
- * 'value' => counted total
- *
- * @var array
- */
- protected $_totals = [];
- /**
- * Factory model
- *
- * @var \Magento\Framework\DataObject\Factory
- */
- protected $_factory;
- /**
- * Parser for expressions like operand operation operand
- *
- * @var \Magento\Backend\Model\Widget\Grid\Parser
- */
- protected $_parser;
- /**
- * @param \Magento\Framework\DataObject\Factory $factory
- * @param \Magento\Backend\Model\Widget\Grid\Parser $parser
- */
- public function __construct(
- \Magento\Framework\DataObject\Factory $factory,
- \Magento\Backend\Model\Widget\Grid\Parser $parser
- ) {
- $this->_factory = $factory;
- $this->_parser = $parser;
- }
- /**
- * Count collection column sum based on column index
- *
- * @param string $index
- * @param \Magento\Framework\Data\Collection $collection
- * @return float|int
- * @abstract
- */
- abstract protected function _countSum($index, $collection);
- /**
- * Count collection column average based on column index
- *
- * @param string $index
- * @param \Magento\Framework\Data\Collection $collection
- * @return float|int
- * @abstract
- */
- abstract protected function _countAverage($index, $collection);
- /**
- * Count collection column sum based on column index and expression
- *
- * @param string $index
- * @param string $expr
- * @param \Magento\Framework\Data\Collection $collection
- * @return float|int
- */
- protected function _count($index, $expr, $collection)
- {
- switch ($expr) {
- case 'sum':
- $result = $this->_countSum($index, $collection);
- break;
- case 'avg':
- $result = $this->_countAverage($index, $collection);
- break;
- default:
- $result = $this->_countExpr($expr, $collection);
- break;
- }
- $this->_totals[$index] = $result;
- return $result;
- }
- /**
- * Return counted expression accorded parsed string
- *
- * @param string $expr
- * @param \Magento\Framework\Data\Collection $collection
- * @return float|int
- */
- protected function _countExpr($expr, $collection)
- {
- $parsedExpression = $this->_parser->parseExpression($expr);
- $result = $tmpResult = 0;
- $firstOperand = $secondOperand = null;
- foreach ($parsedExpression as $operand) {
- if ($this->_parser->isOperation($operand)) {
- $this->_checkOperandsSet($firstOperand, $secondOperand, $tmpResult, $result);
- $result = $this->_operate($firstOperand, $secondOperand, $operand, $tmpResult, $result);
- $firstOperand = $secondOperand = null;
- } else {
- if (null === $firstOperand) {
- $firstOperand = $this->_checkOperand($operand, $collection);
- } elseif (null === $secondOperand) {
- $secondOperand = $this->_checkOperand($operand, $collection);
- }
- }
- }
- return $result;
- }
- /**
- * Check if operands in not null and set operands values if they are empty
- *
- * @param float|int &$firstOperand
- * @param float|int &$secondOperand
- * @param float|int &$tmpResult
- * @param float|int $result
- * @return void
- */
- protected function _checkOperandsSet(&$firstOperand, &$secondOperand, &$tmpResult, $result)
- {
- if (null === $firstOperand && null === $secondOperand) {
- $firstOperand = $tmpResult;
- $secondOperand = $result;
- } elseif (null !== $firstOperand && null === $secondOperand) {
- $secondOperand = $result;
- } elseif (null !== $firstOperand && null !== $secondOperand) {
- $tmpResult = $result;
- }
- }
- /**
- * Get result of operation
- *
- * @param float|int $firstOperand
- * @param float|int $secondOperand
- * @param string $operation
- * @return float|int
- */
- protected function _operate($firstOperand, $secondOperand, $operation)
- {
- $result = 0;
- switch ($operation) {
- case '+':
- $result = $firstOperand + $secondOperand;
- break;
- case '-':
- $result = $firstOperand - $secondOperand;
- break;
- case '*':
- $result = $firstOperand * $secondOperand;
- break;
- case '/':
- $result = $secondOperand ? $firstOperand / $secondOperand : $secondOperand;
- break;
- }
- return $result;
- }
- /**
- * Check operand is numeric or has already counted
- *
- * @param string $operand
- * @param \Magento\Framework\Data\Collection $collection
- * @return float|int
- */
- protected function _checkOperand($operand, $collection)
- {
- if (!is_numeric($operand)) {
- if (isset($this->_totals[$operand])) {
- $operand = $this->_totals[$operand];
- } else {
- $operand = $this->_count($operand, $this->_columns[$operand], $collection);
- }
- } else {
- $operand *= 1;
- }
- return $operand;
- }
- /**
- * Fill columns
- *
- * @param string $index
- * @param string $totalExpr
- * @return $this
- */
- public function setColumn($index, $totalExpr)
- {
- $this->_columns[$index] = $totalExpr;
- return $this;
- }
- /**
- * Return columns set
- *
- * @return array
- */
- public function getColumns()
- {
- return $this->_columns;
- }
- /**
- * Count totals for all columns set
- *
- * @param \Magento\Framework\Data\Collection $collection
- * @return \Magento\Framework\DataObject
- */
- public function countTotals($collection)
- {
- foreach ($this->_columns as $index => $expr) {
- $this->_count($index, $expr, $collection);
- }
- return $this->getTotals();
- }
- /**
- * Get totals as object
- *
- * @return \Magento\Framework\DataObject
- */
- public function getTotals()
- {
- return $this->_factory->create($this->_totals);
- }
- /**
- * Reset totals and columns set
- *
- * @param bool $isFullReset
- * @return void
- */
- public function reset($isFullReset = false)
- {
- if ($isFullReset) {
- $this->_columns = [];
- }
- $this->_totals = [];
- }
- }
|