From 32f8a6e24d6c8fd5ed1bc60989ec6786b297a335 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Thu, 9 Jan 2025 15:38:09 +0800 Subject: [PATCH 1/8] feat: add tags field support --- src/Html/Editor/Fields/Tags.php | 146 ++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 src/Html/Editor/Fields/Tags.php diff --git a/src/Html/Editor/Fields/Tags.php b/src/Html/Editor/Fields/Tags.php new file mode 100644 index 0000000..05dc8d9 --- /dev/null +++ b/src/Html/Editor/Fields/Tags.php @@ -0,0 +1,146 @@ +attributes['ajax'] = $url; + + return $this; + } + + /** + * @see https://editor.datatables.net/reference/field/tags#display + */ + public function display(string $display): static + { + $this->attributes['display'] = $display; + + return $this; + } + + /** + * @see https://editor.datatables.net/reference/field/tags#escapeLabelHtml + */ + public function escapeLabelHtml(bool $escape): static + { + $this->attributes['escapeLabelHtml'] = $escape; + + return $this; + } + + /** + * @param array { + * addButton?: string + * inputPlaceholder?: string + * noResults?: string + * title?: string + * placeholder?: string + * } $i18n + * @see https://editor.datatables.net/reference/field/tags#i18n + */ + public function i18n(array $i18n): static + { + $this->attributes['i18n'] = array_merge($this->attributes['i18n'] ?? [], $i18n); + + return $this; + } + + /** + * @see https://editor.datatables.net/reference/field/tags#i18n + */ + public function addButton(string $text): static + { + return $this->i18n(['addButton' => $text]); + } + + /** + * @see https://editor.datatables.net/reference/field/tags#i18n + */ + public function inputPlaceholder(string $text): static + { + return $this->i18n(['inputPlaceholder' => $text]); + } + + /** + * @see https://editor.datatables.net/reference/field/tags#i18n + */ + public function noResults(string $text): static + { + return $this->i18n(['noResults' => $text]); + } + + /** + * @see https://editor.datatables.net/reference/field/tags#i18n + */ + public function title(string $text): static + { + return $this->i18n(['title' => $text]); + } + + /** + * @see https://editor.datatables.net/reference/field/tags#i18n + */ + public function placeholder(string $text): static + { + return $this->i18n(['placeholder' => $text]); + } + + /** + * @see https://editor.datatables.net/reference/field/tags#limit + */ + public function limit(int $limit): static + { + $this->attributes['limit'] = $limit; + + return $this; + } + + /** + * @see https://editor.datatables.net/reference/field/tags#multiple + */ + public function multiple(bool $multiple = true): static + { + $this->attributes['multiple'] = $multiple; + + return $this; + } + + /** + * @see https://editor.datatables.net/reference/field/tags#options + */ + public function options(array|Arrayable $options): static + { + return parent::options($options); + } + + /** + * @see https://editor.datatables.net/reference/field/tags#separator + */ + public function separator(string $separator = ','): static + { + return parent::separator($separator); + } + + /** + * @see https://editor.datatables.net/reference/field/tags#unique + */ + public function unique(bool $unique = true): static + { + $this->attributes['unique'] = $unique; + + return $this; + } +} From 220f2d2c71f23a40cf553e83b6b87141b2a4ec08 Mon Sep 17 00:00:00 2001 From: yajra Date: Thu, 9 Jan 2025 07:43:13 +0000 Subject: [PATCH 2/8] fix: pint --- src/Html/Editor/Fields/Tags.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Html/Editor/Fields/Tags.php b/src/Html/Editor/Fields/Tags.php index 05dc8d9..b406def 100644 --- a/src/Html/Editor/Fields/Tags.php +++ b/src/Html/Editor/Fields/Tags.php @@ -49,6 +49,7 @@ public function escapeLabelHtml(bool $escape): static * title?: string * placeholder?: string * } $i18n + * * @see https://editor.datatables.net/reference/field/tags#i18n */ public function i18n(array $i18n): static From fde0ce44e18a615c746aab8164ede1329a1b26fb Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Thu, 9 Jan 2025 16:24:14 +0800 Subject: [PATCH 3/8] test: tags --- tests/Html/Editor/Fields/TagsTest.php | 131 ++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 tests/Html/Editor/Fields/TagsTest.php diff --git a/tests/Html/Editor/Fields/TagsTest.php b/tests/Html/Editor/Fields/TagsTest.php new file mode 100644 index 0000000..a087605 --- /dev/null +++ b/tests/Html/Editor/Fields/TagsTest.php @@ -0,0 +1,131 @@ +ajax('/tags'); + + $this->assertSame('/tags', $field->toArray()['ajax']); + } + + #[Test] + public function it_can_set_tags_display(): void + { + $field = new Tags; + $field->display('display'); + + $this->assertSame('display', $field->toArray()['display']); + } + + #[Test] + public function it_can_set_tags_escape_label_html(): void + { + $field = new Tags; + $field->escapeLabelHtml(true); + + $this->assertTrue($field->toArray()['escapeLabelHtml']); + } + + #[Test] + public function it_can_set_tags_i18n(): void + { + $field = new Tags; + $field->i18n([ + 'addButton' => 'Add', + 'inputPlaceholder' => 'Input', + 'noResults' => 'No Results', + 'title' => 'Title', + 'placeholder' => 'Placeholder', + ]); + + $this->assertSame('Add', $field->toArray()['i18n']['addButton']); + $this->assertSame('Input', $field->toArray()['i18n']['inputPlaceholder']); + $this->assertSame('No Results', $field->toArray()['i18n']['noResults']); + $this->assertSame('Title', $field->toArray()['i18n']['title']); + $this->assertSame('Placeholder', $field->toArray()['i18n']['placeholder']); + + $field->addButton('Add Button') + ->inputPlaceholder('Input Placeholder') + ->noResults('No Results X') + ->title('Title X') + ->placeholder('Placeholder X'); + + $this->assertSame('Add Button', $field->toArray()['i18n']['addButton']); + $this->assertSame('Input Placeholder', $field->toArray()['i18n']['inputPlaceholder']); + $this->assertSame('No Results X', $field->toArray()['i18n']['noResults']); + $this->assertSame('Title X', $field->toArray()['i18n']['title']); + $this->assertSame('Placeholder X', $field->toArray()['i18n']['placeholder']); + } + + #[Test] + public function it_can_set_tags_type(): void + { + $field = new Tags; + + $this->assertSame('tags', $field->toArray()['type']); + } + + #[Test] + public function it_can_set_tags_limit(): void + { + $field = new Tags; + $field->limit(2); + + $this->assertSame(2, $field->toArray()['limit']); + } + + #[Test] + public function it_can_set_tags_multiple(): void + { + $field = new Tags; + $field->multiple(); + + $this->assertTrue($field->toArray()['multiple']); + } + + #[Test] + public function it_can_set_tags_options(): void + { + $field = new Tags; + $field->options(['tag1', 'tag2']); + + $this->assertSame(['tag1', 'tag2'], $field->toArray()['options']); + + $field->options([ + ['value' => 'tag1', 'label' => 'Tag 1'], + ['value' => 'tag2', 'label' => 'Tag 2'], + ]); + + $this->assertSame([ + ['value' => 'tag1', 'label' => 'Tag 1'], + ['value' => 'tag2', 'label' => 'Tag 2'], + ], $field->toArray()['options']); + } + + #[Test] + public function it_can_set_tags_separator(): void + { + $field = new Tags; + $field->separator(','); + + $this->assertSame(',', $field->toArray()['separator']); + } + + #[Test] + public function it_can_set_tags_unique(): void + { + $field = new Tags; + $field->unique(); + + $this->assertTrue($field->toArray()['unique']); + } +} From 75fc79a90e5f50c3c7de55587e4cca5a04aa537d Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Thu, 9 Jan 2025 16:29:12 +0800 Subject: [PATCH 4/8] test: refactor --- tests/{ => Html/Builder}/BuilderOptionsLanguageTest.php | 3 ++- tests/{ => Html/Builder}/BuilderOptionsPluginsTest.php | 3 ++- tests/{ => Html/Builder}/BuilderOptionsTest.php | 3 ++- tests/{ => Html/Builder}/BuilderTest.php | 3 ++- tests/{ => Html/Builder}/LayoutTest.php | 3 ++- tests/{ => Html/Builder}/SearchPaneTest.php | 3 ++- tests/{ => Html/Column}/ColumnDefinitionTest.php | 3 ++- tests/{ => Html/Column}/ColumnTest.php | 3 ++- tests/{ => Html/Editor}/EditorFormOptionsTest.php | 3 ++- tests/{ => Html/Editor}/EditorTest.php | 3 ++- tests/{ => Html/Editor/Fields}/FieldOptionsTest.php | 3 ++- tests/{ => Html/Editor/Fields}/FieldTest.php | 3 ++- 12 files changed, 24 insertions(+), 12 deletions(-) rename tests/{ => Html/Builder}/BuilderOptionsLanguageTest.php (98%) rename tests/{ => Html/Builder}/BuilderOptionsPluginsTest.php (99%) rename tests/{ => Html/Builder}/BuilderOptionsTest.php (99%) rename tests/{ => Html/Builder}/BuilderTest.php (99%) rename tests/{ => Html/Builder}/LayoutTest.php (99%) rename tests/{ => Html/Builder}/SearchPaneTest.php (96%) rename tests/{ => Html/Column}/ColumnDefinitionTest.php (88%) rename tests/{ => Html/Column}/ColumnTest.php (98%) rename tests/{ => Html/Editor}/EditorFormOptionsTest.php (95%) rename tests/{ => Html/Editor}/EditorTest.php (98%) rename tests/{ => Html/Editor/Fields}/FieldOptionsTest.php (96%) rename tests/{ => Html/Editor/Fields}/FieldTest.php (99%) diff --git a/tests/BuilderOptionsLanguageTest.php b/tests/Html/Builder/BuilderOptionsLanguageTest.php similarity index 98% rename from tests/BuilderOptionsLanguageTest.php rename to tests/Html/Builder/BuilderOptionsLanguageTest.php index c65c45b..9567d16 100644 --- a/tests/BuilderOptionsLanguageTest.php +++ b/tests/Html/Builder/BuilderOptionsLanguageTest.php @@ -1,8 +1,9 @@ Date: Thu, 9 Jan 2025 16:31:53 +0800 Subject: [PATCH 5/8] test: search panes --- tests/Html/{Builder => Extensions}/SearchPaneTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/Html/{Builder => Extensions}/SearchPaneTest.php (97%) diff --git a/tests/Html/Builder/SearchPaneTest.php b/tests/Html/Extensions/SearchPaneTest.php similarity index 97% rename from tests/Html/Builder/SearchPaneTest.php rename to tests/Html/Extensions/SearchPaneTest.php index 6fe60a0..48fd975 100644 --- a/tests/Html/Builder/SearchPaneTest.php +++ b/tests/Html/Extensions/SearchPaneTest.php @@ -1,6 +1,6 @@ Date: Thu, 9 Jan 2025 16:47:43 +0800 Subject: [PATCH 6/8] ci: fix static analysis --- phpstan.neon.dist | 7 +++---- src/Html/Editor/Fields/Tags.php | 12 +++--------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 08605d3..92b6e18 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -10,9 +10,8 @@ parameters: ignoreErrors: - '#Unsafe usage of new static\(\).#' + - identifier: missingType.generics + - identifier: missingType.iterableValue excludePaths: - - checkMissingIterableValueType: false - - checkGenericClassInNonGenericObjectType: false + - ./src/Html/Fluent.php diff --git a/src/Html/Editor/Fields/Tags.php b/src/Html/Editor/Fields/Tags.php index b406def..d9112d1 100644 --- a/src/Html/Editor/Fields/Tags.php +++ b/src/Html/Editor/Fields/Tags.php @@ -42,19 +42,13 @@ public function escapeLabelHtml(bool $escape): static } /** - * @param array { - * addButton?: string - * inputPlaceholder?: string - * noResults?: string - * title?: string - * placeholder?: string - * } $i18n - * * @see https://editor.datatables.net/reference/field/tags#i18n */ public function i18n(array $i18n): static { - $this->attributes['i18n'] = array_merge($this->attributes['i18n'] ?? [], $i18n); + $options = (array) $this->attributes['i18n']; + + $this->attributes['i18n'] = array_merge($options, $i18n); return $this; } From 0136e8e00ba1e476ced1ff0f84046e6ae5d09637 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Thu, 9 Jan 2025 22:08:31 +0800 Subject: [PATCH 7/8] fix: error when i18n is not set --- src/Html/Editor/Fields/Tags.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Html/Editor/Fields/Tags.php b/src/Html/Editor/Fields/Tags.php index d9112d1..2b910be 100644 --- a/src/Html/Editor/Fields/Tags.php +++ b/src/Html/Editor/Fields/Tags.php @@ -46,7 +46,9 @@ public function escapeLabelHtml(bool $escape): static */ public function i18n(array $i18n): static { - $options = (array) $this->attributes['i18n']; + $options = isset($this->attributes['i18n']) + ? (array) $this->attributes['i18n'] + : []; $this->attributes['i18n'] = array_merge($options, $i18n); From bf85ccba57fd468781b2df95bd3dcafee1e735cc Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Thu, 9 Jan 2025 22:14:23 +0800 Subject: [PATCH 8/8] test: set i18n prop directly --- tests/Html/Editor/Fields/TagsTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Html/Editor/Fields/TagsTest.php b/tests/Html/Editor/Fields/TagsTest.php index a087605..da466e0 100644 --- a/tests/Html/Editor/Fields/TagsTest.php +++ b/tests/Html/Editor/Fields/TagsTest.php @@ -35,6 +35,14 @@ public function it_can_set_tags_escape_label_html(): void $this->assertTrue($field->toArray()['escapeLabelHtml']); } + #[Test] + public function it_can_set_tags_i18n_props_directly(): void + { + $field = new Tags; + $field->addButton('Add Tag'); + $this->assertSame('Add Tag', $field->toArray()['i18n']['addButton']); + } + #[Test] public function it_can_set_tags_i18n(): void {