From f380cdc14dea789a77d82b63e7deebdfe0cef334 Mon Sep 17 00:00:00 2001 From: Drew Date: Fri, 8 Sep 2017 21:20:14 -0700 Subject: [PATCH 1/2] Added $optgroupsAttributes parameter to select method. --- src/FormBuilder.php | 32 ++++++++++++++++++++++---------- tests/FormBuilderTest.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 2b69ac0..3a9795a 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -565,6 +565,7 @@ class FormBuilder * @param string $selected * @param array $selectAttributes * @param array $optionsAttributes + * @param array $optgroupsAttributes * * @return \Illuminate\Support\HtmlString */ @@ -573,7 +574,8 @@ class FormBuilder $list = [], $selected = null, array $selectAttributes = [], - array $optionsAttributes = [] + array $optionsAttributes = [], + array $optgroupsAttributes = [] ) { $this->type = 'select'; @@ -599,8 +601,9 @@ class FormBuilder } foreach ($list as $value => $display) { - $optionAttributes = isset($optionsAttributes[$value]) ? $optionsAttributes[$value] : []; - $html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes); + $optionAttributes = $optionsAttributes[$value] ?? []; + $optgroupAttributes = $optgroupsAttributes[$value] ?? []; + $html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes, $optgroupAttributes); } // Once we have all of this HTML, we can join this into a single element after @@ -675,13 +678,14 @@ class FormBuilder * @param string $value * @param string $selected * @param array $attributes + * @param array $optgroupAttributes * * @return \Illuminate\Support\HtmlString */ - public function getSelectOption($display, $value, $selected, array $attributes = []) + public function getSelectOption($display, $value, $selected, array $attributes = [], array $optgroupAttributes = []) { if (is_array($display)) { - return $this->optionGroup($display, $value, $selected, $attributes); + return $this->optionGroup($display, $value, $selected, $optgroupAttributes, $attributes); } return $this->option($display, $value, $selected, $attributes); @@ -694,18 +698,21 @@ class FormBuilder * @param string $label * @param string $selected * @param array $attributes + * @param array $optionsAttributes * * @return \Illuminate\Support\HtmlString */ - protected function optionGroup($list, $label, $selected, array $attributes = []) + protected function optionGroup($list, $label, $selected, array $attributes = [], array $optionsAttributes = []) { $html = []; foreach ($list as $value => $display) { - $html[] = $this->option($display, $value, $selected, $attributes); - } + $optionAttributes = $optionsAttributes[$value] ?? []; - return $this->toHtmlString('' . implode('', $html) . ''); + $html[] = $this->option($display, $value, $selected, $optionAttributes); + } + + return $this->toHtmlString('html->attributes($attributes) . '>' . implode('', $html) . ''); } /** @@ -724,7 +731,12 @@ class FormBuilder $options = ['value' => $value, 'selected' => $selected] + $attributes; - return $this->toHtmlString('html->attributes($options) . '>' . e($display) . ''); + $string = 'html->attributes($options) . '>'; + if ($display !== null) { + $string .= e($display) . ''; + } + + return $this->toHtmlString($string); } /** diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index d7b1a95..e6b1765 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -402,6 +402,37 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase '' ); + $select = $this->formBuilder->select( + 'size', + [ + 'Large sizes' => [ + 'L' => 'Large', + 'XL' => 'Extra Large', + ], + 'M' => 'Medium', + 'Small sizes' => [ + 'S' => 'Small', + 'XS' => 'Extra Small', + ], + ], + null, + [], + [ + 'Large sizes' => [ + 'L' => ['disabled'] + ], + 'M' => ['disabled'], + ], + [ + 'Small sizes' => ['disabled'], + ] + ); + + $this->assertEquals( + $select, + '' + ); + $select = $this->formBuilder->select( 'encoded_html', ['no_break_space' => ' ', 'ampersand' => '&', 'lower_than' => '<'], From b4c0a3e26d8fbcf264d7c615a0bbf41abec2259f Mon Sep 17 00:00:00 2001 From: Drew Date: Mon, 18 Sep 2017 16:28:31 -0700 Subject: [PATCH 2/2] Style fixes. Replaced == with ===. --- src/Eloquent/FormAccessible.php | 2 +- src/FormBuilder.php | 28 ++++++++++++++-------------- src/HtmlBuilder.php | 4 ++-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Eloquent/FormAccessible.php b/src/Eloquent/FormAccessible.php index de1c191..9b5d6d0 100644 --- a/src/Eloquent/FormAccessible.php +++ b/src/Eloquent/FormAccessible.php @@ -87,7 +87,7 @@ trait FormAccessible $mutator = collect($methods) ->first(function (ReflectionMethod $method) use ($key) { - return $method->getName() == 'form' . Str::studly($key) . 'Attribute'; + return $method->getName() === 'form' . Str::studly($key) . 'Attribute'; }); return (bool) $mutator; diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 3a9795a..94879a4 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -775,7 +775,7 @@ class FormBuilder return $selected->contains($value) ? 'selected' : null; } - return ((string) $value == (string) $selected) ? 'selected' : null; + return ((string) $value === (string) $selected) ? 'selected' : null; } /** @@ -856,7 +856,7 @@ class FormBuilder return $this->getRadioCheckedState($name, $value, $checked); default: - return $this->getValueAttribute($name) == $value; + return $this->getValueAttribute($name) === $value; } } @@ -909,7 +909,7 @@ class FormBuilder return $checked; } - return $this->getValueAttribute($name) == $value; + return $this->getValueAttribute($name) === $value; } /** @@ -1008,7 +1008,7 @@ class FormBuilder { $method = strtoupper($method); - return $method != 'GET' ? 'POST' : $method; + return $method !== 'GET' ? 'POST' : $method; } /** @@ -1110,7 +1110,7 @@ class FormBuilder // If the method is something other than GET we will go ahead and attach the // CSRF token to the form, as this can't hurt and is convenient to simply // always have available on every form the developers creates for them. - if ($method != 'GET') { + if ($method !== 'GET') { $appendage .= $this->token(); } @@ -1152,7 +1152,7 @@ class FormBuilder $old = $this->old($name); - if (! is_null($old) && $name != '_method') { + if (! is_null($old) && $name !== '_method') { return $old; } @@ -1163,7 +1163,7 @@ class FormBuilder if ($hasNullMiddleware && is_null($old) && is_null($value) - && !is_null($this->view->shared('errors')) + && ! is_null($this->view->shared('errors')) && count($this->view->shared('errors')) > 0 ) { return null; @@ -1171,7 +1171,7 @@ class FormBuilder } $request = $this->request($name); - if (!is_null($request)) { + if (! is_null($request)) { return $request; } @@ -1191,7 +1191,7 @@ class FormBuilder */ protected function request($name) { - if (!isset($this->request)) { + if (! isset($this->request)) { return null; } @@ -1229,16 +1229,16 @@ class FormBuilder $key = $this->transformKey($name); $payload = $this->session->getOldInput($key); - if (!is_array($payload)) { + if (! is_array($payload)) { return $payload; } - if (!in_array($this->type, ['select', 'checkbox'])) { - if (!isset($this->payload[$key])) { + if (! in_array($this->type, ['select', 'checkbox'])) { + if (! isset($this->payload[$key])) { $this->payload[$key] = collect($payload); } - if (!empty($this->payload[$key])) { + if (! empty($this->payload[$key])) { $value = $this->payload[$key]->shift(); return $value; } @@ -1255,7 +1255,7 @@ class FormBuilder */ public function oldInputIsEmpty() { - return (isset($this->session) && count($this->session->getOldInput()) == 0); + return (isset($this->session) && count($this->session->getOldInput()) === 0); } /** diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index d2ba3cd..672c437 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -357,7 +357,7 @@ class HtmlBuilder { $html = ''; - if (count($list) == 0) { + if (count($list) === 0) { return $html; } @@ -451,7 +451,7 @@ class HtmlBuilder } // Treat boolean attributes as HTML properties - if (is_bool($value) && $key != 'value') { + if (is_bool($value) && $key !== 'value') { return $value ? $key : ''; }