Skip to content

Commit a9869fe

Browse files
committed
Use the field name as array key
1 parent d126fc7 commit a9869fe

File tree

4 files changed

+71
-12
lines changed

4 files changed

+71
-12
lines changed

src/FormFieldPrefixer.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@ class FormFieldPrefixer
2323
protected $arrayKey = null;
2424

2525
/**
26-
* Indicator if we are generating a form field name
27-
* with a multi dimensional array.
26+
* Indicator if we are generating a form field name as an array.
27+
*
28+
* @var bool
29+
*/
30+
protected $isArray = false;
31+
32+
/**
33+
* Indicator if we are generating a form field name with a multi dimensional array.
2834
*
2935
* @var bool
3036
*/
@@ -75,9 +81,10 @@ public function withPrefix($prefix)
7581
*
7682
* @return $this
7783
*/
78-
public function asArray($arrayKey, $multiDimensional = false)
84+
public function asArray($arrayKey = null, $multiDimensional = false)
7985
{
8086
$this->arrayKey = $arrayKey;
87+
$this->isArray = true;
8188
$this->multiDimensional = $multiDimensional && $this->prefix !== null;
8289

8390
return $this;
@@ -229,11 +236,11 @@ public function validationKey($key)
229236
*/
230237
public function isArray()
231238
{
232-
return ! is_null($this->arrayKey);
239+
return $this->isArray;
233240
}
234241

235242
/**
236-
* Check if the form field is an array.
243+
* Check if the form field is a multi dimensional array.
237244
*
238245
* @return bool
239246
*/
@@ -252,6 +259,16 @@ public function hasPrefix()
252259
return !! $this->prefix;
253260
}
254261

262+
/**
263+
* Check if the form field has an array key.
264+
*
265+
* @return bool
266+
*/
267+
public function hasArrayKey()
268+
{
269+
return ! is_null($this->arrayKey);
270+
}
271+
255272
/**
256273
* Build the attribute.
257274
*
@@ -307,7 +324,7 @@ protected function buildAttributeValue($name, $useArraySyntax, $separator = null
307324
$separator = $separator ?: $this->getDefaultSeparator();
308325

309326
$prefix = $this->buildName($name);
310-
$arrayKey = $this->buildArrayKey($useArraySyntax, $separator);
327+
$arrayKey = $this->buildArrayKey($name, $useArraySyntax, $separator);
311328
$arrayName = $this->buildArrayName($name, $useArraySyntax, $separator);
312329

313330
$identifier = $prefix . $arrayKey . $arrayName;
@@ -332,7 +349,7 @@ protected function buildName($name)
332349
return $name;
333350
}
334351

335-
if ($this->isMultiDimensionalArray()) {
352+
if ($this->isMultiDimensionalArray() || ($this->isArray() && ! $this->hasArrayKey())) {
336353
return $this->prefix;
337354
}
338355

@@ -342,18 +359,19 @@ protected function buildName($name)
342359
/**
343360
* Build the array key part of the form field identifier if needed.
344361
*
362+
* @param string $name
345363
* @param bool $useArraySyntax
346364
* @param string $separator
347365
*
348366
* @return string
349367
*/
350-
protected function buildArrayKey($useArraySyntax, $separator)
368+
protected function buildArrayKey($name, $useArraySyntax, $separator)
351369
{
352370
if ( ! $this->isArray()) {
353371
return '';
354372
}
355373

356-
return $this->buildArrayIdentifier($this->arrayKey, $useArraySyntax, $separator);
374+
return $this->buildArrayIdentifier($this->arrayKey ?: $name, $useArraySyntax, $separator);
357375
}
358376

359377
/**
@@ -368,7 +386,7 @@ protected function buildArrayKey($useArraySyntax, $separator)
368386
*/
369387
protected function buildArrayName($name, $useArraySyntax, $separator)
370388
{
371-
if ( ! $this->hasPrefix() || ! $this->isMultiDimensionalArray()) {
389+
if ( ! $this->isMultiDimensionalArray()) {
372390
return '';
373391
}
374392

@@ -398,7 +416,7 @@ protected function buildArrayIdentifier($value, $useArraySyntax, $separator)
398416
*/
399417
protected function buildJavaScriptValueKey($key)
400418
{
401-
if ($this->isMultiDimensionalArray()) {
419+
if ($this->isMultiDimensionalArray() || ($this->isArray() && ! $this->hasArrayKey())) {
402420
$key = "'{$key}'";
403421
}
404422

tests/Feature/OldInputValueTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ public function it_builds_the_value_attribute_with_prefixed_arrays()
9292
$this->assertEquals('value="test value"', $prefixer->value('abc'));
9393
}
9494

95+
/** @test */
96+
public function it_builds_the_value_attribute_with_prefixed_arrays_using_the_field_name_as_array_key()
97+
{
98+
Session::flashInput([
99+
'prefix' => [
100+
'abc' => 'test value'
101+
]
102+
]);
103+
104+
$prefixer = (new FormFieldPrefixer('prefix'))->asArray();
105+
106+
$this->assertEquals('value="test value"', $prefixer->value('abc'));
107+
}
108+
95109
/** @test */
96110
public function it_builds_the_value_attribute_with_multi_dimensional_arrays()
97111
{

tests/Feature/OldSelectedOptionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ public function it_builds_the_selected_attribute_with_prefixed_arrays()
103103
$this->assertEquals('', $prefixer->select('abc'));
104104
}
105105

106+
/** @test */
107+
public function it_builds_the_selected_attribute_with_prefixed_arrays_using_the_field_name_as_array_key()
108+
{
109+
Session::flashInput([
110+
'prefix' => [
111+
'abc' => 'selected option value'
112+
]
113+
]);
114+
115+
$prefixer = (new FormFieldPrefixer('prefix'))->asArray();
116+
117+
$this->assertEquals('selected="selected"', $prefixer->selected('abc', 'selected option value'));
118+
$this->assertEquals('', $prefixer->selected('abc', 'other option value'));
119+
$this->assertEquals('', $prefixer->select('abc'));
120+
}
121+
106122
/** @test */
107123
public function it_builds_the_selected_attribute_with_multi_dimensional_arrays()
108124
{

tests/Unit/FormFieldPrefixerTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function it_builds_input_identifiers_with_a_prefix_via_a_method_call()
4141
}
4242

4343
/** @test */
44-
public function it_builds_input_identifiers_as_an_array()
44+
public function it_builds_input_identifiers_as_an_array_without_prefix()
4545
{
4646
$prefixer = (new FormFieldPrefixer())->asArray('arrayKey');
4747

@@ -62,6 +62,17 @@ public function it_builds_input_identifiers_as_an_array_with_prefix()
6262
$this->assertEquals('prefix_abc.arrayKey', $prefixer->validationKey('abc'));
6363
}
6464

65+
/** @test */
66+
public function it_builds_input_identifiers_using_the_field_name_as_array_key()
67+
{
68+
$prefixer = (new FormFieldPrefixer('prefix'))->asArray();
69+
70+
$this->assertEquals('name="prefix[abc]"', $prefixer->name('abc'));
71+
$this->assertEquals('id="prefix_abc"', $prefixer->id('abc'));
72+
$this->assertEquals('for="prefix_abc"', $prefixer->for('abc'));
73+
$this->assertEquals('prefix.abc', $prefixer->validationKey('abc'));
74+
}
75+
6576
/** @test */
6677
public function it_builds_input_identifiers_as_a_multi_dimensional_array_with_prefix()
6778
{

0 commit comments

Comments
 (0)