Initial commit

This commit is contained in:
2018-04-02 08:07:38 +02:00
commit 7330c1ed3e
2054 changed files with 405203 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Vhs\Traits\ArrayConsumingViewHelperTrait;
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
/**
* Base class: Math ViewHelpers operating on one number or an
* array of numbers.
*
* @author Claus Due <claus@namelesscoder.net>
* @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);
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Vhs\Traits\ArrayConsumingViewHelperTrait;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
/**
* Base class: Math ViewHelpers operating on one number or an
* array of numbers.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers
*/
abstract class AbstractSingleMathViewHelper extends AbstractViewHelper {
use ArrayConsumingViewHelperTrait;
/**
* @return void
*/
public function initializeArguments() {
$this->registerArgument('a', 'mixed', 'First number for calculation', FALSE, NULL, TRUE);
$this->registerArgument('fail', 'boolean', 'If TRUE, throws an Exception if argument "a" is not specified and no child content or inline argument is found. Usually okay to use a NULL value (as integer zero).', FALSE, FALSE);
}
/**
* @param mixed $subject
* @return boolean
*/
protected function assertIsArrayOrIterator($subject) {
return (boolean) (TRUE === is_array($subject) || TRUE === $subject instanceof \Iterator);
}
/**
* @return mixed
* @throw Exception
*/
public function render() {
$a = $this->getInlineArgument();
return $this->calculate($a);
}
/**
* @throws Exception
* @return mixed
*/
protected function getInlineArgument() {
$a = $this->renderChildren();
if (NULL === $a && TRUE === isset($this->arguments['a'])) {
$a = $this->arguments['a'];
}
if (NULL === $a && TRUE === (boolean) $this->arguments['fail']) {
throw new Exception('Required argument "a" was not supplied', 1237823699);
}
return $a;
}
/**
* @param mixed $a
* @return mixed
*/
protected function calculate($a) {
$aIsIterable = $this->assertIsArrayOrIterator($a);
if (TRUE === $aIsIterable) {
$a = $this->arrayFromArrayOrTraversableOrCSV($a);
foreach ($a as $index => $value) {
$a[$index] = $this->calculateAction($value);
}
return $a;
}
return $this->calculateAction($a);
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Average
*
* Performs average across an array. If $a is an array and
* $b is an array, each member of $a is averaged against the
* same member in $b. If $a is an array and $b is a number,
* each member of $a is averaged agained $b. If $a is an array
* this array is averaged to one number. If $a is a number and
* $b is not provided or NULL, $a is gracefully returned as an
* average value of itself.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class AverageViewHelper extends AbstractMultipleMathViewHelper {
/**
* @return void
*/
public function initializeArguments() {
parent::initializeArguments();
$this->overrideArgument('b', 'mixed', 'Optional: Second number or Iterator/Traversable/Array for calculation', FALSE, NULL);
}
/**
* @return mixed
* @throw Exception
*/
public function render() {
$a = $this->getInlineArgument();
$b = $this->arguments['b'];
$aIsIterable = $this->assertIsArrayOrIterator($a);
$bIsIterable = $this->assertIsArrayOrIterator($b);
if (TRUE === $aIsIterable && NULL === $b) {
$a = $this->arrayFromArrayOrTraversableOrCSV($a);
$sum = array_sum($a);
$distribution = count($a);
return $sum / $distribution;
} elseif (TRUE === $aIsIterable && FALSE === $bIsIterable) {
$a = $this->arrayFromArrayOrTraversableOrCSV($a);
foreach ($a as $index => $value) {
$a[$index] = $this->calculateAction($value, $b);
}
return $a;
} elseif (TRUE === isset($a) && NULL === $b) {
return $a;
}
return $this->calculate($a, $b);
}
/**
* @param mixed $a
* @param $b
* @return mixed
*/
protected function calculateAction($a, $b) {
return ($a + $b) / 2;
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Ceil
*
* Ceiling on $a which can be either an array-accessible
* value (Iterator+ArrayAccess || array) or a raw numeric
* value.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class CeilViewHelper extends AbstractSingleMathViewHelper {
/**
* @param mixed $a
* @return integer
*/
protected function calculateAction($a) {
return ceil($a);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Square
*
* Performs $a ^ 3
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class CubeViewHelper extends AbstractSingleMathViewHelper {
/**
* @param mixed $a
* @return integer
*/
protected function calculateAction($a) {
return pow($a, 3);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: CubicRoot
*
* Performs pow($a, 1/3) - cubic or third root
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class CubicRootViewHelper extends AbstractSingleMathViewHelper {
/**
* @param mixed $a
* @return integer
*/
protected function calculateAction($a) {
return pow($a, 1 / 3);
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Division
*
* Performs division of $a using $b. A can be an array and $b a
* number, in which case each member of $a gets divided by $b.
* If both $a and $b are arrays, each member of $a is summed
* against the corresponding member in $b compared using index.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class DivisionViewHelper extends AbstractMultipleMathViewHelper {
/**
* @param mixed $a
* @param mixed $b
* @return mixed
*/
protected function calculateAction($a, $b) {
return (0 <> $b ? $a / $b : $a);
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Floor
*
* Floors $a which can be either an array-accessible
* value (Iterator+ArrayAccess || array) or a raw numeric
* value.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class FloorViewHelper extends AbstractSingleMathViewHelper {
/**
* @param mixed $a
* @return integer
*/
protected function calculateAction($a) {
return floor($a);
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Maximum
*
* Gets the highest number in array $a or the highest
* number of numbers $a and $b.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class MaximumViewHelper extends AbstractMultipleMathViewHelper {
/**
* @return void
*/
public function initializeArguments() {
parent::initializeArguments();
$this->overrideArgument('b', 'mixed', 'Second number or Iterator/Traversable/Array for calculation', FALSE, NULL);
}
/**
* @return mixed
* @throw Exception
*/
public function render() {
$a = $this->getInlineArgument();
$b = $this->arguments['b'];
$aIsIterable = $this->assertIsArrayOrIterator($a);
if (TRUE === $aIsIterable && NULL === $b) {
$a = $this->arrayFromArrayOrTraversableOrCSV($a);
return max($a);
}
return $this->calculate($a, $b);
}
/**
* @param mixed $a
* @param mixed $b
* @return mixed
*/
protected function calculateAction($a, $b) {
return max($a, $b);
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Median
*
* Gets the median value from an array of numbers. If there
* is an odd number of numbers the middle value is returned.
* If there is an even number of numbers an average of the
* two middle numbers is returned.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class MedianViewHelper extends AbstractSingleMathViewHelper {
/**
* @return mixed
* @throw Exception
*/
public function render() {
$a = $this->getInlineArgument();
$aIsIterable = $this->assertIsArrayOrIterator($a);
if (TRUE === $aIsIterable) {
$a = $this->arrayFromArrayOrTraversableOrCSV($a);
sort($a, SORT_NUMERIC);
$size = count($a);
$midpoint = $size / 2;
if (1 === $size % 2) {
/*
* Array indexes of float are truncated to integers,
* not everybody knows, let's make it explicit for everybody
* wondering.
*/
return $a[(integer) $midpoint];
}
$candidates = array_slice($a, floor($midpoint) - 1, 2);
return array_sum($candidates) / 2;
}
return $a;
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Minimum
*
* Gets the lowest number in array $a or the lowest
* number of numbers $a and $b.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class MinimumViewHelper extends AbstractMultipleMathViewHelper {
/**
* @return void
*/
public function initializeArguments() {
parent::initializeArguments();
$this->overrideArgument('b', 'mixed', 'Second number or Iterator/Traversable/Array for calculation', FALSE, NULL);
}
/**
* @return mixed
* @throw Exception
*/
public function render() {
$a = $this->getInlineArgument();
$b = $this->arguments['b'];
$aIsIterable = $this->assertIsArrayOrIterator($a);
if (TRUE === $aIsIterable && NULL === $b) {
$a = $this->arrayFromArrayOrTraversableOrCSV($a);
return min($a);
}
return $this->calculate($a, $b);
}
/**
* @param mixed $a
* @param mixed $b
* @return mixed
*/
protected function calculateAction($a, $b) {
return min($a, $b);
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Modulo
* Perform modulo on $input. Returns the same type as $input,
* i.e. if given an array, will transform each member and return
* the result. Supports array and Iterator (in the following
* descriptions "array" means both these types):
*
* If $a and $b are both arrays of the same size then modulo is
* performed on $a using members of $b, by their index (so these
* must match in both arrays).
*
* If $a is an array and $b is a number then modulo is performed
* on $a using $b for each calculation.
*
* If $a and $b are both numbers simple modulo is performed.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class ModuloViewHelper extends AbstractMultipleMathViewHelper {
/**
* @param mixed $a
* @param mixed $b
* @return integer
*/
protected function calculateAction($a, $b) {
return $a % $b;
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Power
*
* Performs pow($a, $b) where $a is the base and $b is the exponent.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class PowerViewHelper extends AbstractMultipleMathViewHelper {
/**
* @param mixed $a
* @param mixed $b
* @return integer
*/
protected function calculateAction($a, $b) {
return pow($a, $b);
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* ### Math: Product (multiplication)
*
* Product (multiplication) of $a and $b. A can be an array and $b a
* number, in which case each member of $a gets multiplied by $b.
* If $a is an array and $b is not provided then array_product is
* used to return a single numeric value. If both $a and $b are
* arrays, each member of $a is multiplied against the corresponding
* member in $b compared using index.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class ProductViewHelper extends AbstractMultipleMathViewHelper {
/**
* @return mixed
* @throw Exception
*/
public function render() {
$a = $this->getInlineArgument();
$b = $this->arguments['b'];
$aIsIterable = $this->assertIsArrayOrIterator($a);
if (TRUE === $aIsIterable && NULL === $b) {
$a = $this->arrayFromArrayOrTraversableOrCSV($a);
return array_product($a);
}
return $this->calculate($a, $b);
}
/**
* @param mixed $a
* @param mixed $b
* @return mixed
*/
protected function calculateAction($a, $b) {
return $a * $b;
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Range
*
* Gets the lowest and highest number from an array of numbers.
* Returns an array of [low, high]. For individual low/high
* values please use v:math.maximum and v:math.minimum.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class RangeViewHelper extends AbstractSingleMathViewHelper {
/**
* @return mixed
* @throw Exception
*/
public function render() {
$a = $this->getInlineArgument();
$aIsIterable = $this->assertIsArrayOrIterator($a);
if (TRUE === $aIsIterable) {
$a = $this->arrayFromArrayOrTraversableOrCSV($a);
sort($a, SORT_NUMERIC);
if (1 === count($a)) {
return array(reset($a), reset($a));
} else {
return array(array_shift($a), array_pop($a));
}
}
return $a;
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Round
*
* Rounds off $a which can be either an array-accessible
* value (Iterator+ArrayAccess || array) or a raw numeric
* value.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class RoundViewHelper extends AbstractSingleMathViewHelper {
/**
* @return void
*/
public function initializeArguments() {
parent::initializeArguments();
$this->registerArgument('decimals', 'integer', 'Number of decimals', FALSE, 0);
}
/**
* @param mixed $a
* @return integer
*/
protected function calculateAction($a) {
return round($a, $this->arguments['decimals']);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: SquareRoot
*
* Performs sqrt($a)
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class SquareRootViewHelper extends AbstractSingleMathViewHelper {
/**
* @param mixed $a
* @return integer
*/
protected function calculateAction($a) {
return sqrt($a);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Square
*
* Performs $a ^ 2
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class SquareViewHelper extends AbstractSingleMathViewHelper {
/**
* @param mixed $a
* @return integer
*/
protected function calculateAction($a) {
return pow($a, 2);
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Subtract
*
* Performs subtraction of $a and $b. A can be an array and $b a
* number, in which case each member of $a gets subtracted $b.
* If $a is an array and $b is not provided then neg. array_sum is
* used to return a single numeric value. If both $a and $b are
* arrays, each member of $a is summed against the corresponding
* member in $b compared using index.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class SubtractViewHelper extends AbstractMultipleMathViewHelper {
/**
* @return void
*/
public function initializeArguments() {
parent::initializeArguments();
$this->overrideArgument('b', 'mixed', 'Optional: Second number or Iterator/Traversable/Array for calculation', FALSE, NULL);
}
/**
* @return mixed
* @throw Exception
*/
public function render() {
$a = $this->getInlineArgument();
$b = $this->arguments['b'];
$aIsIterable = $this->assertIsArrayOrIterator($a);
if (TRUE === $aIsIterable && NULL === $b) {
$a = $this->arrayFromArrayOrTraversableOrCSV($a);
return -array_sum($a);
}
return $this->calculate($a, $b);
}
/**
* @param mixed $a
* @param $b
* @return mixed
*/
protected function calculateAction($a, $b) {
return $a - $b;
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* Math: Sum
*
* Performs sum of $a and $b. A can be an array and $b a
* number, in which case each member of $a gets summed with $b.
* If $a is an array and $b is not provided then array_sum is
* used to return a single numeric value. If both $a and $b are
* arrays, each member of $a is summed against the corresponding
* member in $b compared using index.
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers\Math
*/
class SumViewHelper extends AbstractMultipleMathViewHelper {
/**
* @return void
*/
public function initializeArguments() {
parent::initializeArguments();
$this->overrideArgument('b', 'mixed', 'Optional: Second number or Iterator/Traversable/Array for calculation', FALSE, NULL);
}
/**
* @return mixed
* @throw Exception
*/
public function render() {
$a = $this->getInlineArgument();
$b = $this->arguments['b'];
$aIsIterable = $this->assertIsArrayOrIterator($a);
if (TRUE === $aIsIterable && NULL === $b) {
$a = $this->arrayFromArrayOrTraversableOrCSV($a);
return array_sum($a);
}
return $this->calculate($a, $b);
}
/**
* @param mixed $a
* @param $b
* @return mixed
*/
protected function calculateAction($a, $b) {
return $a + $b;
}
}