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 new file mode 100644 index 0000000..2b910be --- /dev/null +++ b/src/Html/Editor/Fields/Tags.php @@ -0,0 +1,143 @@ +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; + } + + /** + * @see https://editor.datatables.net/reference/field/tags#i18n + */ + public function i18n(array $i18n): static + { + $options = isset($this->attributes['i18n']) + ? (array) $this->attributes['i18n'] + : []; + + $this->attributes['i18n'] = array_merge($options, $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; + } +} 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 @@ 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_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 + { + $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']); + } +} diff --git a/tests/SearchPaneTest.php b/tests/Html/Extensions/SearchPaneTest.php similarity index 96% rename from tests/SearchPaneTest.php rename to tests/Html/Extensions/SearchPaneTest.php index d32716a..48fd975 100644 --- a/tests/SearchPaneTest.php +++ b/tests/Html/Extensions/SearchPaneTest.php @@ -1,11 +1,12 @@