Skip to content

Commit

Permalink
[FEATURE] Add v:iterator.range to implement PHP range()
Browse files Browse the repository at this point in the history
Supports same arguments and behaves the same way but lets `low` be optional with a default value of `1`.
  • Loading branch information
NamelessCoder committed Aug 8, 2016
1 parent 05d4f91 commit 9c14db5
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Classes/ViewHelpers/Iterator/RangeViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Iterator;

/*
* 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 FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;

/**
* ### Iterator Range ViewHelper
*
* Implementation of `range` for Fluid
*
* Creates a new array of numbers from the low to the high given
* value, incremented by the step value.
*
* #### Usage examples
*
* ```xml
* Numbers 1-10: {v:iterator.implode(glue: ',') -> v:iterator.range(low: 1, high: 10)}
* Even numbers 0-10: {v:iterator.implode(glue: ',') -> v:iterator.range(low: 0, high: 10, step: 2)}
* ```
*/
class RangeViewHelper extends AbstractViewHelper
{

use TemplateVariableViewHelperTrait;

/**
* Initialize arguments
*
* @return void
*/
public function initializeArguments()
{
$this->registerAsArgument();
$this->registerArgument('low', 'integer', 'The low number of the range to be generated', false, 1);
$this->registerArgument('high', 'integer', 'The high number of the range to be generated', true);
$this->registerArgument('step', 'integer', 'The step (increment amount) between each number', false, 1);
}

/**
* @return mixed
*/
public function render()
{
return $this->renderChildrenWithVariableOrReturnInput(
range($this->arguments['low'], $this->arguments['high'], $this->arguments['step'])
);
}
}
41 changes: 41 additions & 0 deletions Tests/Unit/ViewHelpers/Iterator/RangeViewHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace FluidTYPO3\Vhs\Tests\Unit\ViewHelpers\Iterator;

/*
* 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\Tests\Unit\ViewHelpers\AbstractViewHelperTest;

/**
* Class RangeViewHelperTest
*/
class RangeViewHelperTest extends AbstractViewHelperTest
{

/**
* @param array $arguments
* @param array $expected
* @dataProvider getRenderTestValues
*/
public function testRender(array $arguments, array $expected)
{
$this->assertSame($this->executeViewHelper($arguments), $expected);
}

/**
* @return array
*/
public function getRenderTestValues()
{
return [
[['low' => 1, 'high' => 10, 'step' => 1], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]],
[['low' => 5, 'high' => 10, 'step' => 1], [5, 6, 7, 8, 9, 10]],
[['low' => 1, 'high' => 5, 'step' => 1], [1, 2, 3, 4, 5]],
[['low' => 1, 'high' => 10, 'step' => 3], [1, 4, 7, 10]],
];
}
}

0 comments on commit 9c14db5

Please sign in to comment.