Skip to content

Commit 5ad0426

Browse files
committed
Fix Undefined array key "{plugin_name}"
1 parent 80c0305 commit 5ad0426

File tree

12 files changed

+69
-89
lines changed

12 files changed

+69
-89
lines changed

src/Html/HasOptions.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,34 @@ public function tabIndex(int $value = 0): static
368368

369369
return $this;
370370
}
371+
372+
/**
373+
* @param string $key
374+
* @param array|bool $value
375+
* @return $this
376+
*/
377+
public function setPluginAttribute(string $key, array|bool $value): static
378+
{
379+
if (is_array($value)) {
380+
$this->attributes[$key] = array_merge((array) ($this->attributes[$key] ?? []), $value);
381+
} else {
382+
$this->attributes[$key] = $value;
383+
}
384+
385+
return $this;
386+
}
387+
388+
/**
389+
* @param string $plugin
390+
* @param string|null $key
391+
* @return mixed
392+
*/
393+
public function getPluginAttribute(string $plugin, string $key = null): mixed
394+
{
395+
if (is_null($key)) {
396+
return $this->attributes[$plugin] ?? true;
397+
}
398+
399+
return $this->attributes[$plugin][$key] ?? false;
400+
}
371401
}

src/Html/Options/Plugins/AutoFill.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,13 @@ public function autoFillAlwaysAsk(bool $value = true): static
2727
* Set autoFill option value.
2828
* Enable and configure the AutoFill extension for DataTables.
2929
*
30-
* @param bool|array $value
30+
* @param array|bool $value
3131
* @return $this
3232
* @see https://datatables.net/reference/option/autoFill
3333
*/
34-
public function autoFill(bool|array $value = true): static
34+
public function autoFill(array|bool $value = true): static
3535
{
36-
if (is_array($value)) {
37-
$this->attributes['autoFill'] = array_merge((array) $this->attributes['autoFill'], $value);
38-
} else {
39-
$this->attributes['autoFill'] = $value;
40-
}
41-
42-
return $this;
36+
return $this->setPluginAttribute('autoFill', $value);
4337
}
4438

4539
/**

src/Html/Options/Plugins/ColReorder.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,13 @@ public function colReorderEnable(bool $value = true): static
2727
* Set colReorder option value.
2828
* Enable and configure the AutoFill extension for DataTables.
2929
*
30-
* @param bool|array $value
30+
* @param array|bool $value
3131
* @return $this
3232
* @see https://datatables.net/reference/option/colReorder
3333
*/
34-
public function colReorder(bool|array $value = true): static
34+
public function colReorder(array|bool $value = true): static
3535
{
36-
if (is_array($value)) {
37-
$this->attributes['colReorder'] = array_merge((array) $this->attributes['colReorder'], $value);
38-
} else {
39-
$this->attributes['colReorder'] = $value;
40-
}
41-
42-
return $this;
36+
return $this->setPluginAttribute('colReorder', $value);
4337
}
4438

4539
/**

src/Html/Options/Plugins/FixedColumns.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,13 @@ public function fixedColumnsHeightMatch(string $value = 'semiauto'): static
2626
/**
2727
* Set fixedColumns option value.
2828
*
29-
* @param bool|array $value
29+
* @param array|bool $value
3030
* @return $this
3131
* @see https://datatables.net/reference/option/fixedColumns
3232
*/
33-
public function fixedColumns(bool|array $value = true): static
33+
public function fixedColumns(array|bool $value = true): static
3434
{
35-
if (is_array($value)) {
36-
$this->attributes['fixedColumns'] = array_merge((array) $this->attributes['fixedColumns'], $value);
37-
} else {
38-
$this->attributes['fixedColumns'] = $value;
39-
}
40-
41-
return $this;
35+
return $this->setPluginAttribute('fixedColumns', $value);
4236
}
4337

4438
/**

src/Html/Options/Plugins/FixedHeader.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,13 @@ public function fixedHeaderFooter(bool $value = true): static
2626
/**
2727
* Set fixedHeader option value.
2828
*
29-
* @param bool|array $value
29+
* @param array|bool $value
3030
* @return $this
3131
* @see https://datatables.net/reference/option/fixedHeader
3232
*/
33-
public function fixedHeader(bool|array $value = true): static
33+
public function fixedHeader(array|bool $value = true): static
3434
{
35-
if (is_array($value)) {
36-
$this->attributes['fixedHeader'] = array_merge((array) $this->attributes['fixedHeader'], $value);
37-
} else {
38-
$this->attributes['fixedHeader'] = $value;
39-
}
40-
41-
return $this;
35+
return $this->setPluginAttribute('fixedHeader', $value);
4236
}
4337

4438
/**

src/Html/Options/Plugins/KeyTable.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,13 @@ public function keysBlurable(bool $value = true): static
2626
/**
2727
* Set keys option value.
2828
*
29-
* @param bool|array $value
29+
* @param array|bool $value
3030
* @return $this
3131
* @see https://datatables.net/reference/option/keys
3232
*/
33-
public function keys(bool|array $value = true): static
33+
public function keys(array|bool $value = true): static
3434
{
35-
if (is_array($value)) {
36-
$this->attributes['keys'] = array_merge((array) $this->attributes['keys'], $value);
37-
} else {
38-
$this->attributes['keys'] = $value;
39-
}
40-
41-
return $this;
35+
return $this->setPluginAttribute('keys', $value);
4236
}
4337

4438
/**

src/Html/Options/Plugins/Responsive.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,13 @@ public function responsiveBreakpoints(array $value): static
2626
/**
2727
* Set responsive option value.
2828
*
29-
* @param bool|array $value
29+
* @param array|bool $value
3030
* @return $this
3131
* @see https://datatables.net/reference/option/responsive
3232
*/
33-
public function responsive(bool|array $value = true): static
33+
public function responsive(array|bool $value = true): static
3434
{
35-
if (is_array($value)) {
36-
$this->attributes['responsive'] = array_merge((array) $this->attributes['responsive'], $value);
37-
} else {
38-
$this->attributes['responsive'] = $value;
39-
}
40-
41-
return $this;
35+
return $this->setPluginAttribute('responsive', $value);
4236
}
4337

4438
/**

src/Html/Options/Plugins/RowGroup.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,13 @@ public function rowGroupUpdate(string $value = 'group'): static
2626
/**
2727
* Set rowGroup option value.
2828
*
29-
* @param bool|array $value
29+
* @param array|bool $value
3030
* @return $this
3131
* @see https://datatables.net/reference/option/rowGroup
3232
*/
33-
public function rowGroup(bool|array $value = true): static
33+
public function rowGroup(array|bool $value = true): static
3434
{
35-
if (is_array($value)) {
36-
$this->attributes['rowGroup'] = array_merge((array) $this->attributes['rowGroup'], $value);
37-
} else {
38-
$this->attributes['rowGroup'] = $value;
39-
}
40-
41-
return $this;
35+
return $this->setPluginAttribute('rowGroup', $value);
4236
}
4337

4438
/**

src/Html/Options/Plugins/RowReorder.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,13 @@ public function rowReorderDataSrc(array|int $value = 0): static
2626
/**
2727
* Set rowReorder option value.
2828
*
29-
* @param bool|array $value
29+
* @param array|bool $value
3030
* @return $this
3131
* @see https://datatables.net/reference/option/rowReorder
3232
*/
33-
public function rowReorder(bool|array $value = true): static
33+
public function rowReorder(array|bool $value = true): static
3434
{
35-
if (is_array($value)) {
36-
$this->attributes['rowReorder'] = array_merge((array) $this->attributes['rowReorder'], $value);
37-
} else {
38-
$this->attributes['rowReorder'] = $value;
39-
}
40-
41-
return $this;
35+
return $this->setPluginAttribute('rowReorder', $value);
4236
}
4337

4438
/**

src/Html/Options/Plugins/Scroller.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,13 @@ public function scrollerBoundaryScale(float $value = 0.5): static
2626
/**
2727
* Set scroller option value.
2828
*
29-
* @param bool|array $value
29+
* @param array|bool $value
3030
* @return $this
3131
* @see https://datatables.net/reference/option/scroller
3232
*/
33-
public function scroller(bool|array $value = true): static
33+
public function scroller(array|bool $value = true): static
3434
{
35-
if (is_array($value)) {
36-
$this->attributes['scroller'] = array_merge((array) $this->attributes['scroller'], $value);
37-
} else {
38-
$this->attributes['scroller'] = $value;
39-
}
40-
41-
return $this;
35+
return $this->setPluginAttribute('scroller', $value);
4236
}
4337

4438
/**

src/Html/Options/Plugins/Select.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,13 @@ public function selectStyleSingle(): static
159159
/**
160160
* Set select option value.
161161
*
162-
* @param bool|array $value
162+
* @param array|bool $value
163163
* @return $this
164164
* @see https://datatables.net/reference/option/select
165165
*/
166166
public function select(bool|array $value = true): static
167167
{
168-
if (is_array($value)) {
169-
$this->attributes['select'] = array_merge((array) $this->attributes['select'], $value);
170-
} else {
171-
$this->attributes['select'] = $value;
172-
}
173-
174-
return $this;
168+
return $this->setPluginAttribute('select', $value);
175169
}
176170

177171
/**

tests/BuilderOptionsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,14 @@ public function it_has_internationalisation_options()
215215
$builder->languageUrl('languageUrl');
216216
$this->assertEquals('languageUrl', $builder->getLanguage('url'));
217217
}
218+
219+
/** @test */
220+
public function it_has_plugin_attribute_getter()
221+
{
222+
$builder = $this->getHtmlBuilder();
223+
224+
$builder->selectStyleSingle();
225+
226+
$this->assertEquals(Builder::SELECT_STYLE_SINGLE, $builder->getPluginAttribute('select', 'style'));
227+
}
218228
}

0 commit comments

Comments
 (0)