parent
9ffe109cb1
commit
ed3f166997
5 changed files with 194 additions and 0 deletions
@ -0,0 +1,31 @@ |
||||
--TEST-- |
||||
Symfony AssetMapper pattern: uasort with array spread spaceship comparison |
||||
--FILE-- |
||||
<?php |
||||
|
||||
function sort_path_rows(array $rows): array |
||||
{ |
||||
uasort($rows, static fn (array $a, array $b): int => [(bool) $a[1], ...$a] <=> [(bool) $b[1], ...$b]); |
||||
|
||||
return $rows; |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
$rows = [ |
||||
['assets/z.css', ''], |
||||
['assets/app.js', 'App\\'], |
||||
['assets/a.css', ''], |
||||
['vendor/package.js', 'Vendor\\'], |
||||
]; |
||||
|
||||
foreach (sort_path_rows($rows) as $row) { |
||||
echo $row[0], '|', $row[1], "\n"; |
||||
} |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
assets/a.css| |
||||
assets/z.css| |
||||
assets/app.js|App\ |
||||
vendor/package.js|Vendor\ |
||||
@ -0,0 +1,48 @@ |
||||
--TEST-- |
||||
Symfony DomCrawler pattern: parse_str values and recursive replace with unpack |
||||
--FILE-- |
||||
<?php |
||||
|
||||
function expand_form_values(array $fields): array |
||||
{ |
||||
$values = []; |
||||
|
||||
foreach ($fields as $name => $value) { |
||||
$qs = http_build_query([$name => $value], '', '&'); |
||||
if ($qs) { |
||||
parse_str($qs, $expandedValue); |
||||
$varName = substr($name, 0, strlen(key($expandedValue))); |
||||
$values[] = [$varName => current($expandedValue)]; |
||||
} |
||||
} |
||||
|
||||
return array_replace_recursive([], ...$values); |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
var_dump(expand_form_values([ |
||||
'user[name]' => 'Ada', |
||||
'user[roles][0]' => 'admin', |
||||
'user[roles][1]' => 'editor', |
||||
'token' => 'abc', |
||||
])); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
array(2) { |
||||
["user"]=> |
||||
array(2) { |
||||
["name"]=> |
||||
string(3) "Ada" |
||||
["roles"]=> |
||||
array(2) { |
||||
[0]=> |
||||
string(5) "admin" |
||||
[1]=> |
||||
string(6) "editor" |
||||
} |
||||
} |
||||
["token"]=> |
||||
string(3) "abc" |
||||
} |
||||
@ -0,0 +1,54 @@ |
||||
--TEST-- |
||||
Symfony HtmlSanitizer pattern: array visitor lookup with nullsafe render fallback |
||||
--FILE-- |
||||
<?php |
||||
|
||||
final class RenderedNode |
||||
{ |
||||
public function __construct(private string $html) |
||||
{ |
||||
} |
||||
|
||||
public function render(): string |
||||
{ |
||||
return $this->html; |
||||
} |
||||
} |
||||
|
||||
final class Visitor |
||||
{ |
||||
public function __construct(private ?RenderedNode $node) |
||||
{ |
||||
} |
||||
|
||||
public function visit(object $parsed): ?RenderedNode |
||||
{ |
||||
return $this->node; |
||||
} |
||||
} |
||||
|
||||
function render_sanitized(array $domVisitors, string $context, ?object $parsed): string |
||||
{ |
||||
if (null === $parsed) { |
||||
return ''; |
||||
} |
||||
|
||||
return $domVisitors[$context]->visit($parsed)?->render() ?? ''; |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
$visitors = [ |
||||
'body' => new Visitor(new RenderedNode('<p>ok</p>')), |
||||
'head' => new Visitor(null), |
||||
]; |
||||
|
||||
var_dump(render_sanitized($visitors, 'body', new stdClass())); |
||||
var_dump(render_sanitized($visitors, 'head', new stdClass())); |
||||
var_dump(render_sanitized($visitors, 'body', null)); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
string(9) "<p>ok</p>" |
||||
string(0) "" |
||||
string(0) "" |
||||
@ -0,0 +1,30 @@ |
||||
--TEST-- |
||||
Symfony AssetMapper pattern: array_filter with ARRAY_FILTER_USE_KEY |
||||
--FILE-- |
||||
<?php |
||||
|
||||
function strip_numeric_keys(array $parsed): array |
||||
{ |
||||
return array_filter($parsed, static fn ($key) => !is_int($key), ARRAY_FILTER_USE_KEY); |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
var_dump(strip_numeric_keys([ |
||||
0 => '@symfony/stimulus-bundle', |
||||
'package' => '@symfony/stimulus-bundle', |
||||
1 => 'extra', |
||||
'version' => '1.2.3', |
||||
'alias' => 'stimulus', |
||||
])); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
array(3) { |
||||
["package"]=> |
||||
string(24) "@symfony/stimulus-bundle" |
||||
["version"]=> |
||||
string(5) "1.2.3" |
||||
["alias"]=> |
||||
string(8) "stimulus" |
||||
} |
||||
@ -0,0 +1,31 @@ |
||||
--TEST-- |
||||
Symfony Translation pattern: complex conditional expression as match subject |
||||
--FILE-- |
||||
<?php |
||||
|
||||
function plural_locale_bucket(string $locale): int |
||||
{ |
||||
return match ('pt_BR' !== $locale && 'en_US_POSIX' !== $locale && strlen($locale) > 3 ? substr($locale, 0, strrpos($locale, '_')) : $locale) { |
||||
'en', |
||||
'fr' => 1, |
||||
'pt_BR' => 2, |
||||
'en_US_POSIX' => 3, |
||||
default => 0, |
||||
}; |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
var_dump(plural_locale_bucket('en_GB')); |
||||
var_dump(plural_locale_bucket('fr_CA')); |
||||
var_dump(plural_locale_bucket('pt_BR')); |
||||
var_dump(plural_locale_bucket('en_US_POSIX')); |
||||
var_dump(plural_locale_bucket('zh_Hant')); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
int(1) |
||||
int(1) |
||||
int(2) |
||||
int(3) |
||||
int(0) |
||||
Loading…
Reference in new issue