Move functions for HtmlExtension

This commit is contained in:
Fabien Potencier
2023-12-09 18:04:05 +01:00
parent 16abb69d12
commit 6a18cda5aa
4 changed files with 79 additions and 26 deletions
+28 -26
View File
@@ -9,8 +9,10 @@
* file that was distributed with this source code.
*/
namespace Twig\Extra\Html {
namespace Twig\Extra\Html;
use Symfony\Component\Mime\MimeTypes;
use Twig\Error\RuntimeError;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
@@ -34,7 +36,7 @@ final class HtmlExtension extends AbstractExtension
public function getFunctions(): array
{
return [
new TwigFunction('html_classes', 'twig_html_classes'),
new TwigFunction('html_classes', [self::class, 'htmlClasses']),
];
}
@@ -45,6 +47,8 @@ final class HtmlExtension extends AbstractExtension
* be done before calling this filter.
*
* @return string The generated data URI
*
* @internal
*/
public function dataUri(string $data, string $mime = null, array $parameters = []): string
{
@@ -79,33 +83,31 @@ final class HtmlExtension extends AbstractExtension
return $repr;
}
}
}
namespace {
use Twig\Error\RuntimeError;
function twig_html_classes(...$args): string
{
$classes = [];
foreach ($args as $i => $arg) {
if (\is_string($arg)) {
$classes[] = $arg;
} elseif (\is_array($arg)) {
foreach ($arg as $class => $condition) {
if (!\is_string($class)) {
throw new RuntimeError(sprintf('The html_classes function argument %d (key %d) should be a string, got "%s".', $i, $class, \gettype($class)));
/**
* @internal
*/
public static function htmlClasses(...$args): string
{
$classes = [];
foreach ($args as $i => $arg) {
if (\is_string($arg)) {
$classes[] = $arg;
} elseif (\is_array($arg)) {
foreach ($arg as $class => $condition) {
if (!\is_string($class)) {
throw new RuntimeError(sprintf('The html_classes function argument %d (key %d) should be a string, got "%s".', $i, $class, \gettype($class)));
}
if (!$condition) {
continue;
}
$classes[] = $class;
}
if (!$condition) {
continue;
}
$classes[] = $class;
} else {
throw new RuntimeError(sprintf('The html_classes function argument %d should be either a string or an array, got "%s".', $i, \gettype($arg)));
}
} else {
throw new RuntimeError(sprintf('The html_classes function argument %d should be either a string or an array, got "%s".', $i, \gettype($arg)));
}
}
return implode(' ', array_unique($classes));
}
return implode(' ', array_unique($classes));
}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Twig\Extra\Html\HtmlExtension;
/**
* @internal
* @deprecated since Twig 3.9.0
*/
function twig_html_classes(...$args): string
{
trigger_deprecation('twig/html-extra', '3.9.0', 'Using the internal "%s" function is deprecated.', __FUNCTION__);
return HtmlExtension::htmlClasses(...$args);
}
@@ -0,0 +1,26 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extra\Html\Tests;
use PHPUnit\Framework\TestCase;
use Twig\Extra\Html\HtmlExtension;
/**
* @group legacy
*/
class LegacyFunctionsTest extends TestCase
{
public function testHtmlToMarkdown()
{
$this->assertSame(HtmlExtension::htmlClasses(['charset' => 'utf-8']), \twig_html_classes(['charset' => 'utf-8']));
}
}
+2
View File
@@ -16,6 +16,7 @@
],
"require": {
"php": ">=7.2.5",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/mime": "^5.4|^6.0|^7.0",
"twig/twig": "^3.0"
},
@@ -23,6 +24,7 @@
"symfony/phpunit-bridge": "^6.4|^7.0"
},
"autoload": {
"files": [ "Resources/functions.php" ],
"psr-4" : { "Twig\\Extra\\Html\\" : "" },
"exclude-from-classmap": [
"/Tests/"