From d724953f203ee82f21756e887cebbeee72d5d0f3 Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Wed, 30 Dec 2015 00:17:06 +0000 Subject: [PATCH 1/9] Updating travis CI & Scrutinizer config files --- .scrutinizer.yml | 2 +- .travis.yml | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 69d4755..cacb217 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -22,7 +22,7 @@ checks: tools: external_code_coverage: timeout: 600 - runs: 4 + runs: 8 php_code_sniffer: enabled: true config: diff --git a/.travis.yml b/.travis.yml index 46db9c3..21ebd31 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: php +sudo: false + php: - 5.5.9 - 5.5 @@ -7,11 +9,13 @@ php: - 7.0 - hhvm -sudo: false +env: + - TESTBENCH_VERSION=3.1.* + - TESTBENCH_VERSION=3.2.* before_script: - travis_retry composer self-update - - travis_retry composer install --prefer-source --no-interaction + - travis_retry composer require --prefer-source --no-interaction --dev "orchestra/testbench:${TESTBENCH_VERSION}" script: - composer validate From 61e4c33dbb4b8a6ab362bf423c16fa941adc623c Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Wed, 30 Dec 2015 00:17:21 +0000 Subject: [PATCH 2/9] Updating composer.json --- composer.json | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index e03955f..4da5756 100644 --- a/composer.json +++ b/composer.json @@ -7,19 +7,17 @@ { "name": "ARCANEDEV", "email": "arcanedev.maroc@gmail.com", - "homepage": "https://github.com/ARCANEDEV" + "homepage": "https://github.com/arcanedev-maroc", + "role": "Developer" } ], "type": "library", "license": "MIT", "require": { "php": ">=5.5.9", - "arcanedev/support": "~3.0", - "illuminate/routing": "~5.2.0", - "illuminate/session": "~5.2.0" + "arcanedev/support": "~3.0" }, "require-dev": { - "orchestra/testbench": "~3.2.0", "phpunit/phpcov": "~2.0", "phpunit/phpunit": "~4.0|~5.0" }, @@ -34,6 +32,9 @@ "Arcanedev\\LaravelHtml\\Tests\\": "tests/" } }, + "scripts": { + "testbench": "composer require --dev \"orchestra/testbench=~3.0\"" + }, "extra": { "branch-alias": { "dev-master": "5.3-dev" From 04f68a3c02c0ed1f9d1dd9dc620c9788dceefc37 Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Wed, 30 Dec 2015 01:45:25 +0000 Subject: [PATCH 3/9] Adding Componentable --- src/Traits/Componentable.php | 119 +++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/Traits/Componentable.php diff --git a/src/Traits/Componentable.php b/src/Traits/Componentable.php new file mode 100644 index 0000000..bb63b17 --- /dev/null +++ b/src/Traits/Componentable.php @@ -0,0 +1,119 @@ + + */ +trait Componentable +{ + /* ------------------------------------------------------------------------------------------------ + | Properties + | ------------------------------------------------------------------------------------------------ + */ + /** + * The registered components. + * + * @var array + */ + protected static $components = []; + + /* ------------------------------------------------------------------------------------------------ + | Main Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Register a custom component. + * + * @param string $name + * @param string $view + * @param array $signature + */ + public static function component($name, $view, array $signature) + { + static::$components[$name] = compact('view', 'signature'); + } + + /** + * Check if a component is registered. + * + * @param string $name + * + * @return bool + */ + public static function hasComponent($name) + { + return isset(static::$components[$name]); + } + + /* ------------------------------------------------------------------------------------------------ + | Other Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Render a custom component. + * + * @param string $name + * @param array $arguments + * + * @return \Illuminate\Contracts\View\View + */ + protected function renderComponent($name, array $arguments) + { + $component = static::$components[$name]; + $data = $this->getComponentData($component['signature'], $arguments); + + return new HtmlString( + view($component['view'], $data)->render() + ); + } + + /** + * Prepare the component data, while respecting provided defaults. + * + * @param array $signature + * @param array $arguments + * + * @return array + */ + protected function getComponentData(array $signature, array $arguments) + { + $data = []; + $i = 0; + + foreach ($signature as $variable => $default) { + if (is_numeric($variable)) { + $variable = $default; + $default = null; + } + + $data[$variable] = array_get($arguments, $i, $default); + $i++; + } + + return $data; + } + + /** + * Dynamically handle calls to the class. + * + * @param string $method + * @param array $parameters + * + * @return \Illuminate\Contracts\View\View|mixed + * + * @throws \BadMethodCallException + */ + public function __call($method, $parameters) + { + if (static::hasComponent($method)) { + return $this->renderComponent($method, $parameters); + } + + throw new BadMethodCallException("Method {$method} does not exist."); + } +} From 3830356bda93b112acfd4cc2b5d970fc60b7421f Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Wed, 30 Dec 2015 01:45:43 +0000 Subject: [PATCH 4/9] Adding FormAccessible Trait --- src/Traits/FormAccessible.php | 143 ++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 src/Traits/FormAccessible.php diff --git a/src/Traits/FormAccessible.php b/src/Traits/FormAccessible.php new file mode 100644 index 0000000..04b06f2 --- /dev/null +++ b/src/Traits/FormAccessible.php @@ -0,0 +1,143 @@ + + */ +trait FormAccessible +{ + /* ------------------------------------------------------------------------------------------------ + | Properties + | ------------------------------------------------------------------------------------------------ + */ + /** + * A cached ReflectionClass instance for $this. + * + * @var ReflectionClass + */ + protected $reflection; + + /* ------------------------------------------------------------------------------------------------ + | Main Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Get form value from the eloquent model. + * + * @param string $key + * + * @return mixed + */ + public function getFormValue($key) + { + $value = $this->getAttributeFromArray($key); + + if (in_array($key, $this->getDates()) && ! is_null($value)) { + $value = $this->asDateTime($value); + } + + if ($this->hasFormMutator($key)) { + $value = $this->mutateFormAttribute($key, $value); + } + + return $value; + } + + /* ------------------------------------------------------------------------------------------------ + | Eloquent Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Get an attribute from the $attributes array. + * + * @param string $key + * + * @return mixed + */ + abstract protected function getAttributeFromArray($key); + + /** + * Get the attributes that should be converted to dates. + * + * @return array + */ + abstract public function getDates(); + + /** + * Return a timestamp as DateTime object. + * + * @param mixed $value + * + * @return \Carbon\Carbon + */ + abstract protected function asDateTime($value); + + /* ------------------------------------------------------------------------------------------------ + | Other Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Check if has a form mutator. + * + * @param string $key + * + * @return bool + */ + protected function hasFormMutator($key) + { + $methods = $this->getReflection()->getMethods( + ReflectionMethod::IS_PUBLIC + ); + + $mutator = collect($methods) + ->first(function ($index, ReflectionMethod $method) use ($key) { + return $method->name === $this->getMutateFromMethodName($key); + }); + + return (bool) $mutator; + } + + /** + * Mutate the form attribute. + * + * @param string $key + * @param mixed $value + * + * @return mixed + */ + private function mutateFormAttribute($key, $value) + { + return $this->{$this->getMutateFromMethodName($key)}($value); + } + + /** + * Get the mutate form method name. + * + * @param string $key + * + * @return string + */ + private function getMutateFromMethodName($key) + { + return 'form' . str_studly($key) . 'Attribute'; + } + + /** + * Get a ReflectionClass Instance. + * + * @return ReflectionClass + */ + protected function getReflection() + { + if ( ! $this->reflection) { + $this->reflection = new ReflectionClass($this); + } + + return $this->reflection; + } +} From 52c12ae7f824870deeb79c2f0b11063b6d9b6f7b Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Wed, 30 Dec 2015 01:46:45 +0000 Subject: [PATCH 5/9] Updating the base TestCase --- tests/TestCase.php | 28 +++++++++++++++++++ .../2015_01_01_000001_create_models_table.php | 28 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 tests/migrations/2015_01_01_000001_create_models_table.php diff --git a/tests/TestCase.php b/tests/TestCase.php index e4ab20d..5c8b4a6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -82,10 +82,38 @@ protected function getPackageAliases($app) ]; } + /** + * Define environment setup. + * + * @param \Illuminate\Foundation\Application $app + */ + protected function getEnvironmentSetUp($app) + { + // Setup default database to use sqlite :memory: + $app['config']->set('database.default', 'testbench'); + $app['config']->set('database.connections.testbench', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + } + + /* ------------------------------------------------------------------------------------------------ | Other Functions | ------------------------------------------------------------------------------------------------ */ + /** + * Migrate the database. + */ + protected function migrate() + { + $this->artisan('migrate', [ + '--database' => 'testbench', + '--realpath' => realpath(__DIR__ . '/migrations'), + ]); + } + /** * Register routes for tests. * diff --git a/tests/migrations/2015_01_01_000001_create_models_table.php b/tests/migrations/2015_01_01_000001_create_models_table.php new file mode 100644 index 0000000..2ba94c3 --- /dev/null +++ b/tests/migrations/2015_01_01_000001_create_models_table.php @@ -0,0 +1,28 @@ + + */ +class CreateModelsTable extends Migration +{ + public function up() + { + Schema::create('models', function (Blueprint $table) { + $table->increments('id'); + $table->string('string'); + $table->string('email'); + $table->timestamps(); + }); + } + + public function down() + { + Schema::drop('models'); + } +} From ed07e87a93e8a013fa18919144f2c1ba0e44563b Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Wed, 30 Dec 2015 01:47:03 +0000 Subject: [PATCH 6/9] Adding Stub Models --- tests/Stubs/ModelThatDoesntUseForms.php | 32 ++++++++++++++++ tests/Stubs/ModelThatUsesForms.php | 50 +++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 tests/Stubs/ModelThatDoesntUseForms.php create mode 100644 tests/Stubs/ModelThatUsesForms.php diff --git a/tests/Stubs/ModelThatDoesntUseForms.php b/tests/Stubs/ModelThatDoesntUseForms.php new file mode 100644 index 0000000..21ff974 --- /dev/null +++ b/tests/Stubs/ModelThatDoesntUseForms.php @@ -0,0 +1,32 @@ + + */ +class ModelThatDoesntUseForms extends Model +{ + /* ------------------------------------------------------------------------------------------------ + | Properties + | ------------------------------------------------------------------------------------------------ + */ + protected $table = 'models'; + + /* ------------------------------------------------------------------------------------------------ + | Other Functions + | ------------------------------------------------------------------------------------------------ + */ + public function getStringAttribute($value) + { + return strtoupper($value); + } + + public function getCreatedAtAttribute($value) + { + return '1 second ago'; + } +} diff --git a/tests/Stubs/ModelThatUsesForms.php b/tests/Stubs/ModelThatUsesForms.php new file mode 100644 index 0000000..6d175a3 --- /dev/null +++ b/tests/Stubs/ModelThatUsesForms.php @@ -0,0 +1,50 @@ + + */ +class ModelThatUsesForms extends Model +{ + /* ------------------------------------------------------------------------------------------------ + | Traits + | ------------------------------------------------------------------------------------------------ + */ + use FormAccessible; + + /* ------------------------------------------------------------------------------------------------ + | Properties + | ------------------------------------------------------------------------------------------------ + */ + protected $table = 'models'; + + /* ------------------------------------------------------------------------------------------------ + | Other Functions + | ------------------------------------------------------------------------------------------------ + */ + public function formStringAttribute($value) + { + return strrev($value); + } + + public function getStringAttribute($value) + { + return strtoupper($value); + } + + public function formCreatedAtAttribute(Carbon $value) + { + return $value->timestamp; + } + + public function getCreatedAtAttribute($value) + { + return '1 second ago'; + } +} From 373e0512eb4dd6d5deacf8ad5ab2974af947ebca Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Wed, 30 Dec 2015 01:47:18 +0000 Subject: [PATCH 7/9] Testing the FromAccessible Trait --- tests/Traits/FormAccessible.php | 107 ++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 tests/Traits/FormAccessible.php diff --git a/tests/Traits/FormAccessible.php b/tests/Traits/FormAccessible.php new file mode 100644 index 0000000..44477aa --- /dev/null +++ b/tests/Traits/FormAccessible.php @@ -0,0 +1,107 @@ + + */ +class FormAccessible extends TestCase +{ + /* ------------------------------------------------------------------------------------------------ + | Properties + | ------------------------------------------------------------------------------------------------ + */ + /** + * The model data. + * + * @var array + */ + protected $modelData = []; + + /** + * The Carbon instance. + * + * @var \Carbon\Carbon + */ + protected $now; + + /** + * The Form builder instance. + * + * @var \Arcanedev\LaravelHtml\FormBuilder + */ + protected $form; + + /* ------------------------------------------------------------------------------------------------ + | Main Functions + | ------------------------------------------------------------------------------------------------ + */ + public function setUp() + { + parent::setUp(); + + $this->migrate(); + + Model::unguard(); + + $this->now = Carbon::now(); + $this->modelData = [ + 'string' => 'abcdefghijklmnop', + 'email' => 'tj@tjshafer.com', + 'created_at' => $this->now, + 'updated_at' => $this->now, + ]; + + $this->form = new FormBuilder($this->htmlBuilder, $this->urlGenerator, 'abc'); + } + + public function tearDown() + { + $this->artisan('migrate:reset'); + + parent::tearDown(); + } + + + /* ------------------------------------------------------------------------------------------------ + | Test Functions + | ------------------------------------------------------------------------------------------------ + */ + /** @test */ + public function it_can_mutate_values_for_forms() + { + $model = new ModelThatUsesForms($this->modelData); + $this->form->setModel($model); + + $this->assertEquals($model->getFormValue('string'), 'ponmlkjihgfedcba'); + $this->assertEquals($model->getFormValue('created_at'), $this->now->timestamp); + } + + /** @test */ + public function it_can_still_mutate_values_for_views() + { + $model = new ModelThatUsesForms($this->modelData); + $this->form->setModel($model); + + $this->assertEquals($model->string, 'ABCDEFGHIJKLMNOP'); + $this->assertEquals($model->created_at, '1 second ago'); + } + + /** @test */ + public function it_doesnt_require_the_use_of_this_feature() + { + $model = new ModelThatDoesntUseForms($this->modelData); + $this->form->setModel($model); + + $this->assertEquals($model->string, 'ABCDEFGHIJKLMNOP'); + $this->assertEquals($model->created_at, '1 second ago'); + } +} From bf2b8cfd3add719c84f94ec549e815fb70517c67 Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Wed, 30 Dec 2015 04:06:13 +0000 Subject: [PATCH 8/9] Adding the fixtures files --- tests/TestCase.php | 14 +++++++++++--- .../2015_01_01_000001_create_models_table.php | 0 tests/fixtures/views/_components/text.blade.php | 4 ++++ 3 files changed, 15 insertions(+), 3 deletions(-) rename tests/{ => fixtures}/migrations/2015_01_01_000001_create_models_table.php (100%) create mode 100644 tests/fixtures/views/_components/text.blade.php diff --git a/tests/TestCase.php b/tests/TestCase.php index 5c8b4a6..9fc2a96 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -89,13 +89,21 @@ protected function getPackageAliases($app) */ protected function getEnvironmentSetUp($app) { + /** @var \Illuminate\Contracts\Config\Repository $config */ + $config = $app['config']; + // Setup default database to use sqlite :memory: - $app['config']->set('database.default', 'testbench'); - $app['config']->set('database.connections.testbench', [ + $config->set('database.default', 'testbench'); + $config->set('database.connections.testbench', [ 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', ]); + + $viewPaths = $config->get('view.paths'); + $viewPaths[] = __DIR__ . '/fixtures/views'; + + $config->set('view.paths', array_map('realpath', $viewPaths)); } @@ -110,7 +118,7 @@ protected function migrate() { $this->artisan('migrate', [ '--database' => 'testbench', - '--realpath' => realpath(__DIR__ . '/migrations'), + '--realpath' => realpath(__DIR__ . '/fixtures/migrations'), ]); } diff --git a/tests/migrations/2015_01_01_000001_create_models_table.php b/tests/fixtures/migrations/2015_01_01_000001_create_models_table.php similarity index 100% rename from tests/migrations/2015_01_01_000001_create_models_table.php rename to tests/fixtures/migrations/2015_01_01_000001_create_models_table.php diff --git a/tests/fixtures/views/_components/text.blade.php b/tests/fixtures/views/_components/text.blade.php new file mode 100644 index 0000000..b85781a --- /dev/null +++ b/tests/fixtures/views/_components/text.blade.php @@ -0,0 +1,4 @@ +
+ {!! Form::label($name, null, ['class' => 'control-label']) !!} + {!! Form::text($name, $value, array_merge(['class' => 'form-control'], $attributes)) !!} +
From 7011e6d8ce5826bafd5186797d6c8ea2661bcf93 Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Wed, 30 Dec 2015 01:46:13 +0000 Subject: [PATCH 9/9] Updating the Builders --- src/Bases/Builder.php | 66 +++++++++++++++++++++++++++ src/FormBuilder.php | 96 +++++++++++++++++++-------------------- src/HtmlBuilder.php | 85 +++++++++++++++++++--------------- tests/FormBuilderTest.php | 30 +++++++++--- tests/HtmlBuilderTest.php | 29 ++++++++++-- 5 files changed, 208 insertions(+), 98 deletions(-) create mode 100644 src/Bases/Builder.php diff --git a/src/Bases/Builder.php b/src/Bases/Builder.php new file mode 100644 index 0000000..f3da814 --- /dev/null +++ b/src/Bases/Builder.php @@ -0,0 +1,66 @@ + + */ +abstract class Builder +{ + /* ------------------------------------------------------------------------------------------------ + | Traits + | ------------------------------------------------------------------------------------------------ + */ + use Macroable, Componentable { + Macroable::__call as macroCall; + Componentable::__call as componentCall; + } + + /* ------------------------------------------------------------------------------------------------ + | Main Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Dynamically handle calls to the class. + * + * @param string $method + * @param array $parameters + * + * @return \Illuminate\Contracts\View\View|mixed + * + * @throws \BadMethodCallException + */ + public function __call($method, $parameters) + { + try { + return $this->componentCall($method, $parameters); + } + catch (BadMethodCallException $e) { + // Continue + } + + return $this->macroCall($method, $parameters); + } + + /* ------------------------------------------------------------------------------------------------ + | Other Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Transform the string to an Html serializable object + * + * @param string $html + * + * @return \Illuminate\Support\HtmlString + */ + protected function toHtmlString($html) + { + return new HtmlString($html); + } +} diff --git a/src/FormBuilder.php b/src/FormBuilder.php index b66d9f6..bc865a5 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -1,11 +1,11 @@ */ -class FormBuilder implements FormBuilderInterface +class FormBuilder extends Builder implements FormBuilderInterface { - /* ------------------------------------------------------------------------------------------------ - | Traits - | ------------------------------------------------------------------------------------------------ - */ - use Macroable; - /* ------------------------------------------------------------------------------------------------ | Properties | ------------------------------------------------------------------------------------------------ @@ -307,17 +301,17 @@ public function open(array $options = []) // different method than it actually is, for convenience from the forms. $append = $this->getAppendage($method); - return '' . $append; + return $this->toHtmlString('' . $append); } /** - * Create a new model based form builder. - * - * @param mixed $model - * @param array $options - * - * @return string - */ + * Create a new model based form builder. + * + * @param mixed $model + * @param array $options + * + * @return \Illuminate\Support\HtmlString + */ public function model($model, array $options = []) { $this->setModel($model); @@ -328,20 +322,20 @@ public function model($model, array $options = []) /** * Close the current form. * - * @return string + * @return \Illuminate\Support\HtmlString */ public function close() { $this->labels = []; $this->setModel(null); - return ''; + return $this->toHtmlString(''); } /** * Generate a hidden field with the current CSRF token. * - * @return string + * @return \Illuminate\Support\HtmlString */ public function token() { @@ -359,13 +353,13 @@ public function token() * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function label($name, $value = null, array $options = []) { $this->labels[] = $name; - return Helpers\Label::make($name, $value, $options); + return $this->toHtmlString(Helpers\Label::make($name, $value, $options)); } /** @@ -376,7 +370,7 @@ public function label($name, $value = null, array $options = []) * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function input($type, $name, $value = null, array $options = []) { @@ -398,7 +392,7 @@ public function input($type, $name, $value = null, array $options = []) // when creating the HTML element. Then, we will return the entire input. $options = array_merge($options, compact('type', 'value', 'id')); - return 'html->attributes($options) . '>'; + return $this->toHtmlString('html->attributes($options) . '>'); } /** @@ -408,7 +402,7 @@ public function input($type, $name, $value = null, array $options = []) * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function text($name, $value = null, array $options = []) { @@ -421,7 +415,7 @@ public function text($name, $value = null, array $options = []) * @param string $name * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function password($name, array $options = []) { @@ -435,7 +429,7 @@ public function password($name, array $options = []) * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function hidden($name, $value = null, array $options = []) { @@ -449,7 +443,7 @@ public function hidden($name, $value = null, array $options = []) * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function email($name, $value = null, array $options = []) { @@ -463,7 +457,7 @@ public function email($name, $value = null, array $options = []) * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function tel($name, $value = null, array $options = []) { @@ -477,7 +471,7 @@ public function tel($name, $value = null, array $options = []) * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function number($name, $value = null, array $options = []) { @@ -491,7 +485,7 @@ public function number($name, $value = null, array $options = []) * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function date($name, $value = null, array $options = []) { @@ -509,7 +503,7 @@ public function date($name, $value = null, array $options = []) * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function datetime($name, $value = null, array $options = []) { @@ -527,7 +521,7 @@ public function datetime($name, $value = null, array $options = []) * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function datetimeLocal($name, $value = null, array $options = []) { @@ -545,7 +539,7 @@ public function datetimeLocal($name, $value = null, array $options = []) * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function time($name, $value = null, array $options = []) { @@ -559,7 +553,7 @@ public function time($name, $value = null, array $options = []) * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function url($name, $value = null, array $options = []) { @@ -572,7 +566,7 @@ public function url($name, $value = null, array $options = []) * @param string $name * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function file($name, array $options = []) { @@ -586,7 +580,7 @@ public function file($name, array $options = []) * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function textarea($name, $value = null, array $options = []) { @@ -608,7 +602,7 @@ public function textarea($name, $value = null, array $options = []) // the element. Then we'll create the final textarea elements HTML for us. $options = $this->html->attributes($options); - return '' . e($value) . ''; + return $this->toHtmlString('' . e($value) . ''); } /** @@ -658,7 +652,7 @@ protected function setQuickTextAreaSize(array $options) * @param string $selected * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function select($name, $list = [], $selected = null, array $options = []) { @@ -697,7 +691,7 @@ public function select($name, $list = [], $selected = null, array $options = []) // build out a final select statement, which will contain all the values. $options = $this->html->attributes($options); - return "" . implode('', $html) . ""; + return $this->toHtmlString("" . implode('', $html) . ""); } /** @@ -709,7 +703,7 @@ public function select($name, $list = [], $selected = null, array $options = []) * @param string $selected * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function selectRange($name, $begin, $end, $selected = null, array $options = []) { @@ -727,7 +721,7 @@ public function selectRange($name, $begin, $end, $selected = null, array $option * @param string $selected * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function selectYear($name, $begin, $end, $selected = null, array $options = []) { @@ -745,7 +739,7 @@ public function selectYear($name, $begin, $end, $selected = null, array $options * @param array $options * @param string $format * - * @return string + * @return \Illuminate\Support\HtmlString */ public function selectMonth($name, $selected = null, array $options = [], $format = '%B') { @@ -855,7 +849,7 @@ private function getSelectedValue($value, $selected) * @param bool|null $checked * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function checkbox($name, $value = 1, $checked = null, array $options = []) { @@ -870,7 +864,7 @@ public function checkbox($name, $value = 1, $checked = null, array $options = [] * @param bool $checked * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function radio($name, $value = null, $checked = null, array $options = []) { @@ -890,7 +884,7 @@ public function radio($name, $value = null, $checked = null, array $options = [] * @param bool|null $checked * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ protected function checkable($type, $name, $value, $checked, array $options) { @@ -997,7 +991,7 @@ private function missingOldAndModel($name) * @param string $value * @param array $attributes * - * @return string + * @return \Illuminate\Support\HtmlString */ public function reset($value, array $attributes = []) { @@ -1011,7 +1005,7 @@ public function reset($value, array $attributes = []) * @param string $name * @param array $attributes * - * @return string + * @return \Illuminate\Support\HtmlString */ public function image($url, $name = null, array $attributes = []) { @@ -1026,7 +1020,7 @@ public function image($url, $name = null, array $attributes = []) * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function submit($value = null, array $options = []) { @@ -1039,7 +1033,7 @@ public function submit($value = null, array $options = []) * @param string $value * @param array $options * - * @return string + * @return \Illuminate\Support\HtmlString */ public function button($value = null, array $options = []) { @@ -1047,7 +1041,9 @@ public function button($value = null, array $options = []) $options['type'] = 'button'; } - return 'html->attributes($options) . '>' . $value . ''; + return $this->toHtmlString( + 'html->attributes($options) . '>' . $value . '' + ); } /** diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index e92370c..5e84224 100644 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -1,8 +1,8 @@ */ -class HtmlBuilder implements HtmlBuilderInterface +class HtmlBuilder extends Builder implements HtmlBuilderInterface { - /* ------------------------------------------------------------------------------------------------ - | Traits - | ------------------------------------------------------------------------------------------------ - */ - use Macroable; - /* ------------------------------------------------------------------------------------------------ | Properties | ------------------------------------------------------------------------------------------------ @@ -78,13 +72,15 @@ public function decode($value) * @param array $attributes * @param bool $secure * - * @return string + * @return \Illuminate\Support\HtmlString */ public function script($url, $attributes = [], $secure = null) { $attributes['src'] = $this->url->asset($url, $secure); - return 'attributes($attributes) . '>' . PHP_EOL; + return $this->toHtmlString( + 'attributes($attributes) . '>' . PHP_EOL + ); } /** @@ -94,7 +90,7 @@ public function script($url, $attributes = [], $secure = null) * @param array $attributes * @param bool $secure * - * @return string + * @return \Illuminate\Support\HtmlString */ public function style($url, $attributes = [], $secure = null) { @@ -105,7 +101,9 @@ public function style($url, $attributes = [], $secure = null) ]; $attributes['href'] = $this->url->asset($url, $secure); - return 'attributes($attributes) . '>' . PHP_EOL; + return $this->toHtmlString( + 'attributes($attributes) . '>' . PHP_EOL + ); } /** @@ -116,13 +114,15 @@ public function style($url, $attributes = [], $secure = null) * @param array $attributes * @param bool $secure * - * @return string + * @return \Illuminate\Support\HtmlString */ public function image($url, $alt = null, $attributes = [], $secure = null) { $attributes['alt'] = $alt; - return 'attributes($attributes) . '>'; + return $this->toHtmlString( + 'attributes($attributes) . '>' + ); } /** @@ -132,18 +132,19 @@ public function image($url, $alt = null, $attributes = [], $secure = null) * @param array $attributes * @param bool $secure * - * @return string + * @return \Illuminate\Support\HtmlString */ public function favicon($url, $attributes = [], $secure = null) { $attributes = array_merge($attributes, [ - 'rel' => 'shortcut icon', - 'type' => 'image/x-icon' + 'rel' => 'shortcut icon', + 'type' => 'image/x-icon', + 'href' => $this->url->asset($url, $secure) ]); - $attributes['href'] = $this->url->asset($url, $secure); - - return 'attributes($attributes) . '>' . PHP_EOL; + return $this->toHtmlString( + 'attributes($attributes) . '>' . PHP_EOL + ); } /** @@ -154,7 +155,7 @@ public function favicon($url, $attributes = [], $secure = null) * @param array $attributes * @param bool $secure * - * @return string + * @return \Illuminate\Support\HtmlString */ public function link($url, $title = null, $attributes = [], $secure = null) { @@ -164,7 +165,9 @@ public function link($url, $title = null, $attributes = [], $secure = null) $title = $url; } - return 'attributes($attributes) . '>' . $this->entities($title) . ''; + return $this->toHtmlString( + 'attributes($attributes) . '>' . $this->entities($title) . '' + ); } /** @@ -174,7 +177,7 @@ public function link($url, $title = null, $attributes = [], $secure = null) * @param string $title * @param array $attributes * - * @return string + * @return \Illuminate\Support\HtmlString */ public function secureLink($url, $title = null, $attributes = []) { @@ -189,7 +192,7 @@ public function secureLink($url, $title = null, $attributes = []) * @param array $attributes * @param bool $secure * - * @return string + * @return \Illuminate\Support\HtmlString */ public function linkAsset($url, $title = null, $attributes = [], $secure = null) { @@ -205,7 +208,7 @@ public function linkAsset($url, $title = null, $attributes = [], $secure = null) * @param string $title * @param array $attributes * - * @return string + * @return \Illuminate\Support\HtmlString */ public function linkSecureAsset($url, $title = null, $attributes = []) { @@ -220,7 +223,7 @@ public function linkSecureAsset($url, $title = null, $attributes = []) * @param array $parameters * @param array $attributes * - * @return string + * @return \Illuminate\Support\HtmlString */ public function linkRoute($name, $title = null, $parameters = [], $attributes = []) { @@ -239,7 +242,7 @@ public function linkRoute($name, $title = null, $parameters = [], $attributes = * @param array $parameters * @param array $attributes * - * @return string + * @return \Illuminate\Support\HtmlString */ public function linkAction($action, $title = null, $parameters = [], $attributes = []) { @@ -257,7 +260,7 @@ public function linkAction($action, $title = null, $parameters = [], $attributes * @param string $title * @param array $attributes * - * @return string + * @return \Illuminate\Support\HtmlString */ public function mailto($email, $title = null, $attributes = []) { @@ -265,7 +268,9 @@ public function mailto($email, $title = null, $attributes = []) $title = $title ?: $email; $email = $this->obfuscate('mailto:') . $email; - return 'attributes($attributes) . '>' . $this->entities($title) . ''; + return $this->toHtmlString( + 'attributes($attributes) . '>' . $this->entities($title) . '' + ); } /** @@ -286,11 +291,13 @@ public function email($email) * @param array $list * @param array $attributes * - * @return string + * @return \Illuminate\Support\HtmlString */ public function ol(array $list, array $attributes = []) { - return Helpers\Lister::make('ol', $list, $attributes); + return $this->toHtmlString( + Helpers\Lister::make('ol', $list, $attributes) + ); } /** @@ -299,11 +306,13 @@ public function ol(array $list, array $attributes = []) * @param array $list * @param array $attributes * - * @return string + * @return \Illuminate\Support\HtmlString */ public function ul(array $list, array $attributes = []) { - return Helpers\Lister::make('ul', $list, $attributes); + return $this->toHtmlString( + Helpers\Lister::make('ul', $list, $attributes) + ); } /** @@ -312,7 +321,7 @@ public function ul(array $list, array $attributes = []) * @param array $list * @param array $attributes * - * @return string + * @return \Illuminate\Support\HtmlString */ public function dl(array $list, array $attributes = []) { @@ -331,7 +340,7 @@ public function dl(array $list, array $attributes = []) $html .= ''; - return $html; + return $this->toHtmlString($html); } /** @@ -365,10 +374,12 @@ public function obfuscate($value) * @param string $content * @param array $attributes * - * @return string + * @return \Illuminate\Support\HtmlString */ public function meta($name, $content, array $attributes = []) { - return Helpers\Meta::make($name, $content, $attributes); + return $this->toHtmlString( + Helpers\Meta::make($name, $content, $attributes) + ); } } diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 6129bb2..6b69f40 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -917,7 +917,7 @@ public function it_can_make_select_year_inputs() '' // To be continued... ]), - $this->form->selectYear('year', 2000, 2020) + $this->form->selectYear('year', 2000, 2020)->toHtml() ); $this->assertStringStartsWith( @@ -927,7 +927,7 @@ public function it_can_make_select_year_inputs() '' // To be continued... ]), - $this->form->selectYear('year', 2000, 2020, null, ['id' => 'foo']) + $this->form->selectYear('year', 2000, 2020, null, ['id' => 'foo'])->toHtml() ); $this->assertStringStartsWith( @@ -937,14 +937,14 @@ public function it_can_make_select_year_inputs() '' // To be continued... ]), - $this->form->selectYear('year', 2000, 2020, '2000') + $this->form->selectYear('year', 2000, 2020, '2000')->toHtml() ); } /** @test */ public function it_can_make_select_range_inputs() { - $range = $this->form->selectRange('dob', 1900, 2013, 2000); + $range = $this->form->selectRange('dob', 1900, 2013, 2000)->toHtml(); $this->assertStringStartsWith('