From a0b36849664203531ff5ab5f8624de49ad876868 Mon Sep 17 00:00:00 2001 From: Krzysztof Grzelak Date: Sat, 29 Jun 2024 06:58:12 +0200 Subject: [PATCH 01/12] update --- src/Attributes.php | 42 ++++++++++++++++++++++++++++++++++++++++ src/Items/Attributes.php | 26 ------------------------- 2 files changed, 42 insertions(+), 26 deletions(-) create mode 100644 src/Attributes.php delete mode 100644 src/Items/Attributes.php diff --git a/src/Attributes.php b/src/Attributes.php new file mode 100644 index 0000000..dbc6129 --- /dev/null +++ b/src/Attributes.php @@ -0,0 +1,42 @@ +attributes[$name] = $value; + + return $this; + } + + public function getAttributes(): array + { + return $this->attributes; + } + + /** + * @return string + */ + public function __toString(): string + { + $html = ''; + + foreach ($this->attributes as $name => $value) { + $html .= sprintf('%s="%s" ', $name, $value); + } + + return $html; + } +} diff --git a/src/Items/Attributes.php b/src/Items/Attributes.php deleted file mode 100644 index 6f00aa7..0000000 --- a/src/Items/Attributes.php +++ /dev/null @@ -1,26 +0,0 @@ - $this->attributes - ])->render(); - } - - public function __toString() - { - return $this->toHtml(); - } -} \ No newline at end of file From 97efb9bea68f0a7034d0a81610b552c6d265582f Mon Sep 17 00:00:00 2001 From: Krzysztof Grzelak Date: Sat, 29 Jun 2024 07:06:38 +0200 Subject: [PATCH 02/12] update --- src/Attributes.php | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Attributes.php b/src/Attributes.php index dbc6129..01ab144 100644 --- a/src/Attributes.php +++ b/src/Attributes.php @@ -9,6 +9,11 @@ class Attributes */ private array $attributes = []; + /** + * @var array + */ + private array $classes = []; + /** * @param string $name * @param string|null $value @@ -21,22 +26,40 @@ public function setAttribute(string $name, ?string $value = null): self return $this; } + /** + * @return array + */ public function getAttributes(): array { return $this->attributes; } + /** + * @param string $class + * @return Attributes + */ + public function addClass(string $class): static + { + $this->classes[] = $class; + + return $this; + } + /** * @return string */ public function __toString(): string { - $html = ''; + $html = []; + + if ($this->classes) { + $this->attributes['class'] = implode(' ', $this->classes); + } foreach ($this->attributes as $name => $value) { - $html .= sprintf('%s="%s" ', $name, $value); + $html[$name] = sprintf('%s="%s"', $name, $value); } - return $html; + return implode(' ', $html); } } From cfc17d4143be29c71bf47a03c101fae9d1109408 Mon Sep 17 00:00:00 2001 From: Krzysztof Grzelak Date: Sat, 29 Jun 2024 10:50:21 +0200 Subject: [PATCH 03/12] update --- resources/views/_attributes.blade.php | 3 --- resources/views/input.blade.php | 9 --------- src/Attributes.php | 17 +++++++++++++++-- 3 files changed, 15 insertions(+), 14 deletions(-) delete mode 100644 resources/views/_attributes.blade.php delete mode 100644 resources/views/input.blade.php diff --git a/resources/views/_attributes.blade.php b/resources/views/_attributes.blade.php deleted file mode 100644 index b681226..0000000 --- a/resources/views/_attributes.blade.php +++ /dev/null @@ -1,3 +0,0 @@ -@foreach($attributes as $key => $value) -{{ $key }}="{{ $value }}" -@endforeach \ No newline at end of file diff --git a/resources/views/input.blade.php b/resources/views/input.blade.php deleted file mode 100644 index ed2e2aa..0000000 --- a/resources/views/input.blade.php +++ /dev/null @@ -1,9 +0,0 @@ - -@error($name) - - {{ $message }} - -@enderror diff --git a/src/Attributes.php b/src/Attributes.php index 01ab144..a2bddf0 100644 --- a/src/Attributes.php +++ b/src/Attributes.php @@ -15,11 +15,12 @@ class Attributes private array $classes = []; /** + * @description Set attribute * @param string $name * @param string|null $value * @return Attributes */ - public function setAttribute(string $name, ?string $value = null): self + public function setAttribute(string $name, mixed $value = null): self { $this->attributes[$name] = $value; @@ -27,6 +28,7 @@ public function setAttribute(string $name, ?string $value = null): self } /** + * @description Get all attributes * @return array */ public function getAttributes(): array @@ -35,6 +37,17 @@ public function getAttributes(): array } /** + * @description Get attribute by name + * @param string $name + * @return string|null + */ + public function getAttribute(string $name): ?string + { + return $this->attributes[$name] ?? null; + } + + /** + * @description Add class to the attributes * @param string $class * @return Attributes */ @@ -57,7 +70,7 @@ public function __toString(): string } foreach ($this->attributes as $name => $value) { - $html[$name] = sprintf('%s="%s"', $name, $value); + $html[$name] = sprintf('%s="%s"', $name, e($value)); } return implode(' ', $html); From 7ca8ec4fe897642b932d16ed6d54622c509c81c1 Mon Sep 17 00:00:00 2001 From: Krzysztof Grzelak Date: Sat, 29 Jun 2024 19:45:06 +0200 Subject: [PATCH 04/12] version 1.0.0 first update --- .gitignore | 2 + composer.json | 91 +++++++---- config/laravel-form.php | 13 +- phpunit.xml | 12 ++ resources/views/radio.blade.php | 9 -- resources/views/select.blade.php | 10 -- resources/views/textarea.blade.php | 6 - src/Attributes.php | 28 +++- src/BaseItem.php | 250 +++++++++++++++++++++++++++++ src/Items/BaseItem.php | 109 ------------- src/Items/FormInput.php | 10 +- src/Items/FormSelect.php | 12 +- src/Items/FormTextarea.php | 12 +- src/Renders/InputRender.php | 13 ++ src/Renders/Render.php | 10 ++ src/Renders/SelectRender.php | 26 +++ src/Renders/TextareaRender.php | 16 ++ tests/AttributesTest.php | 41 +++++ tests/FormInputTest.php | 85 ++++++++++ tests/FormTextareaTest.php | 62 +++++++ tests/TestCase.php | 27 ++++ 21 files changed, 662 insertions(+), 182 deletions(-) create mode 100644 phpunit.xml delete mode 100644 resources/views/radio.blade.php delete mode 100644 resources/views/select.blade.php delete mode 100644 resources/views/textarea.blade.php create mode 100644 src/BaseItem.php delete mode 100644 src/Items/BaseItem.php create mode 100644 src/Renders/InputRender.php create mode 100644 src/Renders/Render.php create mode 100644 src/Renders/SelectRender.php create mode 100644 src/Renders/TextareaRender.php create mode 100644 tests/AttributesTest.php create mode 100644 tests/FormInputTest.php create mode 100644 tests/FormTextareaTest.php create mode 100644 tests/TestCase.php diff --git a/.gitignore b/.gitignore index c253173..d829d85 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ composer.lock vendor/ test.php + +.phpunit.result.cache diff --git a/composer.json b/composer.json index d1020e7..f85c566 100644 --- a/composer.json +++ b/composer.json @@ -1,31 +1,64 @@ { - "name": "kgrzelak/laravel-form", - "license": "MIT", - "authors": [ - { - "name": "Krzysztof Grzelak", - "email": "krzysztof@grzelak.dev", - "homepage": "https://grzelak.dev" + "name": "kgrzelak/laravel-form", + "license": "MIT", + "authors": [ + { + "name": "Krzysztof Grzelak", + "email": "krzysztof@grzelak.dev", + "homepage": "https://grzelak.dev" + } + ], + "require": { + "php": "^8.2", + "spatie/laravel-package-tools": "^1.16.1", + "laravel/framework": "^10.41|^11.0" + }, + "require-dev": { + "phpunit/phpunit": "^11.2", + "mockery/mockery": "^1.6", + "orchestra/testbench": "^9.1" + }, + "autoload": { + "psr-4": { + "Kgrzelak\\LaravelForm\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "Kgrzelak\\LaravelForm\\Tests\\": "tests", + "Workbench\\App\\": "workbench/app/", + "Workbench\\Database\\Factories\\": "workbench/database/factories/", + "Workbench\\Database\\Seeders\\": "workbench/database/seeders/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Kgrzelak\\LaravelForm\\LaravelFormServiceProvider" + ], + "aliases": { + "Form": "Kgrzelak\\LaravelForm\\LaravelForm" + } + } + }, + "scripts": { + "post-autoload-dump": [ + "@clear", + "@prepare" + ], + "clear": "@php vendor/bin/testbench package:purge-skeleton --ansi", + "prepare": "@php vendor/bin/testbench package:discover --ansi", + "build": "@php vendor/bin/testbench workbench:build --ansi", + "serve": [ + "Composer\\Config::disableProcessTimeout", + "@build", + "@php vendor/bin/testbench serve" + ], + "lint": [ + "@php vendor/bin/phpstan analyse" + ], + "test": [ + "@php vendor/bin/phpunit" + ] } - ], - "require": { - "php": "^8.2", - "spatie/laravel-package-tools": "^1.16.1", - "laravel/framework": "^10.41|^11.0" - }, - "autoload": { - "psr-4": { - "Kgrzelak\\LaravelForm\\": "src" - } - }, - "extra": { - "laravel": { - "providers": [ - "Kgrzelak\\LaravelForm\\LaravelFormServiceProvider" - ], - "aliases": { - "Form": "Kgrzelak\\LaravelForm\\LaravelForm" - } - } - } -} +} \ No newline at end of file diff --git a/config/laravel-form.php b/config/laravel-form.php index 8ac3ea8..80128fd 100644 --- a/config/laravel-form.php +++ b/config/laravel-form.php @@ -1,13 +1,20 @@ [ 'class' => 'form-control' ], 'select' => [ 'class' => 'form-select' - ] + ], -]; \ No newline at end of file + 'textarea' => [ + 'class' => 'form-control' + ], + + 'errors' => [ + 'enabled' => true, + 'html' => ':message:' + ] +]; diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..6b3835b --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,12 @@ + + + + + ./tests + + + diff --git a/resources/views/radio.blade.php b/resources/views/radio.blade.php deleted file mode 100644 index 2049bd3..0000000 --- a/resources/views/radio.blade.php +++ /dev/null @@ -1,9 +0,0 @@ - -@error($name) - - {{ $message }} - -@enderror \ No newline at end of file diff --git a/resources/views/select.blade.php b/resources/views/select.blade.php deleted file mode 100644 index b7b93d0..0000000 --- a/resources/views/select.blade.php +++ /dev/null @@ -1,10 +0,0 @@ - -@error($name) - - {{ $message }} - -@enderror \ No newline at end of file diff --git a/resources/views/textarea.blade.php b/resources/views/textarea.blade.php deleted file mode 100644 index 3b686e1..0000000 --- a/resources/views/textarea.blade.php +++ /dev/null @@ -1,6 +0,0 @@ - -@error($name) - - {{ $message }} - -@enderror \ No newline at end of file diff --git a/src/Attributes.php b/src/Attributes.php index a2bddf0..e253ced 100644 --- a/src/Attributes.php +++ b/src/Attributes.php @@ -18,10 +18,15 @@ class Attributes * @description Set attribute * @param string $name * @param string|null $value - * @return Attributes + * @return static */ public function setAttribute(string $name, mixed $value = null): self { + if ($value === null) { + unset($this->attributes[$name]); + return $this; + } + $this->attributes[$name] = $value; return $this; @@ -58,6 +63,27 @@ public function addClass(string $class): static return $this; } + /** + * @description Set class to the attributes + * @param string $class + * @return static + */ + public function setClass(string $class): static + { + $this->classes = [$class]; + + return $this; + } + + /** + * @description Get all classes + * @return array + */ + public function getClass(): array + { + return $this->classes; + } + /** * @return string */ diff --git a/src/BaseItem.php b/src/BaseItem.php new file mode 100644 index 0000000..0148d86 --- /dev/null +++ b/src/BaseItem.php @@ -0,0 +1,250 @@ +attributes = new Attributes; + + $this->attributes->addClass(config('laravel-form.' . $this->viewName . '.class', 'form-control')); + $this->attributes->setAttribute('type', $this->type); + } + + /** + * @description Set the id attribute of the input + * @param string|int $id + * @return static + */ + public function id(string|int $id): static + { + $this->attributes->setAttribute('id', $id); + + return $this; + } + + /** + * @deprecated Use id() instead + * @param string $id + * @return static + */ + public function setId(string $id): static + { + $this->attributes->setAttribute('id', $id); + + return $this; + } + + /** + * @description Set the name attribute of the input + * @param string $name - Name of the input + * @return static + */ + public function name(string $name): static + { + $this->attributes->setAttribute('name', $name); + + return $this; + } + + /** + * @deprecated Use name() instead + * @param string $name - Name of the input + * @return $this + */ + public function setName(string $name): static + { + $this->attributes->setAttribute('name', $name); + + return $this; + } + + public function type(string $type): static + { + $this->attributes->setAttribute('type', $type); + + return $this; + } + + /** + * @description Set the value of the input + * @param mixed|null $value - Value of the input + * @return static + */ + public function value(mixed $value = null): static + { + if ($value instanceof BackedEnum) { + $value = $value->value; + } + + if (app('request')->old($this->attributes->getAttribute('name'))) { + $value = app('request')->old($this->attributes->getAttribute('name')); + } + + $this->attributes->setAttribute('value', $value); + + return $this; + } + + /** + * @deprecated Use value() instead + * @param mixed|null $value + * @return $this + */ + public function setValue(mixed $value = null): static + { + if ($value instanceof BackedEnum) { + $value = $value->value; + } + + if (app('request')->old($this->attributes->getAttribute('name'))) { + $value = app('request')->old($this->attributes->getAttribute('name')); + } + + $this->attributes->setAttribute('value', $value); + + return $this; + } + + /** + * @description Set the placeholder of the input + * @param string $placeholder + * @return static + */ + public function placeholder(string $placeholder): static + { + $this->attributes->setAttribute('placeholder', $placeholder); + + return $this; + } + + /** + * @description Set the required attribute of the input + * @param bool $required + * @return static + */ + public function required(bool $required): static + { + $this->attributes->setAttribute('required', $required); + + return $this; + } + + /** + * @description Set the class of the input + * @param string $class - Name of the class + * @return static + */ + public function setClass(string $class): static + { + $this->attributes->setClass($class); + + return $this; + } + + /** + * @description Add a class to the input + * @param string $class - Name of the class + * @return static + */ + public function addClass(string $class): static + { + $this->attributes->addClass($class); + + return $this; + } + + /** + * @description Set the attribute of the input + * @param string|int $name - Name of the attribute + * @param string|null $value - Value of the attribute + * @return static + */ + public function attribute(string|int $name, ?string $value = null): static + { + $this->attributes->setAttribute($name, $value); + + return $this; + } + + public function toHtml(): string + { + if ($this->hasError() && config('laravel-form.errors.enabled', false)) { + $this->attributes->addClass('is-invalid'); + } + + return new HtmlString( + match ($this->viewName) { + 'input' => (new InputRender())->render($this->attributes), + 'textarea' => (new TextareaRender())->render($this->attributes), + 'select' => (new SelectRender($this->options))->render($this->attributes), + } . $this->getErrors() + ); + } + + public function __toString(): string + { + return $this->toHtml(); + } + + private function getError(): ?string + { + if (!$this->hasError()) { + return null; + } + + $errors = app('session')->get('errors', app(ViewErrorBag::class)); + + return $errors->getBag('default')->first($this->attributes->getAttribute('name')); + } + + private function hasError() + { + if (!$name = $this->attributes->getAttribute('name')) { + return null; + } + + $errors = app('session')->get('errors', app(ViewErrorBag::class)); + if (!$errors->getBag('default')->has($name)) { + return null; + } + + return $errors->getBag('default')->has($name); + } + + private function getErrors(): string + { + $errors = ''; + if (config('laravel-form.errors.enabled', false)) { + if ($error = $this->getError()) { + $errors = + Str::replace( + search: ':message:', + replace: $error, + subject: config('laravel-form.errors.html', ':message:') + ); + } + } + + return $errors; + } +} diff --git a/src/Items/BaseItem.php b/src/Items/BaseItem.php deleted file mode 100644 index ec11665..0000000 --- a/src/Items/BaseItem.php +++ /dev/null @@ -1,109 +0,0 @@ -attributes['id'] = $id; - - return $this; - } - - public function setName(string $name): static - { - $this->name = $name; - - return $this; - } - - public function setType(string $type): static - { - $this->type = $type; - - return $this; - } - - public function setValue(mixed $value = null): static - { - if ($value instanceof BackedEnum) { - $value = $value->value; - } - - $this->value = $value; - - return $this; - } - - public function setPlaceholder(string $placeholder): static - { - $this->placeholder = $placeholder; - - return $this; - } - - public function setAttributes(array $attributes): static - { - $this->attributes = $attributes; - - return $this; - } - - public function setRequired(bool $required): static - { - $this->required = $required; - - return $this; - } - - public function setClass(string $class): static - { - $this->class = $class; - - return $this; - } - - public function toHtml(): string - { - return view('laravel-form::' . $this->viewName, [ - 'name' => $this->name, - 'class' => $this->class, - 'type' => $this->type, - 'value' => $this->value, - 'placeholder' => $this->placeholder, - 'attributes' => new Attributes($this->attributes), - 'required' => $this->required, - 'options' => $this->options - ])->render(); - } - - public function __toString(): string - { - return $this->toHtml(); - } -} diff --git a/src/Items/FormInput.php b/src/Items/FormInput.php index 2bab771..7be13cd 100644 --- a/src/Items/FormInput.php +++ b/src/Items/FormInput.php @@ -2,10 +2,14 @@ namespace Kgrzelak\LaravelForm\Items; +use Kgrzelak\LaravelForm\BaseItem; + class FormInput extends BaseItem { - public function __construct() + public string $viewName = 'input'; + + public function render(): string { - $this->class = config('laravel-form.input.class', 'form-input'); + return 'attributes . '>'; } -} \ No newline at end of file +} diff --git a/src/Items/FormSelect.php b/src/Items/FormSelect.php index 88456a3..49a2dee 100644 --- a/src/Items/FormSelect.php +++ b/src/Items/FormSelect.php @@ -2,13 +2,13 @@ namespace Kgrzelak\LaravelForm\Items; +use Kgrzelak\LaravelForm\BaseItem; + class FormSelect extends BaseItem { - public function __construct() - { - $this->class = config('laravel-form.select.class', 'form-select'); - $this->viewName = 'select'; - } + public string $viewName = 'select'; + + protected array $options = []; public function setOptions(array $options): static { @@ -23,4 +23,4 @@ public function addOption(string $value, string $label): static return $this; } -} \ No newline at end of file +} diff --git a/src/Items/FormTextarea.php b/src/Items/FormTextarea.php index 59081a1..0aa2794 100644 --- a/src/Items/FormTextarea.php +++ b/src/Items/FormTextarea.php @@ -2,11 +2,11 @@ namespace Kgrzelak\LaravelForm\Items; +use Kgrzelak\LaravelForm\BaseItem; + class FormTextarea extends BaseItem { - public function __construct () - { - $this->class = config('laravel-form.textarea.class', 'form-control'); - $this->viewName = 'textarea'; - } -} \ No newline at end of file + public string $viewName = 'textarea'; + + public ?string $type = null; +} diff --git a/src/Renders/InputRender.php b/src/Renders/InputRender.php new file mode 100644 index 0000000..8ecc1db --- /dev/null +++ b/src/Renders/InputRender.php @@ -0,0 +1,13 @@ +'; + } +} diff --git a/src/Renders/Render.php b/src/Renders/Render.php new file mode 100644 index 0000000..9236778 --- /dev/null +++ b/src/Renders/Render.php @@ -0,0 +1,10 @@ +'; + + foreach ($this->options as $option) { + $options .= ''; + } + + $options .= ''; + + return $options; + } +} diff --git a/src/Renders/TextareaRender.php b/src/Renders/TextareaRender.php new file mode 100644 index 0000000..ae53ae6 --- /dev/null +++ b/src/Renders/TextareaRender.php @@ -0,0 +1,16 @@ +getAttribute('value'); + $attributes->setAttribute('value', null); + + return ''; + } +} diff --git a/tests/AttributesTest.php b/tests/AttributesTest.php new file mode 100644 index 0000000..e8c3736 --- /dev/null +++ b/tests/AttributesTest.php @@ -0,0 +1,41 @@ +setClass('test-class'); + + $this->assertEquals(['test-class'], $attributes->getClass()); + } + + public function testCanAddClassAttribute(): void + { + $attributes = new Attributes(); + $attributes->setClass('test-class'); + $attributes->addClass('another-class'); + + $this->assertEquals(['test-class', 'another-class'], $attributes->getClass()); + } + + public function testCanAddAttribute(): void + { + $attributes = new Attributes(); + $attributes->setAttribute('data-test', 'test'); + + $this->assertEquals(['data-test' => 'test'], $attributes->getAttributes()); + } + + public function testCanGetAttribute(): void + { + $attributes = new Attributes(); + $attributes->setAttribute('data-test', 'test'); + + $this->assertEquals('test', $attributes->getAttribute('data-test')); + } +} diff --git a/tests/FormInputTest.php b/tests/FormInputTest.php new file mode 100644 index 0000000..cb9d455 --- /dev/null +++ b/tests/FormInputTest.php @@ -0,0 +1,85 @@ +assertEquals('', $item); + } + + public function testCanCreateInputWithName(): void + { + $item = LaravelForm::input()->name('test'); + + $this->assertEquals('', $item); + } + + public function testCanCreateInputWithId(): void + { + $item = LaravelForm::input()->id('test'); + + $this->assertEquals('', $item); + } + + public function testCanCreateInputWithErrors(): void + { + $this->setErrors(); + + $item = LaravelForm::input()->name('test'); + + $this->assertEquals( + expected: 'error', + actual: $item + ); + } + + public function testCanCreateInputWithNameAndId(): void + { + $item = LaravelForm::input()->name('test')->id('test'); + + $this->assertEquals('', $item); + } + + public function testCanCreateInputWithCustomAttribute(): void + { + $item = LaravelForm::input()->name('test')->attribute('data-test', 'test'); + + $this->assertEquals('', $item); + } + + public function testCanReplaceClassAttribute(): void + { + $item = LaravelForm::input()->name('test')->setClass('test'); + + $this->assertEquals('', $item); + } + + public function testErrorWorksWithCustomClassName(): void + { + $this->setErrors(); + + $item = LaravelForm::input()->name('test')->setClass('test'); + + $this->assertEquals( + expected: 'error', + actual: $item + ); + } + + public function testDontShowErrorsWhenDisabledInConfiguration(): void + { + $this->app['config']->set('laravel-form.errors.enabled', false); + + $this->setErrors(); + + $item = LaravelForm::input()->name('test'); + + $this->assertEquals('', $item); + } +} diff --git a/tests/FormTextareaTest.php b/tests/FormTextareaTest.php new file mode 100644 index 0000000..9e8a5fb --- /dev/null +++ b/tests/FormTextareaTest.php @@ -0,0 +1,62 @@ +assertEquals('', $item); + } + + public function testCanCreateTextareaWithName(): void + { + $item = LaravelForm::textarea()->name('test'); + + $this->assertEquals('', $item); + } + + public function testCanCreateTextareaWithId(): void + { + $item = LaravelForm::textarea()->id('test'); + + $this->assertEquals('', $item); + } + + public function testCanCreateTextareaWithNameAndId(): void + { + $item = LaravelForm::textarea()->name('test')->id('test'); + + $this->assertEquals('', $item); + } + + public function testCanCreateTextareaWithCustomAttribute(): void + { + $item = LaravelForm::textarea()->name('test')->attribute('data-test', 'test'); + + $this->assertEquals('', $item); + } + + public function testCanCreateTextareaWithErrors(): void + { + $this->setErrors(); + + $item = LaravelForm::textarea()->name('test'); + + $this->assertEquals( + expected: 'error', + actual: $item + ); + } + + public function testCanCreateTextareaWithValue(): void + { + $item = LaravelForm::textarea()->value('test'); + + $this->assertEquals('', $item); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..70ee8bc --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,27 @@ +startSession(); + $this->withSession(['errors' => (new ViewErrorBag)->put('default', new MessageBag(['test' => 'error']))]); + } +} From 86b6fb19dbc58c1b0265a15b98542bd60b39fdb9 Mon Sep 17 00:00:00 2001 From: Krzysztof Grzelak Date: Sat, 29 Jun 2024 19:50:38 +0200 Subject: [PATCH 05/12] Form select update --- src/Items/FormSelect.php | 2 ++ tests/FormSelectTest.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tests/FormSelectTest.php diff --git a/src/Items/FormSelect.php b/src/Items/FormSelect.php index 49a2dee..163ba0b 100644 --- a/src/Items/FormSelect.php +++ b/src/Items/FormSelect.php @@ -8,6 +8,8 @@ class FormSelect extends BaseItem { public string $viewName = 'select'; + public ?string $type = null; + protected array $options = []; public function setOptions(array $options): static diff --git a/tests/FormSelectTest.php b/tests/FormSelectTest.php new file mode 100644 index 0000000..d23dd4a --- /dev/null +++ b/tests/FormSelectTest.php @@ -0,0 +1,29 @@ +name('test'); + + $this->assertEquals('', $select); + } + + public function testCanAddOptionToSelect(): void + { + $select = LaravelForm::select()->name('test')->addOption('test', 'Test'); + + $this->assertEquals('', $select); + } + + public function testCanAddMultipleOptionsToSelect(): void + { + $select = LaravelForm::select()->name('test')->addOption('test', 'Test')->addOption('test2', 'Test 2'); + + $this->assertEquals('', $select); + } +} From 32462ec759595db2f0cbfff3d5b35ce47548fad2 Mon Sep 17 00:00:00 2001 From: Krzysztof Grzelak Date: Sat, 29 Jun 2024 20:12:33 +0200 Subject: [PATCH 06/12] csfixer --- .gitignore | 2 ++ composer.json | 5 +++-- src/BaseItem.php | 2 +- src/LaravelForm.php | 2 +- src/LaravelFormServiceProvider.php | 2 +- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index d829d85..eb49355 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ vendor/ test.php .phpunit.result.cache + +.php-cs-fixer.cache diff --git a/composer.json b/composer.json index f85c566..bbed32b 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "require-dev": { "phpunit/phpunit": "^11.2", "mockery/mockery": "^1.6", - "orchestra/testbench": "^9.1" + "orchestra/testbench": "^9.1", + "friendsofphp/php-cs-fixer": "^3.59" }, "autoload": { "psr-4": { @@ -61,4 +62,4 @@ "@php vendor/bin/phpunit" ] } -} \ No newline at end of file +} diff --git a/src/BaseItem.php b/src/BaseItem.php index 0148d86..4248011 100644 --- a/src/BaseItem.php +++ b/src/BaseItem.php @@ -24,7 +24,7 @@ abstract class BaseItem implements Htmlable, Stringable public function __construct() { - $this->attributes = new Attributes; + $this->attributes = new Attributes(); $this->attributes->addClass(config('laravel-form.' . $this->viewName . '.class', 'form-control')); $this->attributes->setAttribute('type', $this->type); diff --git a/src/LaravelForm.php b/src/LaravelForm.php index ce2098b..73408ea 100644 --- a/src/LaravelForm.php +++ b/src/LaravelForm.php @@ -22,4 +22,4 @@ public static function textarea(): FormTextarea { return new FormTextarea(); } -} \ No newline at end of file +} diff --git a/src/LaravelFormServiceProvider.php b/src/LaravelFormServiceProvider.php index 91d63df..2839da8 100644 --- a/src/LaravelFormServiceProvider.php +++ b/src/LaravelFormServiceProvider.php @@ -13,4 +13,4 @@ public function configurePackage(Package $package): void ->hasConfigFile('laravel-form') ->hasViews('laravel-form'); } -} \ No newline at end of file +} From 7f1cd2db698017591c0953f020177d987a1ddb2f Mon Sep 17 00:00:00 2001 From: Krzysztof Grzelak Date: Sat, 29 Jun 2024 20:13:46 +0200 Subject: [PATCH 07/12] phpstan --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index bbed32b..f760e57 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "phpunit/phpunit": "^11.2", "mockery/mockery": "^1.6", "orchestra/testbench": "^9.1", - "friendsofphp/php-cs-fixer": "^3.59" + "friendsofphp/php-cs-fixer": "^3.59", + "phpstan/phpstan": "^1.11" }, "autoload": { "psr-4": { From 8491b8319c5a81533d7e2debc98f0e0783b6f3f8 Mon Sep 17 00:00:00 2001 From: Krzysztof Grzelak Date: Sun, 30 Jun 2024 20:53:38 +0200 Subject: [PATCH 08/12] update --- config/laravel-form.php | 1 + src/BaseItem.php | 4 ++-- src/Items/FormInput.php | 2 ++ src/Items/FormSelect.php | 2 -- src/Items/FormTextarea.php | 2 -- 5 files changed, 5 insertions(+), 6 deletions(-) diff --git a/config/laravel-form.php b/config/laravel-form.php index 80128fd..8099212 100644 --- a/config/laravel-form.php +++ b/config/laravel-form.php @@ -15,6 +15,7 @@ 'errors' => [ 'enabled' => true, + 'element-class' => 'is-invalid', 'html' => ':message:' ] ]; diff --git a/src/BaseItem.php b/src/BaseItem.php index 4248011..90895be 100644 --- a/src/BaseItem.php +++ b/src/BaseItem.php @@ -14,7 +14,7 @@ abstract class BaseItem implements Htmlable, Stringable { - protected ?string $type = 'text'; + protected ?string $type = null; protected Attributes $attributes; @@ -189,7 +189,7 @@ public function attribute(string|int $name, ?string $value = null): static public function toHtml(): string { if ($this->hasError() && config('laravel-form.errors.enabled', false)) { - $this->attributes->addClass('is-invalid'); + $this->attributes->addClass(config('laravel-form.errors.element-class', 'is-invalid')); } return new HtmlString( diff --git a/src/Items/FormInput.php b/src/Items/FormInput.php index 7be13cd..91aa1d3 100644 --- a/src/Items/FormInput.php +++ b/src/Items/FormInput.php @@ -8,6 +8,8 @@ class FormInput extends BaseItem { public string $viewName = 'input'; + public ?string $type = 'text'; + public function render(): string { return 'attributes . '>'; diff --git a/src/Items/FormSelect.php b/src/Items/FormSelect.php index 163ba0b..49a2dee 100644 --- a/src/Items/FormSelect.php +++ b/src/Items/FormSelect.php @@ -8,8 +8,6 @@ class FormSelect extends BaseItem { public string $viewName = 'select'; - public ?string $type = null; - protected array $options = []; public function setOptions(array $options): static diff --git a/src/Items/FormTextarea.php b/src/Items/FormTextarea.php index 0aa2794..6d726ac 100644 --- a/src/Items/FormTextarea.php +++ b/src/Items/FormTextarea.php @@ -7,6 +7,4 @@ class FormTextarea extends BaseItem { public string $viewName = 'textarea'; - - public ?string $type = null; } From a02c5fee371161e37fc997da0945b4e37e913347 Mon Sep 17 00:00:00 2001 From: Krzysztof Grzelak Date: Sun, 30 Jun 2024 20:55:48 +0200 Subject: [PATCH 09/12] final class --- tests/FormSelectTest.php | 2 +- tests/FormTextareaTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/FormSelectTest.php b/tests/FormSelectTest.php index d23dd4a..ae56865 100644 --- a/tests/FormSelectTest.php +++ b/tests/FormSelectTest.php @@ -4,7 +4,7 @@ use Kgrzelak\LaravelForm\LaravelForm; -class FormSelectTest extends TestCase +final class FormSelectTest extends TestCase { public function testCanRenderSelect(): void { diff --git a/tests/FormTextareaTest.php b/tests/FormTextareaTest.php index 9e8a5fb..bfdd8df 100644 --- a/tests/FormTextareaTest.php +++ b/tests/FormTextareaTest.php @@ -4,7 +4,7 @@ use Kgrzelak\LaravelForm\LaravelForm; -class FormTextareaTest extends TestCase +final class FormTextareaTest extends TestCase { public function testCanCreateTextarea(): void { From 96330ac3d36b615c9cdbca1c95466d10a19d8ebf Mon Sep 17 00:00:00 2001 From: Krzysztof Grzelak Date: Sun, 30 Jun 2024 20:56:24 +0200 Subject: [PATCH 10/12] select selected --- src/Renders/SelectRender.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Renders/SelectRender.php b/src/Renders/SelectRender.php index 774cf6e..8778ec5 100644 --- a/src/Renders/SelectRender.php +++ b/src/Renders/SelectRender.php @@ -16,7 +16,9 @@ public function render(Attributes $attributes): string $options = ''; From ba4e2551949433b7463faaa0ce0b81351b86c301 Mon Sep 17 00:00:00 2001 From: Krzysztof Grzelak Date: Sun, 30 Jun 2024 21:00:16 +0200 Subject: [PATCH 11/12] select update --- src/Renders/SelectRender.php | 5 ++++- tests/FormSelectTest.php | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Renders/SelectRender.php b/src/Renders/SelectRender.php index 8778ec5..110ab7c 100644 --- a/src/Renders/SelectRender.php +++ b/src/Renders/SelectRender.php @@ -13,10 +13,13 @@ public function __construct(protected array $options = []) public function render(Attributes $attributes): string { + $value = $attributes->getAttribute('value'); + $attributes->setAttribute('value'); + $options = '', $select); } + + public function testSelectedOptionWorks() + { + $select = LaravelForm::select()->name('test')->addOption('test', 'Test') + ->addOption('test2', 'Test 2')->value('test2')->toHtml(); + + $this->assertEquals('', $select); + } } From 0e7eff44925996f164276db5c80f5732e8bd543a Mon Sep 17 00:00:00 2001 From: Krzysztof Grzelak Date: Sun, 30 Jun 2024 21:53:58 +0200 Subject: [PATCH 12/12] tests update --- tests/AttributesTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/AttributesTest.php b/tests/AttributesTest.php index e8c3736..9f5a0ce 100644 --- a/tests/AttributesTest.php +++ b/tests/AttributesTest.php @@ -38,4 +38,13 @@ public function testCanGetAttribute(): void $this->assertEquals('test', $attributes->getAttribute('data-test')); } + + public function testCanRemoveAttribute(): void + { + $attributes = new Attributes(); + $attributes->setAttribute('data-test', 'test'); + $attributes->setAttribute('data-test'); + + $this->assertEquals([], $attributes->getAttributes()); + } }