Skip to content

Commit fef523a

Browse files
committed
add utf8-compatible functions to replace "capitalize', "capitalizeWords", "toLowerCase", "toUpperCase",
add a "truncate" function that adds "..." at the end of the string
1 parent 144504a commit fef523a

6 files changed

+210
-4
lines changed

src/Capitalize.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\StringExpressionLanguage;
6+
7+
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
8+
9+
final class Capitalize extends ExpressionFunction
10+
{
11+
public function __construct($name)
12+
{
13+
parent::__construct(
14+
$name,
15+
\Closure::fromCallable($this->compile(...))->bindTo($this),
16+
\Closure::fromCallable($this->evaluate(...))->bindTo($this)
17+
);
18+
}
19+
20+
private function compile(string $input): string
21+
{
22+
return <<<PHP
23+
(
24+
!\\is_string({$input}) ?
25+
null :
26+
(
27+
mb_convert_case(mb_substr({$input}, 0, 1), \\MB_CASE_UPPER) . mb_substr({$input}, 1, -1)
28+
)
29+
)
30+
PHP;
31+
}
32+
33+
private function evaluate(array $context, string $input)
34+
{
35+
return !\is_string($input) ?
36+
null :
37+
(
38+
mb_convert_case(mb_substr($input, 0, 1), \MB_CASE_UPPER).mb_substr($input, 1, -1)
39+
);
40+
}
41+
}

src/CapitalizeWords.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\StringExpressionLanguage;
6+
7+
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
8+
9+
final class CapitalizeWords extends ExpressionFunction
10+
{
11+
public function __construct($name)
12+
{
13+
parent::__construct(
14+
$name,
15+
\Closure::fromCallable($this->compile(...))->bindTo($this),
16+
\Closure::fromCallable($this->evaluate(...))->bindTo($this)
17+
);
18+
}
19+
20+
private function compile(string $input): string
21+
{
22+
return <<<PHP
23+
(
24+
!\\is_string({$input}) ?
25+
null :
26+
(
27+
mb_convert_case({$input}, \\MB_CASE_TITLE)
28+
)
29+
)
30+
PHP;
31+
}
32+
33+
private function evaluate(array $context, string $input)
34+
{
35+
return !\is_string($input) ?
36+
null :
37+
(
38+
mb_convert_case($input, \MB_CASE_TITLE)
39+
);
40+
}
41+
}

src/StringExpressionLanguageProvider.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@ public function getFunctions(): array
1414
return [
1515
ExpressionFunction::fromPhp('sprintf', 'format'),
1616
ExpressionFunction::fromPhp('trim', 'trim'),
17-
ExpressionFunction::fromPhp('ucfirst', 'capitalize'),
18-
ExpressionFunction::fromPhp('strtolower', 'toLowerCase'),
17+
new Capitalize('capitalize'),
18+
new ToLowerCase('toLowerCase'),
1919
ExpressionFunction::fromPhp('mb_substr', 'search'),
20-
ExpressionFunction::fromPhp('strtoupper', 'toUpperCase'),
20+
new ToUpperCase('toUpperCase'),
2121
ExpressionFunction::fromPhp('number_format', 'formatNumber'),
2222
ExpressionFunction::fromPhp('strpos', 'indexOf'),
2323
ExpressionFunction::fromPhp('str_replace', 'replace'),
2424
ExpressionFunction::fromPhp('strip_tags', 'stripHtml'),
2525
ExpressionFunction::fromPhp('json_decode', 'decode'),
2626
ExpressionFunction::fromPhp('preg_replace', 'replaceByExpression'),
27-
ExpressionFunction::fromPhp('ucwords', 'capitalizeWords'),
27+
new CapitalizeWords('capitalizeWords'),
2828
ExpressionFunction::fromPhp('rtrim', 'removeWhitespaces'),
2929
ExpressionFunction::fromPhp('explode', 'splitIntoArray'),
30+
new Truncate('truncate'),
3031
new FileName('fileName'),
3132
new DateTime('dateTime'),
3233
new FormatDate('formatDate'),

src/ToLowerCase.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\StringExpressionLanguage;
6+
7+
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
8+
9+
final class ToLowerCase extends ExpressionFunction
10+
{
11+
public function __construct($name)
12+
{
13+
parent::__construct(
14+
$name,
15+
\Closure::fromCallable($this->compile(...))->bindTo($this),
16+
\Closure::fromCallable($this->evaluate(...))->bindTo($this)
17+
);
18+
}
19+
20+
private function compile(string $input): string
21+
{
22+
return <<<PHP
23+
(
24+
!\\is_string({$input}) ?
25+
null :
26+
(
27+
mb_convert_case({$input}, \\MB_CASE_LOWER)
28+
)
29+
)
30+
PHP;
31+
}
32+
33+
private function evaluate(array $context, string $input)
34+
{
35+
return !\is_string($input) ?
36+
null :
37+
(
38+
mb_convert_case($input, \MB_CASE_LOWER)
39+
);
40+
}
41+
}

src/ToUpperCase.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\StringExpressionLanguage;
6+
7+
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
8+
9+
final class ToUpperCase extends ExpressionFunction
10+
{
11+
public function __construct($name)
12+
{
13+
parent::__construct(
14+
$name,
15+
\Closure::fromCallable($this->compile(...))->bindTo($this),
16+
\Closure::fromCallable($this->evaluate(...))->bindTo($this)
17+
);
18+
}
19+
20+
private function compile(string $input): string
21+
{
22+
return <<<PHP
23+
(
24+
!\\is_string({$input}) ?
25+
null :
26+
(
27+
mb_convert_case({$input}, \\MB_CASE_UPPER)
28+
)
29+
)
30+
PHP;
31+
}
32+
33+
private function evaluate(array $context, string $input)
34+
{
35+
return !\is_string($input) ?
36+
null :
37+
(
38+
mb_convert_case($input, \MB_CASE_UPPER)
39+
);
40+
}
41+
}

src/Truncate.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\StringExpressionLanguage;
6+
7+
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
8+
9+
final class Truncate extends ExpressionFunction
10+
{
11+
public function __construct($name)
12+
{
13+
parent::__construct(
14+
$name,
15+
\Closure::fromCallable($this->compile(...))->bindTo($this),
16+
\Closure::fromCallable($this->evaluate(...))->bindTo($this)
17+
);
18+
}
19+
20+
private function compile(string $input, string $limit): string
21+
{
22+
return <<<PHP
23+
(
24+
!\\is_string({$input}) || !\\is_int({$limit}) ?
25+
null :
26+
(
27+
rtrim(mb_substr({$input}, 0, {$limit} - 1)) . '…'
28+
)
29+
)
30+
PHP;
31+
}
32+
33+
private function evaluate(array $context, string $input, string $limit)
34+
{
35+
return !\is_string($input) || !\is_int($limit) ?
36+
null :
37+
(
38+
rtrim(mb_substr($input, 0, $limit - 1)).''
39+
);
40+
}
41+
}

0 commit comments

Comments
 (0)