Skip to content

Commit

Permalink
backport GenerateDataAttributesStringOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Jan 7, 2025
1 parent d944b4c commit 41438fb
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/Util/Html/HtmlUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace HeimrichHannot\UtilsBundle\Util\Html;

use HeimrichHannot\UtilsBundle\Util\HtmlUtil\GenerateDataAttributesStringOptions;
use function Symfony\Component\String\u;

class HtmlUtil
Expand Down Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace HeimrichHannot\UtilsBundle\Util\HtmlUtil;

enum GenerateDataAttributesStringArrayHandling: string

Check failure on line 5 in src/Util/HtmlUtil/GenerateDataAttributesStringArrayHandling.php

View workflow job for this annotation

GitHub Actions / phpstan

Syntax error, unexpected T_STRING on line 5
{
case REDUCE = 'reduce';

Check failure on line 7 in src/Util/HtmlUtil/GenerateDataAttributesStringArrayHandling.php

View workflow job for this annotation

GitHub Actions / phpstan

Syntax error, unexpected T_CASE on line 7
case ENCODE = 'encode';
}
51 changes: 51 additions & 0 deletions src/Util/HtmlUtil/GenerateDataAttributesStringOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace HeimrichHannot\UtilsBundle\Util\HtmlUtil;

class GenerateDataAttributesStringOptions
{
private bool $xhtml = false;
private bool $normalizeKeys = true;
private GenerateDataAttributesStringArrayHandling $arrayHandling = GenerateDataAttributesStringArrayHandling::REDUCE;

public static function create(): self
{
return new self();
}

public function isXhtml(): bool
{
return $this->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;
}
}

0 comments on commit 41438fb

Please sign in to comment.