Skip to content

Commit

Permalink
Added Range Select
Browse files Browse the repository at this point in the history
  • Loading branch information
Grasseh committed Jul 28, 2016
1 parent 02f7e0c commit 59a8148
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/controls.php
Expand Up @@ -35,6 +35,8 @@
echo $form->LegendText('You are', 'legend', ['class' => 'form-control'], 'legend', 'a', 'Wrap');
echo $form->LegendText('You are', 'legend', ['class' => 'form-control', 'position' => 'right'], 'a', 'legend', 'Wrap');
echo $form->Money('Price', 'price', ['class' => 'form-control'], '12.00', 'Wrap');
echo $form->RangeSelect('Selection', 'range', ['class' => 'form-control', 'start' => 5, 'end' => 100, 'step' => 5], 20, 'Wrap');
echo $form->RangeSelect('Noitceles', 'egnar', ['class' => 'form-control', 'start' => 2, 'end' => 20, 'step' => 2, 'inverted' => true], 4, 'Wrap');

echo $form->Submit('Go To Github', 'Click Here', ['class' => 'btn'], 'Wrap');
echo $form->close();
Expand Down
55 changes: 55 additions & 0 deletions src/anekdotes/Controls/RangeSelect.php
@@ -0,0 +1,55 @@
<?php
/*
* This file is part of the Form Wrapper package.
*
* (c) Anekdotes Communication inc. <info@anekdotes.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Anekdotes\FormWrapper\Controls;

use Anekdotes\Support\Arr;

/**
* Wraps a range in a select.
*/
class RangeSelect extends Control
{
protected $nbParams = 3;

/**
* Prepare the input.
*
* @param array $arguments Contains the passed arguments
* [0] string $name Field's name, to be used by the form as a key.
* [1] string[] $opts Contains the option="value" key-value pairs to be added to the field
* [2] string $default Contains the field's default value.
*/
public function prepare($arguments)
{
$name = $arguments[0];
$opts = $arguments[1];
$default = $arguments[2];

$start = Arr::get($opts, 'start', 1);
$end = Arr::get($opts, 'end', 10);
$step = Arr::get($opts, 'step', 1);
$inverted = Arr::get($opts, 'inverted', false);
Arr::remove('start', $opts);
Arr::remove('end', $opts);
Arr::remove('step', $opts);
Arr::remove('inverted', $opts);
$opts['value'] = 0;
$opts['key'] = 0;
$values = array();
if ($inverted) {
for ($i=$end; $i >= $start; $i -= $step) { $values[] = array($i); }
} else {
for ($i=$start; $i <= $end; $i += $step) { $values[] = array($i); }
}
$select = new Select();
return $select->prepare([$name, $values, $opts, [$default], '']);
}
}
53 changes: 53 additions & 0 deletions tests/controls/RangeSelectControlTest.php
@@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Form Wrapper package.
*
* (c) Anekdotes Communication inc. <info@anekdotes.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tests;

use Anekdotes\FormWrapper\Controls\RangeSelect;
use PHPUnit_Framework_TestCase;

class RangeSelectControlTest extends PHPUnit_Framework_TestCase
{
public function testRangeSelectPrepare()
{
$control = new RangeSelect();
$expected = '<select name="name" class="form"><option value="1" class="form-option">1</option><option value="2" class="form-option">2</option><option value="3" class="form-option">3</option><option value="4" class="form-option">4</option><option value="5" selected="selected" class="form-option">5</option><option value="6" class="form-option">6</option><option value="7" class="form-option">7</option><option value="8" class="form-option">8</option><option value="9" class="form-option">9</option><option value="10" class="form-option">10</option></select>';
$opts = [
'name',
[
'class' => 'form',
'step' => 1,
'start' => 1,
'end' => 10,
'inverted' => false,
],
5,
];
$this->assertEquals($control->prepare($opts), $expected);
}

public function testRangeSelectPrepareInverted()
{
$control = new RangeSelect();
$expected = '<select name="name" class="form"><option value="10" class="form-option">10</option><option value="9" class="form-option">9</option><option value="8" class="form-option">8</option><option value="7" class="form-option">7</option><option value="6" class="form-option">6</option><option value="5" selected="selected" class="form-option">5</option><option value="4" class="form-option">4</option><option value="3" class="form-option">3</option><option value="2" class="form-option">2</option><option value="1" class="form-option">1</option></select>';
$opts = [
'name',
[
'class' => 'form',
'step' => 1,
'start' => 1,
'end' => 10,
'inverted' => true,
],
5,
];
$this->assertEquals($control->prepare($opts), $expected);
}
}

0 comments on commit 59a8148

Please sign in to comment.