From 41438fb43655845a0fb36cef12e2eb075cbfafb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6rner?= Date: Tue, 7 Jan 2025 14:22:44 +0100 Subject: [PATCH 1/4] backport GenerateDataAttributesStringOptions --- src/Util/Html/HtmlUtil.php | 33 +++++++++--- ...erateDataAttributesStringArrayHandling.php | 9 ++++ .../GenerateDataAttributesStringOptions.php | 51 +++++++++++++++++++ 3 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 src/Util/HtmlUtil/GenerateDataAttributesStringArrayHandling.php create mode 100644 src/Util/HtmlUtil/GenerateDataAttributesStringOptions.php diff --git a/src/Util/Html/HtmlUtil.php b/src/Util/Html/HtmlUtil.php index 7b5942dd..cde00fa9 100644 --- a/src/Util/Html/HtmlUtil.php +++ b/src/Util/Html/HtmlUtil.php @@ -8,6 +8,7 @@ namespace HeimrichHannot\UtilsBundle\Util\Html; +use HeimrichHannot\UtilsBundle\Util\HtmlUtil\GenerateDataAttributesStringOptions; use function Symfony\Component\String\u; class HtmlUtil @@ -40,16 +41,36 @@ public function generateAttributeString(array $attributes, array $options = []): /** * Generates a data-attributes string out of an array. * + * @param array{ + * xhtml?: bool, + * normalizeKeys?: bool, + * array_handling?: 'reduce'|'encode', + * }|GenerateDataAttributesStringOptions $attributes (GenerateDataAttributesStringOptions is only supported from php 8.1) + * * Options (additional to Options from HtmlUtl::generateAttributeString()): * - normalizeKeys: Array keys are normalized to lowercase dash-cased strings (e.g. Foo Bar_player is transformed to foo-bar-player) */ - public function generateDataAttributesString(array $attributes, array $options = []): string + public function generateDataAttributesString(array $attributes, $options = []): string { - $options = array_merge([ - 'xhtml' => false, - 'normalizeKeys' => true, - 'array_handling' => 'reduce', - ], $options); + if ($options instanceof GenerateDataAttributesStringOptions) { + if (version_compare(phpversion(), '8.1', '<')) { + throw new \InvalidArgumentException('Argument $options must be an array for php versions < 8.1'); + } + + $options = [ + 'xhtml' => $options->isXhtml(), + 'normalizeKeys' => $options->isNormalizeKeys(), + 'array_handling' => $options->getArrayHandling()->value, + ]; + } elseif (is_array($options)) { + $options = array_merge([ + 'xhtml' => false, + 'normalizeKeys' => true, + 'array_handling' => 'reduce', + ], $options); + } else { + throw new \InvalidArgumentException('Argument $options must be an array or an instance of GenerateDataAttributesStringOptions'); + } if (!\in_array($options['array_handling'], ['reduce', 'encode'])) { $options['array_handling'] = 'reduce'; diff --git a/src/Util/HtmlUtil/GenerateDataAttributesStringArrayHandling.php b/src/Util/HtmlUtil/GenerateDataAttributesStringArrayHandling.php new file mode 100644 index 00000000..f761e8b2 --- /dev/null +++ b/src/Util/HtmlUtil/GenerateDataAttributesStringArrayHandling.php @@ -0,0 +1,9 @@ +xhtml; + } + + public function setXhtml(bool $xhtml): GenerateDataAttributesStringOptions + { + $this->xhtml = $xhtml; + return $this; + } + + public function isNormalizeKeys(): bool + { + return $this->normalizeKeys; + } + + /** + * Array keys are normalized to lowercase dash-cased strings (e.g. Foo Bar_player is transformed to foo-bar-player) + */ + public function setNormalizeKeys(bool $normalizeKeys): GenerateDataAttributesStringOptions + { + $this->normalizeKeys = $normalizeKeys; + return $this; + } + + public function getArrayHandling(): GenerateDataAttributesStringArrayHandling + { + return $this->arrayHandling; + } + + public function setArrayHandling(GenerateDataAttributesStringArrayHandling $arrayHandling): GenerateDataAttributesStringOptions + { + $this->arrayHandling = $arrayHandling; + return $this; + } +} \ No newline at end of file From b8c8ef6b98fbfbbfd46f73346593b2bcdf6243eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6rner?= Date: Tue, 7 Jan 2025 14:29:54 +0100 Subject: [PATCH 2/4] update workflows --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 022c6c9b..890514ba 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,7 +38,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 7.4 + php-version: 8.1 extensions: dom, fileinfo, filter, gd, hash, intl, json, mbstring, pcre, pdo, zlib coverage: xdebug tools: php-cs-fixer, phpunit @@ -65,7 +65,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 8.0 + php-version: 8.1 - name: Checkout uses: actions/checkout@v2 From 968840faa2263d6322a88c95f544aed19d4897c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6rner?= Date: Tue, 7 Jan 2025 14:35:47 +0100 Subject: [PATCH 3/4] fixed phpstan issue --- src/Util/Html/HtmlUtil.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Util/Html/HtmlUtil.php b/src/Util/Html/HtmlUtil.php index cde00fa9..4f896d12 100644 --- a/src/Util/Html/HtmlUtil.php +++ b/src/Util/Html/HtmlUtil.php @@ -45,7 +45,7 @@ public function generateAttributeString(array $attributes, array $options = []): * xhtml?: bool, * normalizeKeys?: bool, * array_handling?: 'reduce'|'encode', - * }|GenerateDataAttributesStringOptions $attributes (GenerateDataAttributesStringOptions is only supported from php 8.1) + * }|GenerateDataAttributesStringOptions $options (GenerateDataAttributesStringOptions is only supported from php 8.1) * * Options (additional to Options from HtmlUtl::generateAttributeString()): * - normalizeKeys: Array keys are normalized to lowercase dash-cased strings (e.g. Foo Bar_player is transformed to foo-bar-player) From bffac33c474f607cc16425fa9e1269a61b19dff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6rner?= Date: Tue, 7 Jan 2025 14:42:16 +0100 Subject: [PATCH 4/4] prepared 2.240.0 --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53c0745d..fd42b0a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to this project will be documented in this file. +## [2.240.0] - 2025-01-07 +- Changed: enhance entity finder with universal entity support fallback ([#87](https://github.com/heimrichhannot/contao-utils-bundle/pull/87)) +- Changed: backport GenerateDataAttributesStringOptions as options attributes for HtmlUtil::generateDataAttributesString() from v3 ([#91](https://github.com/heimrichhannot/contao-utils-bundle/pull/91)) +- Deprecated: `ExtendEntityFinderEvent` + ## [2.239.2] - 2024-12-05 - Fixed: do build tree cache on cache warmup