From 2036cabf29d2218581d4e66fbc06a30191d2a342 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Thu, 28 Mar 2024 20:05:48 -0300 Subject: [PATCH 1/3] Add `cookbook()` method to `Widget::class`. --- CHANGELOG.md | 4 ++- docs/README.md | 36 ++++++++++++++++++++++++++ src/Base/Widget.php | 28 ++++++++++++++++++++ src/Factory/SimpleFactory.php | 11 ++------ tests/Factory/SimpleFactoryTest.php | 17 +++--------- tests/Support/Widget/Widget.php | 7 +++++ tests/Widget/ConstructorTest.php | 6 +---- tests/Widget/CookbookTest.php | 40 +++++++++++++++++++++++++++++ 8 files changed, 121 insertions(+), 28 deletions(-) create mode 100644 tests/Widget/CookbookTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dd81b3..2c1f9db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ Change Log ========== -## 0.1.4 Under development +## 0.1.4 March 28, 2024 + +- Enh #37: Add `cookbook()` method to `Widget::class` (@terabytesoftw) ## 0.1.3 March 26, 2024 diff --git a/docs/README.md b/docs/README.md index edf18c8..83f5666 100644 --- a/docs/README.md +++ b/docs/README.md @@ -96,6 +96,42 @@ final class DefaultDefinition extends Element } ``` +## Cookbook + +The `cookbook()` method is used to configure the widget with the provided default definitions. + +```php + BootstrapAlertDismissible::definition($option), + ]; + } +} +``` + +```php +cookbook('bootstrap-alert-dismmisible', 'success')->render(); +``` + ## Creating To create a widget, you need to extend the `PHPForge\Widget\AbstractWidget::class` and implement the `PHPForge\Widget\Widget::run()` protected method. diff --git a/src/Base/Widget.php b/src/Base/Widget.php index 54873ea..5129260 100644 --- a/src/Base/Widget.php +++ b/src/Base/Widget.php @@ -35,6 +35,22 @@ final public function __toString(): string return $this->run(); } + /** + * This method is used to configure the widget with the provided cookbook definitions. + * + * @param string $key The key to load the cookbook for. + * @param string $option The option to load the cookbook for. + * + * @return static The widget instance with the cookbook definitions applied. + */ + final public function cookbook(string $key, string $option): static + { + /** @psalm-var array $cookbook */ + $cookbook = $this->getCookbooks($option)[$key] ?? []; + + return SimpleFactory::configure($this, $cookbook); + } + /** * This method is a static factory method that creates an instance of the widget. * It uses the ReflectionClass to create a new instance of the widget with the provided arguments. @@ -57,6 +73,18 @@ final public static function widget(mixed ...$args): static */ abstract protected function run(): string; + /** + * This method is used to configure the widget with the provided cookbooks. + * + * @param string $option The option to load the cookbook for. + * + * @return array The cookbook for the widget configuration. + */ + protected function getCookbooks(string $option): array + { + return []; + } + /** * This method is used to configure the widget with the provided default definitions. */ diff --git a/src/Factory/SimpleFactory.php b/src/Factory/SimpleFactory.php index 275ace4..fbb2f23 100644 --- a/src/Factory/SimpleFactory.php +++ b/src/Factory/SimpleFactory.php @@ -77,12 +77,7 @@ public static function create(string $class, array $args): Widget * * @return Widget The widget with the applied definitions. * - * @psalm-template T of Widget - * - * @psalm-param T $widget The widget to configure. - * @psalm-param array $definitions The definitions to apply to the widget. - * - * @psalm-return T The widget with the applied definitions. + * @psalm-param array $definitions */ public static function configure(object $widget, array $definitions): widget { @@ -97,7 +92,6 @@ public static function configure(object $widget, array $definitions): widget } if ($definitions === []) { - /** @psalm-var T $widget */ return $widget; } @@ -105,8 +99,7 @@ public static function configure(object $widget, array $definitions): widget if (str_ends_with($action, '()')) { $setter = call_user_func_array([$widget, substr($action, 0, -2)], $arguments); - if ($setter instanceof $widget) { - /** @psalm-var T $widget */ + if ($setter instanceof Widget) { $widget = $setter; } } diff --git a/tests/Factory/SimpleFactoryTest.php b/tests/Factory/SimpleFactoryTest.php index a4a83bb..015660d 100644 --- a/tests/Factory/SimpleFactoryTest.php +++ b/tests/Factory/SimpleFactoryTest.php @@ -23,9 +23,7 @@ public function testConfigure(): void { $widget = SimpleFactory::configure( Widget::widget(), - [ - 'id()' => ['id-configure'], - ] + ['id()' => ['id-configure']] ); $this->assertSame('', $widget->render()); @@ -54,12 +52,8 @@ public function testCreate(): void public function testCreateWithDefaultDefinitions(): void { $defaultDefinitions = [ - Widget::class => [ - 'id()' => ['id-widget'], - ], - WidgetConstructor::class => [ - 'id()' => ['id-constructor'], - ], + Widget::class => ['id()' => ['id-widget']], + WidgetConstructor::class => ['id()' => ['id-constructor']], ]; SimpleFactory::defaultDefinitions($defaultDefinitions); @@ -84,10 +78,7 @@ public function testLoadDefaultDefinitions(): void public function testPriority(): void { $widget = SimpleFactory::configure( - DefaultDefinition::widget(), - [ - 'id()' => ['id-configure'], - ] + DefaultDefinition::widget(), ['id()' => ['id-configure']] ); $this->assertSame('', $widget->render()); diff --git a/tests/Support/Widget/Widget.php b/tests/Support/Widget/Widget.php index a90aa26..640e151 100644 --- a/tests/Support/Widget/Widget.php +++ b/tests/Support/Widget/Widget.php @@ -38,6 +38,13 @@ protected function afterRun(string $result): string return $result; } + protected function getCookbooks(string $option): array + { + return [ + 'cookbook-id' => ['id()' => [$option]], + ]; + } + protected function run(): string { return 'id . '">'; diff --git a/tests/Widget/ConstructorTest.php b/tests/Widget/ConstructorTest.php index 7f94c85..7194efb 100644 --- a/tests/Widget/ConstructorTest.php +++ b/tests/Widget/ConstructorTest.php @@ -21,11 +21,7 @@ public function testRender(): void public function testConstructorWithDefinitions(): void { - $output = WidgetConstructor::widget( - [ - 'id()' => ['w1'], - ] - ); + $output = WidgetConstructor::widget(['id()' => ['w1']]); $this->assertSame('', $output->render()); } diff --git a/tests/Widget/CookbookTest.php b/tests/Widget/CookbookTest.php new file mode 100644 index 0000000..2509403 --- /dev/null +++ b/tests/Widget/CookbookTest.php @@ -0,0 +1,40 @@ +assertSame( + '', + Widget::widget()->cookbook('cookbook-id', 'test-id')->render() + ); + } + + public function testGetCookbooks(): void + { + $instance = new class extends Element { + protected function run(): string + { + return ''; + } + + public function cookbooks(): array + { + return $this->getCookbooks(''); + } + }; + + $this->assertEmpty($instance->cookbooks()); + } +} From d134df5e608e6ab7366e22070ddfbe62670cb15e Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 28 Mar 2024 23:06:14 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI --- tests/Factory/SimpleFactoryTest.php | 3 ++- tests/Widget/CookbookTest.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/Factory/SimpleFactoryTest.php b/tests/Factory/SimpleFactoryTest.php index 015660d..3be3234 100644 --- a/tests/Factory/SimpleFactoryTest.php +++ b/tests/Factory/SimpleFactoryTest.php @@ -78,7 +78,8 @@ public function testLoadDefaultDefinitions(): void public function testPriority(): void { $widget = SimpleFactory::configure( - DefaultDefinition::widget(), ['id()' => ['id-configure']] + DefaultDefinition::widget(), + ['id()' => ['id-configure']] ); $this->assertSame('', $widget->render()); diff --git a/tests/Widget/CookbookTest.php b/tests/Widget/CookbookTest.php index 2509403..e98d49c 100644 --- a/tests/Widget/CookbookTest.php +++ b/tests/Widget/CookbookTest.php @@ -23,7 +23,7 @@ public function testCookbook(): void public function testGetCookbooks(): void { - $instance = new class extends Element { + $instance = new class() extends Element { protected function run(): string { return ''; From 649fc68c452760e9fee8344474f1db7b2c07cac8 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Thu, 28 Mar 2024 20:07:50 -0300 Subject: [PATCH 3/3] Fix ecs test. --- ecs.php | 1 - tests/Widget/CookbookTest.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/ecs.php b/ecs.php index 6331e59..c2a7c88 100644 --- a/ecs.php +++ b/ecs.php @@ -24,7 +24,6 @@ ) ->withPhpCsFixerSets(perCS20: true) ->withPreparedSets( - arrays: true, cleanCode: true, comments:true, docblocks: true, diff --git a/tests/Widget/CookbookTest.php b/tests/Widget/CookbookTest.php index e98d49c..2977b32 100644 --- a/tests/Widget/CookbookTest.php +++ b/tests/Widget/CookbookTest.php @@ -23,7 +23,7 @@ public function testCookbook(): void public function testGetCookbooks(): void { - $instance = new class() extends Element { + $instance = new class () extends Element { protected function run(): string { return '';