From deceea5c0d75a352c2bf52602d1694c42226bfef Mon Sep 17 00:00:00 2001 From: Vincent Klaiber Date: Thu, 2 Nov 2023 08:18:13 +0100 Subject: [PATCH] Added `returnFormat` to `ColorPicker` field --- src/Fields/ColorPicker.php | 16 ++++++++++++++++ tests/Fields/ColorPickerTest.php | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Fields/ColorPicker.php b/src/Fields/ColorPicker.php index 1ba19e56..01bd1efd 100644 --- a/src/Fields/ColorPicker.php +++ b/src/Fields/ColorPicker.php @@ -18,6 +18,7 @@ use Extended\ACF\Fields\Settings\Instructions; use Extended\ACF\Fields\Settings\Required; use Extended\ACF\Fields\Settings\Wrapper; +use InvalidArgumentException; class ColorPicker extends Field { @@ -35,4 +36,19 @@ public function opacity(): static return $this; } + + /** + * @param string $format array, string + * @throws \InvalidArgumentException + */ + public function returnFormat(string $format): static + { + if (!in_array($format, ['array', 'string'])) { + throw new InvalidArgumentException("Invalid argument return format [$format]."); + } + + $this->settings['return_format'] = $format; + + return $this; + } } diff --git a/tests/Fields/ColorPickerTest.php b/tests/Fields/ColorPickerTest.php index a486cc50..b4e8727e 100644 --- a/tests/Fields/ColorPickerTest.php +++ b/tests/Fields/ColorPickerTest.php @@ -19,6 +19,7 @@ use Extended\ACF\Tests\Fields\Settings\Instructions; use Extended\ACF\Tests\Fields\Settings\Required; use Extended\ACF\Tests\Fields\Settings\Wrapper; +use InvalidArgumentException; class ColorPickerTest extends FieldTestCase { @@ -36,4 +37,15 @@ public function testOpacity() $field = ColorPicker::make('Opacity')->opacity()->get(); $this->assertTrue($field['enable_opacity']); } + + public function testReturnFormat() + { + $field = ColorPicker::make('Return Format')->returnFormat('array')->get(); + $this->assertSame('array', $field['return_format']); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid argument return format [test].'); + + ColorPicker::make('Invalid Return Format')->returnFormat('test')->get(); + } }