forked from FluidTYPO3/vhs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add
v:iterator.range
to implement PHP range()
Supports same arguments and behaves the same way but lets `low` be optional with a default value of `1`.
- Loading branch information
1 parent
05d4f91
commit 9c14db5
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]], | ||
]; | ||
} | ||
} |