-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
StringExtension.php
99 lines (85 loc) · 3.04 KB
/
StringExtension.php
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?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\String;
use Symfony\Component\String\AbstractUnicodeString;
use Symfony\Component\String\Inflector\EnglishInflector;
use Symfony\Component\String\Inflector\FrenchInflector;
use Symfony\Component\String\Inflector\InflectorInterface;
use Symfony\Component\String\Inflector\SpanishInflector;
use Symfony\Component\String\Slugger\AsciiSlugger;
use Symfony\Component\String\Slugger\SluggerInterface;
use Symfony\Component\String\UnicodeString;
use Twig\Error\RuntimeError;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
final class StringExtension extends AbstractExtension
{
private $slugger;
private $englishInflector;
private $spanishInflector;
private $frenchInflector;
public function __construct(?SluggerInterface $slugger = null)
{
$this->slugger = $slugger ?: new AsciiSlugger();
}
public function getFilters(): array
{
return [
new TwigFilter('u', [$this, 'createUnicodeString']),
new TwigFilter('slug', [$this, 'createSlug']),
new TwigFilter('plural', [$this, 'plural']),
new TwigFilter('singular', [$this, 'singular']),
];
}
public function createUnicodeString(?string $text): UnicodeString
{
return new UnicodeString($text ?? '');
}
public function createSlug(string $string, string $separator = '-', ?string $locale = null): AbstractUnicodeString
{
return $this->slugger->slug($string, $separator, $locale);
}
/**
* @return array|string
*/
public function plural(string $value, string $locale = 'en', bool $all = false)
{
if ($all) {
return $this->getInflector($locale)->pluralize($value);
}
return $this->getInflector($locale)->pluralize($value)[0];
}
/**
* @return array|string
*/
public function singular(string $value, string $locale = 'en', bool $all = false)
{
if ($all) {
return $this->getInflector($locale)->singularize($value);
}
return $this->getInflector($locale)->singularize($value)[0];
}
private function getInflector(string $locale): InflectorInterface
{
switch ($locale) {
case 'en':
return $this->englishInflector ?? $this->englishInflector = new EnglishInflector();
case 'es':
if (!class_exists(SpanishInflector::class)) {
throw new RuntimeError('SpanishInflector is not available.');
}
return $this->spanishInflector ?? $this->spanishInflector = new SpanishInflector();
case 'fr':
return $this->frenchInflector ?? $this->frenchInflector = new FrenchInflector();
default:
throw new \InvalidArgumentException(\sprintf('Locale "%s" is not supported.', $locale));
}
}
}