From 870c75df1a4a47d0371acafdc610ba8f35e7e6c0 Mon Sep 17 00:00:00 2001 From: Matt Lantz Date: Thu, 14 Oct 2021 21:13:06 -0400 Subject: [PATCH] because of html refactoring --- src/Html/Button.php | 6 +- src/Html/Div.php | 4 +- src/Html/DivClose.php | 2 +- src/Html/DivOpen.php | 4 +- src/Html/Heading.php | 5 +- src/Html/HoneyPot.php | 4 +- src/Html/HrTag.php | 4 +- src/Html/HtmlSnippet.php | 17 +-- src/Html/Link.php | 6 +- src/Html/Span.php | 4 +- src/Services/FieldConfigProcessor.php | 38 ++++++- src/Services/FieldMaker.php | 2 +- src/Services/HtmlConfigProcessor.php | 158 ++++++++++++++++++++++++++ tests/FieldConfigProcessorTest.php | 20 +++- tests/HtmlConfigProcessorTest.php | 72 ++++++++++++ tests/HtmlSnippetTest.php | 15 +-- 16 files changed, 317 insertions(+), 44 deletions(-) create mode 100644 src/Services/HtmlConfigProcessor.php create mode 100644 tests/HtmlConfigProcessorTest.php diff --git a/src/Html/Button.php b/src/Html/Button.php index 9150a3e..38bca7b 100644 --- a/src/Html/Button.php +++ b/src/Html/Button.php @@ -6,11 +6,11 @@ class Button extends HtmlSnippet { - public static function content($options = []) + public static function render($options = []) { $options['class'] = $options['class'] ?? config('forms.buttons.submit'); - $attributes = self::attributes($options); + $attributes = self::attributes($options['attributes']); $content = ''; @@ -20,6 +20,6 @@ public static function content($options = []) throw_if(empty($content), 'You cannot have an empty button'); - return ""; + return "{$content}"; } } diff --git a/src/Html/Div.php b/src/Html/Div.php index 106f625..86b4a89 100644 --- a/src/Html/Div.php +++ b/src/Html/Div.php @@ -6,11 +6,11 @@ class Div extends HtmlSnippet { - public static function content($options = []) + public static function render($options = []) { $content = ''; - $attributes = self::attributes($options); + $attributes = self::attributes($options['attributes']); if (isset($options['content'])) { $content = $options['content']; diff --git a/src/Html/DivClose.php b/src/Html/DivClose.php index f054ae3..3397802 100644 --- a/src/Html/DivClose.php +++ b/src/Html/DivClose.php @@ -6,7 +6,7 @@ class DivClose extends HtmlSnippet { - public static function content($options = []) + public static function render($options = []) { return ''; } diff --git a/src/Html/DivOpen.php b/src/Html/DivOpen.php index 85260e2..03ba298 100644 --- a/src/Html/DivOpen.php +++ b/src/Html/DivOpen.php @@ -6,9 +6,9 @@ class DivOpen extends HtmlSnippet { - public static function content($options = []) + public static function render($options = []) { - $attributes = self::attributes($options); + $attributes = self::attributes($options['attributes']); return ""; } diff --git a/src/Html/Heading.php b/src/Html/Heading.php index 650b5c8..8948154 100644 --- a/src/Html/Heading.php +++ b/src/Html/Heading.php @@ -6,13 +6,12 @@ class Heading extends HtmlSnippet { - public static function content($options = []) + public static function render($options = []) { - $class = ''; $content = ''; $level = 3; - $attributes = self::attributes($options); + $attributes = self::attributes($options['attributes']); if (isset($options['content'])) { $content = $options['content']; diff --git a/src/Html/HoneyPot.php b/src/Html/HoneyPot.php index 594e6eb..454270e 100644 --- a/src/Html/HoneyPot.php +++ b/src/Html/HoneyPot.php @@ -11,7 +11,7 @@ public static function make($content = null, $name = null) { $options = [ 'type' => 'html', - 'content' => (string) self::content(), + 'content' => null, ]; if (is_null($name)) { @@ -21,7 +21,7 @@ public static function make($content = null, $name = null) return (new HtmlConfigProcessor($name, $options)); } - public static function content($options = []) + public static function render($options = []) { return view('honeypot::honeypotFormFields'); } diff --git a/src/Html/HrTag.php b/src/Html/HrTag.php index 19260b0..d711967 100644 --- a/src/Html/HrTag.php +++ b/src/Html/HrTag.php @@ -6,9 +6,9 @@ class HrTag extends HtmlSnippet { - public static function content($options = []) + public static function render($options = []) { - $attributes = self::attributes($options); + $attributes = self::attributes($options['attributes']); return ""; } diff --git a/src/Html/HtmlSnippet.php b/src/Html/HtmlSnippet.php index 1e1dbad..5b7b5aa 100644 --- a/src/Html/HtmlSnippet.php +++ b/src/Html/HtmlSnippet.php @@ -8,6 +8,8 @@ class HtmlSnippet { + public static $tag; + public static function getHtmlOptions() { return [ @@ -16,9 +18,9 @@ public static function getHtmlOptions() ]; } - public static function content($options = []) + public static function render($options = []) { - return []; + return ''; } public static function attributes($options = []) @@ -59,21 +61,20 @@ public static function attributeElement($key, $value) public static function make($content = null, $name = null) { - if (is_array($content) || is_null($content)) { - $content = static::content($content); - } - - throw_if(is_null($content), new Exception('Content cannot be null')); - if (is_null($name)) { $name = 'html-snippet-' . Str::uuid(); } $options = [ + 'instance' => new static(), 'type' => 'html', 'content' => $content, ]; + if (is_array($content)) { + $options = array_merge($options, $content); + } + return (new HtmlConfigProcessor($name, $options)); } } diff --git a/src/Html/Link.php b/src/Html/Link.php index 81d306b..95ebf58 100644 --- a/src/Html/Link.php +++ b/src/Html/Link.php @@ -4,11 +4,11 @@ class Link extends HtmlSnippet { - public static function content($options = []) + public static function render($options = []) { $options['class'] = $options['class'] ?? config('forms.buttons.submit'); - $attributes = self::attributes($options); + $attributes = self::attributes($options['attributes']); $content = ''; @@ -18,6 +18,6 @@ public static function content($options = []) throw_if(empty($content), 'You cannot have an empty button'); - return "{$content}"; + return "{$content}"; } } diff --git a/src/Html/Span.php b/src/Html/Span.php index 9e70a51..0306299 100644 --- a/src/Html/Span.php +++ b/src/Html/Span.php @@ -6,11 +6,11 @@ class Span extends HtmlSnippet { - public static function content($options = []) + public static function render($options = []) { $content = ''; - $attributes = self::attributes($options); + $attributes = self::attributes($options['attributes']); if (isset($options['content'])) { $content = $options['content']; diff --git a/src/Services/FieldConfigProcessor.php b/src/Services/FieldConfigProcessor.php index b840fcb..0b31e6a 100644 --- a/src/Services/FieldConfigProcessor.php +++ b/src/Services/FieldConfigProcessor.php @@ -26,6 +26,7 @@ class FieldConfigProcessor public $wrapper; public $table_class; public $label_class; + public $instance; public function __construct($name, $options) { @@ -79,7 +80,7 @@ public function __toString() { $config = $this->toArray(); - return app(FieldMaker::class)->make($this->name, $config); + return app(FieldMaker::class)->make($this->name, $config, $this->instance); } public function required() @@ -159,6 +160,34 @@ public function model($value) return $this; } + public function modelMethod($value) + { + $this->model_options['model_method'] = $value; + + return $this; + } + + public function modelParams($value) + { + $this->model_options['model_params'] = $value; + + return $this; + } + + public function modelValue($value) + { + $this->model_options['model_value'] = $value; + + return $this; + } + + public function modelLabel($value) + { + $this->model_options['model_label'] = $value; + + return $this; + } + public function groupClass($value) { $this->wrapper = $value; @@ -399,4 +428,11 @@ public function selectOptions($array) return $this; } + + public function instance($value) + { + $this->instance = $value; + + return $this; + } } diff --git a/src/Services/FieldMaker.php b/src/Services/FieldMaker.php index 2fc5d40..1f82362 100644 --- a/src/Services/FieldMaker.php +++ b/src/Services/FieldMaker.php @@ -69,7 +69,7 @@ public function make(string $column, array $columnConfig, $object = null) ->setLivewireOnKeydown($this->livewireOnKeydown); if ($columnConfig['type'] === 'html') { - return $columnConfig['content']; + return $columnConfig['instance']::render($columnConfig); } $field = null; diff --git a/src/Services/HtmlConfigProcessor.php b/src/Services/HtmlConfigProcessor.php new file mode 100644 index 0000000..36677ec --- /dev/null +++ b/src/Services/HtmlConfigProcessor.php @@ -0,0 +1,158 @@ +name = $name; + + $this->processOptions($options); + } + + protected function processOptions($options) + { + $this->instance = $options['instance'] ?? null; + $this->type = $options['type'] ?? 'html'; + $this->options = $options['options'] ?? []; + $this->content = $options['content'] ?? null; + $this->attributes = $options['attributes'] ?? []; + $this->level = $options['level'] ?? null; + + if (isset($options['class'])) { + $this->attributes([ + 'class' => $options['class'], + ]); + } + + if (isset($options['href'])) { + $this->attributes([ + 'href' => $options['href'], + ]); + } + } + + public function toArray() + { + return get_object_vars($this); + } + + public function __toString() + { + $config = $this->toArray(); + + return app(FieldMaker::class)->make($this->name, $config); + } + + public function attributes($value) + { + $this->attributes = array_merge($this->attributes, $value); + + return $this; + } + + public function disabled() + { + $this->attributes = array_merge($this->attributes, [ + 'disabled' => true + ]); + + return $this; + } + + public function cssClass($value) + { + $this->attributes = array_merge($this->attributes, [ + 'class' => $value + ]); + + return $this; + } + + public function level($value) + { + $this->level = $value; + + return $this; + } + + public function href($value) + { + $this->attributes = array_merge($this->attributes, [ + 'href' => $value + ]); + + return $this; + } + + public function name($value) + { + $this->name = $value; + + return $this; + } + + public function id($value) + { + $this->attributes = array_merge($this->attributes, [ + 'id' => $value + ]); + + return $this; + } + + public function style($value) + { + $this->attributes = array_merge($this->attributes, [ + 'style' => $value + ]); + + return $this; + } + + public function data($key, $value) + { + $this->attributes = array_merge($this->attributes, [ + 'data-'.$key => $value + ]); + + return $this; + } + + public function hidden($value) + { + $this->attributes = array_merge($this->attributes, [ + 'hidden' => $value + ]); + + return $this; + } + + public function title($value) + { + $this->attributes = array_merge($this->attributes, [ + 'title' => $value + ]); + + return $this; + } + + public function onClick($value) + { + $this->attributes = array_merge($this->attributes, [ + 'onclick' => $value + ]); + + return $this; + } +} diff --git a/tests/FieldConfigProcessorTest.php b/tests/FieldConfigProcessorTest.php index 38c20c4..79a6f72 100644 --- a/tests/FieldConfigProcessorTest.php +++ b/tests/FieldConfigProcessorTest.php @@ -2,8 +2,10 @@ use Grafite\Forms\Fields\File; use Grafite\Forms\Fields\Text; -use Grafite\Forms\Fields\Select; +use Grafite\Forms\Fields\Select ; use Grafite\Forms\Fields\Checkbox; +use Illuminate\Foundation\Auth\User; +use Illuminate\Support\Facades\Hash; class FieldConfigProcessorTest extends TestCase { @@ -39,7 +41,7 @@ public function testTitle() { $field = Text::make('field')->title('user-field'); - $this->assertEquals('
', (string) $field); + $this-> assertEquals('
', (string) $field); } public function testStyle() @@ -292,5 +294,17 @@ public function testPlaceholder() $this->assertEquals('
', (string) $field); } - // model + public function testModel() + { + $user = new User(); + $user->name = 'Bruce'; + $user->email = 'batman@wayneenterprises.com'; + $user->password = Hash::make('beatTheJoker'); + + $user->save(); + + $field = Text::make('name')->instance(User::find(1)); + + $this->assertStringContainsString('Bruce', (string) $field); + } } diff --git a/tests/HtmlConfigProcessorTest.php b/tests/HtmlConfigProcessorTest.php new file mode 100644 index 0000000..da2ee19 --- /dev/null +++ b/tests/HtmlConfigProcessorTest.php @@ -0,0 +1,72 @@ +id('batmanLink') + ->href('batman.com'); + + $this->assertEquals('Click here for more info!', (string) $html); + } + + public function testDiv() + { + $html = Div::make('

Something goes here

') + ->id('div-123'); + + $this->assertEquals('

Something goes here

', (string) $html); + } + + public function testHeading() + { + $html = Heading::make('Heading!')->level(1)->cssClass('hero'); + + $this->assertEquals('

Heading!

', (string) $html); + } + + public function testCloseDiv() + { + $html = DivClose::make(); + + $this->assertEquals('', (string) $html); + } + + public function testSpan() + { + $html = Span::make('who')->cssClass('are-you'); + + $this->assertEquals('who', (string) $html); + } + + public function testButton() + { + $html = Button::make('click Me!')->cssClass('are-you'); + + $this->assertEquals('', (string) $html); + } + + public function testHrTag() + { + $html = HrTag::make()->cssClass('are-you'); + + $this->assertEquals('
', (string) $html); + } + + public function testButtonOnClick() + { + $html = Button::make('click Me!')->cssClass('are-you')->onClick('window.location.reload();'); + + $this->assertEquals('', (string) $html); + } +} diff --git a/tests/HtmlSnippetTest.php b/tests/HtmlSnippetTest.php index fedb220..d9ab53f 100644 --- a/tests/HtmlSnippetTest.php +++ b/tests/HtmlSnippetTest.php @@ -13,14 +13,8 @@ public function testHtmlSnippet() { $snippet = HtmlSnippet::make('
')->toArray(); - // dd($snippet); - $this->assertEquals('html', $snippet['type']); $this->assertEquals('
', $snippet['content']); - - // $snippet = HtmlSnippet::make('
'); - - // dd((string) $snippet); } public function testDivOpen() @@ -28,7 +22,7 @@ public function testDivOpen() $snippet = DivOpen::make(['class' => 'card'])->toArray(); $this->assertEquals('html', $snippet['type']); - $this->assertEquals('
', $snippet['content']); + $this->assertEquals('card', $snippet['attributes']['class']); } public function testDivClose() @@ -36,7 +30,6 @@ public function testDivClose() $snippet = DivClose::make()->toArray(); $this->assertEquals('html', $snippet['type']); - $this->assertEquals('
', $snippet['content']); } public function testHrTag() @@ -44,7 +37,7 @@ public function testHrTag() $snippet = HrTag::make()->toArray(); $this->assertEquals('html', $snippet['type']); - $this->assertEquals('
', $snippet['content']); + // $this->assertEquals('
', $snippet['content']); } public function testHeadingTag() @@ -54,7 +47,7 @@ public function testHeadingTag() ])->toArray(); $this->assertEquals('html', $snippet['type']); - $this->assertEquals('

Billing Details

', $snippet['content']); + $this->assertEquals('Billing Details', $snippet['content']); } public function testDivTag() @@ -64,6 +57,6 @@ public function testDivTag() ])->toArray(); $this->assertEquals('html', $snippet['type']); - $this->assertEquals('

Bar

', $snippet['content']); + $this->assertEquals('

Bar

', $snippet['content']); } }