From 59a8148f11d91644998b591d16d783202fc08c00 Mon Sep 17 00:00:00 2001 From: Grasseh Date: Thu, 28 Jul 2016 16:49:51 -0400 Subject: [PATCH] Added Range Select --- examples/controls.php | 2 + src/anekdotes/Controls/RangeSelect.php | 55 +++++++++++++++++++++++ tests/controls/RangeSelectControlTest.php | 53 ++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 src/anekdotes/Controls/RangeSelect.php create mode 100644 tests/controls/RangeSelectControlTest.php diff --git a/examples/controls.php b/examples/controls.php index 9f92fc7..bfd9788 100644 --- a/examples/controls.php +++ b/examples/controls.php @@ -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(); diff --git a/src/anekdotes/Controls/RangeSelect.php b/src/anekdotes/Controls/RangeSelect.php new file mode 100644 index 0000000..b82a2fd --- /dev/null +++ b/src/anekdotes/Controls/RangeSelect.php @@ -0,0 +1,55 @@ + + * + * 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], '']); + } +} diff --git a/tests/controls/RangeSelectControlTest.php b/tests/controls/RangeSelectControlTest.php new file mode 100644 index 0000000..348a477 --- /dev/null +++ b/tests/controls/RangeSelectControlTest.php @@ -0,0 +1,53 @@ + + * + * 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 = ''; + $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 = ''; + $opts = [ + 'name', + [ + 'class' => 'form', + 'step' => 1, + 'start' => 1, + 'end' => 10, + 'inverted' => true, + ], + 5, + ]; + $this->assertEquals($control->prepare($opts), $expected); + } +}