-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapitalizeWords.php
More file actions
40 lines (34 loc) · 955 Bytes
/
CapitalizeWords.php
File metadata and controls
40 lines (34 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
declare(strict_types=1);
namespace Kiboko\Component\StringExpressionLanguage;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
final class CapitalizeWords extends ExpressionFunction
{
public function __construct($name)
{
parent::__construct(
$name,
\Closure::fromCallable($this->compile(...))->bindTo($this),
\Closure::fromCallable($this->evaluate(...))->bindTo($this)
);
}
private function compile(string $input): string
{
return <<<PHP
(
!\\is_string({$input}) ?
null :
(
mb_convert_case({$input}, \\MB_CASE_TITLE)
)
)
PHP;
}
/**
* @param array<string, mixed> $context
*/
private function evaluate(array $context, string $input): string
{
return mb_convert_case($input, \MB_CASE_TITLE);
}
}