mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-28 19:17:37 +00:00
Move functions for HtmlExtension
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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']));
|
||||
}
|
||||
}
|
||||
@@ -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/"
|
||||
|
||||
Reference in New Issue
Block a user