From 5d8033c722517085933569dec0a22dd380c2c71c Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Tue, 8 Mar 2016 19:41:13 -0300 Subject: [PATCH 1/3] individual attributes for each option (multi checkbox) --- src/Helper/Input/Checkbox.php | 31 ++++++++++++++++++++++++++++- tests/Helper/Input/CheckboxTest.php | 24 ++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/Helper/Input/Checkbox.php b/src/Helper/Input/Checkbox.php index dc06364..5c0c215 100644 --- a/src/Helper/Input/Checkbox.php +++ b/src/Helper/Input/Checkbox.php @@ -17,6 +17,33 @@ */ class Checkbox extends AbstractChecked { + + /** + * + * Individual attributes for each option (only for multi checkbox) + * + * @var array + * + */ + protected $optionsAttribs = array(); + + /** + * + * Prepares the properties on this helper. + * + * @param array $spec The specification array. + * + */ + protected function prep(array $spec) + { + if (isset($spec['optionsAttribs'])) { + $this->optionsAttribs = $spec['optionsAttribs']; + unset($spec['optionsAttribs']); + } + + parent::prep($spec); + } + /** * * Returns the HTML for the input. @@ -80,10 +107,12 @@ protected function multiple() $this->attribs['value'] = $value; $this->attribs['label'] = $label; + $optionAttribs = isset($this->optionsAttribs[$value]) ? $this->optionsAttribs[$value] : []; + $html .= $checkbox(array( 'name' => $this->attribs['name'], 'value' => $this->value, - 'attribs' => $this->attribs + 'attribs' => $this->attribs+$optionAttribs )); } return $html; diff --git a/tests/Helper/Input/CheckboxTest.php b/tests/Helper/Input/CheckboxTest.php index d3a004b..dc03858 100644 --- a/tests/Helper/Input/CheckboxTest.php +++ b/tests/Helper/Input/CheckboxTest.php @@ -170,4 +170,28 @@ public function testMultiCheckboxWithValues() { . '' . PHP_EOL; $this->assertSame($expect, $actual); } + + public function testOptionAttributes() { + $checkbox = $this->helper; + $actual = $checkbox(array( + 'name' => 'foo', + 'value' => 'yes', + 'options' => array( + 'yes' => 'Yes', + 'no' => 'No', + ), + 'optionsAttribs' => array( + 'yes' => array( + 'class' => 'test-class' + ), + 'no' => array( + 'data-no' => 1 + ), + ), + ))->__toString(); + $expect = '' . PHP_EOL + . '' . PHP_EOL; + $this->assertSame($expect, $actual); + } + } From ff88814e8f69799decf8399aef182ec0258c5efe Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Wed, 9 Mar 2016 13:09:44 -0300 Subject: [PATCH 2/3] cs fixes --- src/Helper/Input/Checkbox.php | 12 ++++++------ tests/Helper/Input/CheckboxTest.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Helper/Input/Checkbox.php b/src/Helper/Input/Checkbox.php index 5c0c215..85a7faa 100644 --- a/src/Helper/Input/Checkbox.php +++ b/src/Helper/Input/Checkbox.php @@ -25,7 +25,7 @@ class Checkbox extends AbstractChecked * @var array * */ - protected $optionsAttribs = array(); + protected $options_attribs = array(); /** * @@ -36,9 +36,9 @@ class Checkbox extends AbstractChecked */ protected function prep(array $spec) { - if (isset($spec['optionsAttribs'])) { - $this->optionsAttribs = $spec['optionsAttribs']; - unset($spec['optionsAttribs']); + if (isset($spec['options_attribs'])) { + $this->options_attribs = $spec['options_attribs']; + unset($spec['options_attribs']); } parent::prep($spec); @@ -107,12 +107,12 @@ protected function multiple() $this->attribs['value'] = $value; $this->attribs['label'] = $label; - $optionAttribs = isset($this->optionsAttribs[$value]) ? $this->optionsAttribs[$value] : []; + $optionAttribs = isset($this->options_attribs[$value]) ? $this->options_attribs[$value] : array(); $html .= $checkbox(array( 'name' => $this->attribs['name'], 'value' => $this->value, - 'attribs' => $this->attribs+$optionAttribs + 'attribs' => $this->attribs + $optionAttribs )); } return $html; diff --git a/tests/Helper/Input/CheckboxTest.php b/tests/Helper/Input/CheckboxTest.php index dc03858..ec104a8 100644 --- a/tests/Helper/Input/CheckboxTest.php +++ b/tests/Helper/Input/CheckboxTest.php @@ -180,7 +180,7 @@ public function testOptionAttributes() { 'yes' => 'Yes', 'no' => 'No', ), - 'optionsAttribs' => array( + 'options_attribs' => array( 'yes' => array( 'class' => 'test-class' ), From deacc19db6aab75721d7306cf231b4c498dcd931 Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Wed, 9 Mar 2016 13:42:17 -0300 Subject: [PATCH 3/3] cs fix --- src/Helper/Input/Checkbox.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Helper/Input/Checkbox.php b/src/Helper/Input/Checkbox.php index 85a7faa..f78669c 100644 --- a/src/Helper/Input/Checkbox.php +++ b/src/Helper/Input/Checkbox.php @@ -107,12 +107,12 @@ protected function multiple() $this->attribs['value'] = $value; $this->attribs['label'] = $label; - $optionAttribs = isset($this->options_attribs[$value]) ? $this->options_attribs[$value] : array(); + $option_attribs = isset($this->options_attribs[$value]) ? $this->options_attribs[$value] : array(); $html .= $checkbox(array( 'name' => $this->attribs['name'], 'value' => $this->value, - 'attribs' => $this->attribs + $optionAttribs + 'attribs' => $this->attribs + $option_attribs )); } return $html;