function Htmx::ensureKebabCase

Utility method to transform camelCase strings to kebab-case strings.

Passes kebab-case strings through without any transformation.

Parameters

string $identifier: The string to verify or transform.

Return value

string The original or transformed string.

1 call to Htmx::ensureKebabCase()
Htmx::on in core/lib/Drupal/Core/Htmx/Htmx.php
Creates a `data-hx-on` attribute.

File

core/lib/Drupal/Core/Htmx/Htmx.php, line 119

Class

Htmx
Presents the HTMX controls for developers to use with render arrays.

Namespace

Drupal\Core\Htmx

Code

protected function ensureKebabCase(string $identifier) : string {
  // Check for existing kebab case.
  $kebabParts = explode('-', $identifier);
  // If the number of lower case parts matches the number of parts, then
  // all the parts are lower case.
  $isKebab = count($kebabParts) === count(array_filter($kebabParts, function ($part) {
    return ctype_lower($part);
  }));
  if ($isKebab) {
    return $identifier;
  }
  return (string) u($identifier)->snake()
    ->replaceMatches('#[_:]#', '-');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.