'class6']],
- 7
+@php
+ $id = 17;
+ $fullScreen = false;
+ $darkMode = true;
+@endphp
+
```
-In your code, you can do exactly the same using the `(new Okipa\LaravelHtmlHelper\HtmlClassTag)->render()` method.
+You can call `app(Okipa\LaravelHtmlHelper\HtmlClasses)->toHtml($classes)` if you want to avoid helper use.
-### `htmlAttributes(...$attributesList) : HtmlString`
+### html_attributes
-In you view :
+Calling this helper generates dynamic HTML attributes, taking care about the given key-only, value-only or key-value combinations.
+
+It accepts combination of strings, arrays or null arguments.
```blade
-
'attribute2Value'],
- ['attribute3Key' => null],
- ['attribute4Value', 'attribute5Value'],
- '',
- null,
- ['' => 'attribute6Value'],
- ['attributes7Value', ['attribute8Value', 'attribute9Key' => 'attribute9Value']],
- ['attribute10Key' => ['attribute11Value']],
- ['attribute12Key' => '']
+@php
+ $dragAndDrop = true;
+ $disabled = false;
+@endphp
+
__('Are you sure you want to delete this line?')],
+ $dragAndDrop ? 'data-drag-drop' : null,
+ $disabled ? ['disabled', 'data-forbid-click'] : null,
+ 'required'
) }}>
```
-Will produce :
-
```html
-
+
```
-In your code, you can do exactly the same using the `(new Okipa\LaravelHtmlHelper\HtmlAttributes)->render()` method.
+You can call `app(Okipa\LaravelHtmlHelper\HtmlAttributes)->toHtml($attributes)` if you want to avoid helper use.
## Testing
diff --git a/composer.json b/composer.json
index 464e805..0716dca 100644
--- a/composer.json
+++ b/composer.json
@@ -23,7 +23,7 @@
}
],
"require": {
- "php": "^7.4",
+ "php": "^7.4||^8.0",
"illuminate/support": "^7.0||^8.0"
},
"require-dev": {
@@ -50,8 +50,8 @@
"vendor/bin/phpcbf",
"vendor/bin/phpcs",
"vendor/bin/phpmd src text phpmd.xml",
- "vendor/bin/phpstan analyse",
- "vendor/bin/phpunit"
+ "vendor/bin/phpstan analyse --memory-limit=-1 --error-format=table",
+ "vendor/bin/phpunit -d memory_limit=-1 --testdox --coverage-text"
]
},
"config": {
diff --git a/docs/laravel-html-helper.png b/docs/laravel-html-helper.png
new file mode 100644
index 0000000..9ac66c2
Binary files /dev/null and b/docs/laravel-html-helper.png differ
diff --git a/docs/upgrade-guides/from-v1-to-v2.md b/docs/upgrade-guides/from-v1-to-v2.md
new file mode 100644
index 0000000..4986b67
--- /dev/null
+++ b/docs/upgrade-guides/from-v1-to-v2.md
@@ -0,0 +1,18 @@
+# Upgrade from v1 to V2
+
+Follow the steps below to upgrade the package.
+
+## Naming changes
+
+Helpers have been renamed. Make sure you replaced all following occurrences when upgrading:
+* Search and replace all occurrences of `classTag(` by `html_classes(`
+* Search and replace all occurrences of `HtmlClassTag(` by `HtmlClasses(`
+* Search and replace all occurrences of `htmlAttributes(` by `html_attributes(`
+
+## See all changes
+
+See all change with the [comparison tool](https://github.com/Okipa/laravel-html-helper/compare/1.4.0...2.0.0).
+
+## Undocumented changes
+
+If you see any forgotten and undocumented change, please submit a PR to add them to this upgrade guide.
diff --git a/phpcs.xml b/phpcs.xml
index 6cbe9a9..44df66c 100644
--- a/phpcs.xml
+++ b/phpcs.xml
@@ -1,10 +1,9 @@
-
- PSR-2 validation
+
+ PSR-12 validation
./src
-
./tests/*
-
+
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 7b4ca04..af1d6b0 100755
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -1,5 +1,6 @@
-
+ stopOnFailure="false"
+ xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
+
+
+ src/
+
+
+
+
+
tests
-
-
- src/
-
-
-
-
-
+
diff --git a/src/HtmlAttributes.php b/src/HtmlAttributes.php
index 9755938..cb372f5 100644
--- a/src/HtmlAttributes.php
+++ b/src/HtmlAttributes.php
@@ -2,110 +2,83 @@
namespace Okipa\LaravelHtmlHelper;
-use Exception;
use Illuminate\Support\HtmlString;
+use RuntimeException;
-class HtmlAttributes extends HtmlHelper
+class HtmlAttributes
{
/**
- * Render the generated html.
- *
- * @param mixed ...$attributesList
+ * @param string|array|null ...$attributes
*
* @return \Illuminate\Support\HtmlString
- * @throws \Exception
*/
- public function render(...$attributesList): HtmlString
+ public function toHtml(...$attributes): HtmlString
{
- return $this->generateHtmlString(...$attributesList);
+ return $this->generateHtmlString(...$attributes);
}
/**
- * Render html attributes from the given attributes list.
- *
- * @param mixed ...$attributesList
+ * @param string|array|null ...$attributes
*
* @return \Illuminate\Support\HtmlString
- * @throws \Exception
*/
- protected function generateHtmlString(...$attributesList): HtmlString
+ protected function generateHtmlString(...$attributes): HtmlString
{
- $attributesArray = $this->buildAttributesArray(...$attributesList);
- $html = $this->buildHtmlString($attributesArray);
+ $builtAttributes = $this->buildAttributes(...$attributes);
+ $html = $this->buildHtmlString($builtAttributes);
return new HtmlString($html);
}
- /**
- * Build the attributes array
- *
- * @return array
- * @throws \Exception
- */
- protected function buildAttributesArray(): array
+ protected function buildAttributes(): array
{
- $attributesArray = [];
+ $attributes = [];
foreach (func_get_args() as $arg) {
switch (gettype($arg)) {
case 'string':
- $attributesArray[] = $arg;
+ $attributes[] = $arg;
break;
case 'array':
- $this->analyseArrayAttributes($arg, $attributesArray);
+ $this->buildAttributeFromArray($arg, $attributes);
break;
case 'NULL':
break;
default:
- throw new Exception('The given attributes arguments should be strings or arrays : '
+ throw new RuntimeException('The given attributes arguments should be strings or arrays: '
. gettype($arg) . ' type given.');
}
}
- return array_map('trim', array_filter($attributesArray));
+ return array_map('trim', array_filter($attributes));
}
- /**
- * @param array $array
- * @param array $attributes
- */
- private function analyseArrayAttributes(array $array, array &$attributes): void
+ protected function buildAttributeFromArray(array $attribute, array &$attributes): void
{
- foreach ($array as $key => $value) {
+ foreach ($attribute as $key => $value) {
if (is_array($value)) {
- if (! empty($key) && is_string($key)) {
+ if (is_string($key)) {
$attributes[] = $key;
}
- $this->analyseArrayAttributes($value, $attributes);
- } else {
- if (! empty($key) && is_string($key)) {
- if ($value) {
- $attributes[$key] = $value;
- } else {
- $attributes[] = $key;
- }
- } else {
- if ($value) {
- $attributes[] = $value;
- } else {
- $attributes[] = $key;
- }
- }
+ $this->buildAttributeFromArray($value, $attributes);
+ continue;
+ }
+ if (is_string($key) && $value) {
+ $attributes[$key] = $value;
+ continue;
}
+ if ($value) {
+ $attributes[] = $value;
+ continue;
+ }
+ $attributes[] = $key;
}
}
- /**
- * Build the html string from the attributes array.
- *
- * @param array $attributesArray
- *
- * @return string
- */
- protected function buildHtmlString(array $attributesArray): string
+ protected function buildHtmlString(array $attributes): string
{
$html = '';
- foreach ($attributesArray as $key => $attribute) {
- $spacer = strlen($html) ? ' ' : '';
+ foreach ($attributes as $key => $attribute) {
+ $spacer = $html ? ' ' : '';
if ($key && is_string($key)) {
$html .= $spacer . $key . ($attribute ? '="' . $attribute . '"' : '');
} else {
@@ -113,6 +86,6 @@ protected function buildHtmlString(array $attributesArray): string
}
}
- return strlen($html) ? ' ' . $html : '';
+ return ($html ? ' ' : '') . $html;
}
}
diff --git a/src/HtmlClassTag.php b/src/HtmlClassTag.php
deleted file mode 100644
index e045390..0000000
--- a/src/HtmlClassTag.php
+++ /dev/null
@@ -1,53 +0,0 @@
-generateHtmlClassTag(...$classList);
- }
-
- /**
- * Render a html class tag filled with the given class list.
- *
- * @return \Illuminate\Support\HtmlString
- * @throws \Exception
- */
- protected function generateHtmlClassTag(): HtmlString
- {
- $classArray = [];
- foreach (func_get_args() as $arg) {
- switch (gettype($arg)) {
- case 'string':
- case 'integer':
- $classArray[] = $arg;
- break;
- case 'array':
- $classArray = ! empty($arg) ? array_merge($classArray, $arg) : $classArray;
- break;
- case 'NULL':
- break;
- default:
- throw new Exception('The given class arguments should be strings, integers or arrays : '
- . gettype($arg) . ' type given.');
- }
- }
- $classArray = array_map('trim', array_filter(Arr::flatten($classArray)));
-
- return new HtmlString(! empty($classArray) ? ' class="' . implode(' ', $classArray) . '"' : '');
- }
-}
diff --git a/src/HtmlClasses.php b/src/HtmlClasses.php
new file mode 100644
index 0000000..6dbe6ed
--- /dev/null
+++ b/src/HtmlClasses.php
@@ -0,0 +1,44 @@
+generateHtmlClasses(...$classes);
+ }
+
+ protected function generateHtmlClasses(): HtmlString
+ {
+ $classes = [];
+ foreach (func_get_args() as $arg) {
+ switch (gettype($arg)) {
+ case 'string':
+ case 'integer':
+ $classes[] = $arg;
+ break;
+ case 'array':
+ $classes = $arg ? array_merge($classes, $arg) : $classes;
+ break;
+ case 'NULL':
+ break;
+ default:
+ throw new RuntimeException('Classes should be strings, integers, arrays or null: '
+ . gettype($arg) . ' given.');
+ }
+ }
+ $classes = array_map('trim', array_filter(Arr::flatten($classes)));
+
+ return new HtmlString($classes ? ' class="' . implode(' ', $classes) . '"' : '');
+ }
+}
diff --git a/src/HtmlHelper.php b/src/HtmlHelper.php
deleted file mode 100644
index 023f3d2..0000000
--- a/src/HtmlHelper.php
+++ /dev/null
@@ -1,28 +0,0 @@
-render();
- }
-
- /**
- * Render the generated html.
- *
- * @param mixed ...$args
- *
- * @return \Illuminate\Support\HtmlString
- */
- abstract public function render(...$args): HtmlString;
-}
diff --git a/src/HtmlHelperServiceProvider.php b/src/HtmlHelperServiceProvider.php
deleted file mode 100644
index 6aba20d..0000000
--- a/src/HtmlHelperServiceProvider.php
+++ /dev/null
@@ -1,16 +0,0 @@
-app->singleton('Okipa\LaravelHtmlHelper', function (Application $app) {
- return $app->make(HtmlHelper::class);
- });
- }
-}
diff --git a/src/helpers.php b/src/helpers.php
index 350768d..56303ed 100644
--- a/src/helpers.php
+++ b/src/helpers.php
@@ -2,30 +2,28 @@
use Illuminate\Support\HtmlString;
use Okipa\LaravelHtmlHelper\HtmlAttributes;
-use Okipa\LaravelHtmlHelper\HtmlClassTag;
+use Okipa\LaravelHtmlHelper\HtmlClasses;
-if (! function_exists('classTag')) {
+if (! function_exists('html_classes')) {
/**
- * @param mixed ...$classList
+ * @param string|int|array|null ...$classList
*
* @return \Illuminate\Support\HtmlString
- * @throws \Exception
*/
- function classTag(...$classList): HtmlString
+ function html_classes(...$classList): HtmlString
{
- return (new HtmlClassTag)->render(...$classList);
+ return app(HtmlClasses::class)->toHtml(...$classList);
}
}
-if (! function_exists('htmlAttributes')) {
+if (! function_exists('html_attributes')) {
/**
- * @param mixed ...$attributesList
+ * @param string|array|null ...$attributesList
*
* @return \Illuminate\Support\HtmlString
- * @throws \Exception
*/
- function htmlAttributes(...$attributesList): HtmlString
+ function html_attributes(...$attributesList): HtmlString
{
- return (new HtmlAttributes)->render(...$attributesList);
+ return app(HtmlAttributes::class)->toHtml(...$attributesList);
}
}
diff --git a/tests/HtmlHelperTestCase.php b/tests/HtmlHelperTestCase.php
index 7d4deda..a9974f8 100644
--- a/tests/HtmlHelperTestCase.php
+++ b/tests/HtmlHelperTestCase.php
@@ -2,20 +2,9 @@
namespace Okipa\LaravelHtmlHelper\Test;
-use Okipa\LaravelHtmlHelper\HtmlHelperServiceProvider;
use Orchestra\Testbench\TestCase;
abstract class HtmlHelperTestCase extends TestCase
{
- /**
- * Get package providers.
- *
- * @param \Illuminate\Foundation\Application $app
- *
- * @return array
- */
- protected function getPackageProviders($app)
- {
- return [HtmlHelperServiceProvider::class];
- }
+ //
}
diff --git a/tests/Unit/ClassTagTest.php b/tests/Unit/ClassTagTest.php
deleted file mode 100644
index 989d125..0000000
--- a/tests/Unit/ClassTagTest.php
+++ /dev/null
@@ -1,67 +0,0 @@
- 'class6']],
- [],
- 7
- );
- $this->assertEquals(' class="class1 class2 class3 class4 class5 class6 7"', $html);
- }
-
- public function testEmptyClassTagHtml()
- {
- $this->assertEquals('', classTag([]));
- }
-
- public function testSuccessRenderClassTagHtmlFromClass()
- {
- $html = app(\Okipa\LaravelHtmlHelper\HtmlClassTag::class)->render(
- 'class1',
- ['class2', 'class3', null],
- null,
- [],
- ['class4', ['class5 ', 'class6Key' => 'class6']],
- 7
- );
- $this->assertEquals(' class="class1 class2 class3 class4 class5 class6 7"', $html);
- }
-
- public function testFailRenderClassTagHtmlWithObjectGiven()
- {
- $this->expectException(Exception::class);
- classTag(new stdClass());
- }
-
- public function testFailRenderClassTagHtmlWithDoubleGiven()
- {
- $this->expectException(Exception::class);
- classTag(12.7);
- }
-
- public function testFailRenderClassTagHtmlWithBooleanGiven()
- {
- $this->expectException(Exception::class);
- classTag(true);
- }
-
- public function testRenderedHtml()
- {
- view()->addNamespace('htmlHelper', 'tests/views');
- $html = view('htmlHelper::classTag')->render();
- $this->assertStringContainsString('', $html);
- }
-}
diff --git a/tests/Unit/HtmlAttributesTest.php b/tests/Unit/HtmlAttributesTest.php
index 1cb8644..807be27 100644
--- a/tests/Unit/HtmlAttributesTest.php
+++ b/tests/Unit/HtmlAttributesTest.php
@@ -9,9 +9,10 @@
class HtmlAttributesTest extends HtmlHelperTestCase
{
- public function testSuccessRenderAttributesHtmlFromHelper()
+ /** @test */
+ public function it_generates_html_attributes_from_helper_call(): void
{
- $html = htmlAttributes(
+ $html = html_attributes(
'attribute1Value',
['attribute2Key' => 'attribute2Value'],
['attribute3Key' => null],
@@ -23,7 +24,7 @@ public function testSuccessRenderAttributesHtmlFromHelper()
['attribute10Key' => ['attribute11Value']],
['attribute12Key' => '']
);
- $this->assertEquals(
+ self::assertEquals(
' attribute1Value attribute2Key="attribute2Value" attribute3Key attribute4Value '
. 'attribute5Value attribute6Value attributes7Value attribute8Value attribute9Key="attribute9Value" '
. 'attribute10Key attribute11Value attribute12Key',
@@ -31,9 +32,10 @@ public function testSuccessRenderAttributesHtmlFromHelper()
);
}
- public function testSuccessRenderAttributesHtmlFromClass()
+ /** @test */
+ public function it_generates_html_attributes_from_class_call(): void
{
- $html = app(HtmlAttributes::class)->render(
+ $html = app(HtmlAttributes::class)->toHtml(
'attribute1Value',
['attribute2Key' => 'attribute2Value'],
['attribute3Key' => null],
@@ -45,7 +47,7 @@ public function testSuccessRenderAttributesHtmlFromClass()
['attribute10Key' => ['attribute11Value']],
['attribute12Key' => '']
);
- $this->assertEquals(
+ self::assertEquals(
' attribute1Value attribute2Key="attribute2Value" attribute3Key attribute4Value attribute5Value '
. 'attribute6Value attributes7Value attribute8Value attribute9Key="attribute9Value" attribute10Key '
. 'attribute11Value attribute12Key',
@@ -53,35 +55,40 @@ public function testSuccessRenderAttributesHtmlFromClass()
);
}
- public function testFailRenderAttributesHtmlWithIntGiven()
+ /** @test */
+ public function it_triggers_exception_with_object(): void
{
$this->expectException(Exception::class);
- classTag(htmlAttributes(10));
+ html_attributes(new stdClass());
}
- public function testFailRenderAttributesHtmlWithObjectGiven()
+ /** @test */
+ public function it_triggers_exception_with_double(): void
{
$this->expectException(Exception::class);
- htmlAttributes(new stdClass());
+ html_attributes(12.7);
}
- public function testFailRenderAttributesHtmlWithDoubleGiven()
+ /** @test */
+ public function it_triggers_exception_with_bool(): void
{
$this->expectException(Exception::class);
- htmlAttributes(12.7);
+ html_attributes(true);
}
- public function testFailRenderAttributesHtmlWithBooleanGiven()
+ /** @test */
+ public function it_triggers_exception_with_int(): void
{
$this->expectException(Exception::class);
- htmlAttributes(true);
+ html_classes(html_attributes(10));
}
- public function testRenderedHtml()
+ /** @test */
+ public function it_renders_html_attributes_in_view(): void
{
- view()->addNamespace('htmlHelper', 'tests/views');
- $html = view('htmlHelper::htmlAttributes')->render();
- $this->assertStringContainsString(
+ view()->addNamespace('html_helper', 'tests/views');
+ $html = view('html_helper::html-attributes')->toHtml();
+ self::assertStringContainsString(
'',
@@ -89,10 +96,11 @@ public function testRenderedHtml()
);
}
- public function testHtmlNoAttributesRenderedHtml()
+ /** @test */
+ public function it_renders_no_html_attributes_in_view(): void
{
- view()->addNamespace('htmlHelper', 'tests/views');
- $html = view('htmlHelper::noHtmlAttributes')->render();
- $this->assertStringContainsString('', $html);
+ view()->addNamespace('html_helper', 'tests/views');
+ $html = view('html_helper::no-html-attributes')->toHtml();
+ self::assertStringContainsString('', $html);
}
}
diff --git a/tests/Unit/HtmlClassesTest.php b/tests/Unit/HtmlClassesTest.php
new file mode 100644
index 0000000..3de6dab
--- /dev/null
+++ b/tests/Unit/HtmlClassesTest.php
@@ -0,0 +1,77 @@
+ 'class6']],
+ [],
+ 7
+ );
+ self::assertEquals(' class="class1 class2 class3 class4 class5 class6 7"', $html);
+ }
+
+ /** @test */
+ public function it_generates_html_classes_from_class_call(): void
+ {
+ $html = app(HtmlClasses::class)->toHtml(
+ 'class1',
+ ['class2', 'class3', null],
+ null,
+ [],
+ ['class4', ['class5 ', 'class6Key' => 'class6']],
+ 7
+ );
+ self::assertEquals(' class="class1 class2 class3 class4 class5 class6 7"', $html);
+ }
+
+ /** @test */
+ public function it_returns_empty_string_with_empty_inputs(): void
+ {
+ self::assertEquals('', html_classes([]));
+ self::assertEquals('', html_classes(null));
+ self::assertEquals('', html_classes(''));
+ }
+
+ /** @test */
+ public function it_triggers_exception_with_object(): void
+ {
+ $this->expectException(Exception::class);
+ html_classes(new stdClass());
+ }
+
+ /** @test */
+ public function it_triggers_exception_with_double(): void
+ {
+ $this->expectException(Exception::class);
+ html_classes(12.7);
+ }
+
+ /** @test */
+ public function it_triggers_exception_with_bool(): void
+ {
+ $this->expectException(Exception::class);
+ html_classes(true);
+ }
+
+ /** @test */
+ public function it_renders_html_classes_in_view(): void
+ {
+ view()->addNamespace('html_helper', 'tests/views');
+ $html = view('html_helper::html-classes')->toHtml();
+ self::assertStringContainsString('', $html);
+ }
+}
diff --git a/tests/views/classTag.blade.php b/tests/views/classTag.blade.php
deleted file mode 100644
index d53e87f..0000000
--- a/tests/views/classTag.blade.php
+++ /dev/null
@@ -1 +0,0 @@
- 'class6']], 7) }}>
diff --git a/tests/views/html-attributes.blade.php b/tests/views/html-attributes.blade.php
new file mode 100644
index 0000000..8097fe1
--- /dev/null
+++ b/tests/views/html-attributes.blade.php
@@ -0,0 +1 @@
+ 'attribute2Value'], ['attribute3Key' => null], ['attribute4Value', 'attribute5Value'], '', null, ['' => 'attribute6Value'], ['attributes7Value', ['attribute8Value', 'attribute9Key' => 'attribute9Value']], ['attribute10Key' => ['attribute11Value']], ['attribute12Key' => '']) }}>
diff --git a/tests/views/html-classes.blade.php b/tests/views/html-classes.blade.php
new file mode 100644
index 0000000..2ca452a
--- /dev/null
+++ b/tests/views/html-classes.blade.php
@@ -0,0 +1 @@
+ 'class6']], 7) }}>
diff --git a/tests/views/htmlAttributes.blade.php b/tests/views/htmlAttributes.blade.php
deleted file mode 100644
index bdf6be4..0000000
--- a/tests/views/htmlAttributes.blade.php
+++ /dev/null
@@ -1 +0,0 @@
- 'attribute2Value'], ['attribute3Key' => null], ['attribute4Value', 'attribute5Value'], '', null, ['' => 'attribute6Value'], ['attributes7Value', ['attribute8Value', 'attribute9Key' => 'attribute9Value']], ['attribute10Key' => ['attribute11Value']], ['attribute12Key' => '']) }}>
diff --git a/tests/views/no-html-attributes.blade.php b/tests/views/no-html-attributes.blade.php
new file mode 100644
index 0000000..a0a5e55
--- /dev/null
+++ b/tests/views/no-html-attributes.blade.php
@@ -0,0 +1 @@
+
diff --git a/tests/views/noHtmlAttributes.blade.php b/tests/views/noHtmlAttributes.blade.php
deleted file mode 100644
index 8680e3f..0000000
--- a/tests/views/noHtmlAttributes.blade.php
+++ /dev/null
@@ -1 +0,0 @@
-