* @package Vhs * @subpackage ViewHelpers */ abstract class AbstractMultipleMathViewHelper extends AbstractSingleMathViewHelper { use ArrayConsumingViewHelperTrait; /** * @return void */ public function initializeArguments() { parent::initializeArguments(); $this->registerArgument('b', 'mixed', 'Second number or Iterator/Traversable/Array for calculation', TRUE); } /** * @return mixed * @throw Exception */ public function render() { $a = $this->getInlineArgument(); $b = $this->arguments['b']; return $this->calculate($a, $b); } /** * @param mixed $a * @param mixed $b * @return mixed * @throws Exception */ protected function calculate($a, $b) { if ($b === NULL) { throw new Exception('Required argument "b" was not supplied', 1237823699); } $aIsIterable = $this->assertIsArrayOrIterator($a); $bIsIterable = $this->assertIsArrayOrIterator($b); if (TRUE === $aIsIterable) { $a = $this->arrayFromArrayOrTraversableOrCSV($a); foreach ($a as $index => $value) { $bSideValue = TRUE === $bIsIterable ? $b[$index] : $b; $a[$index] = $this->calculateAction($value, $bSideValue); } return $a; } elseif (TRUE === $bIsIterable) { // condition matched if $a is not iterable but $b is. throw new Exception('Math operation attempted using an iterator $b against a numeric value $a. Either both $a and $b, or only $a, must be array/Iterator', 1351890876); } return $this->calculateAction($a, $b); } }