ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
CaseOfLabel.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
6
10use InvalidArgumentException;
11use LogicException;
12
25{
30 protected $language_key;
34 protected $factory;
38 protected $not_capitalize = [
39 "en" => [
40 // conjunctions
41 "after",
42 "although",
43 "and",
44 "as far as",
45 "as how",
46 "as if",
47 "as long as",
48 "as soon as",
49 "as though",
50 "as well as",
51 "as",
52 "because",
53 "before",
54 "both",
55 "but",
56 "either",
57 "even if",
58 "even though",
59 "for",
60 "how",
61 "however",
62 "if only",
63 "in case",
64 "in order that",
65 "if",
66 "neither",
67 "nor",
68 "once",
69 "only",
70 "or",
71 "now",
72 "provided",
73 "rather",
74 "than",
75 "since",
76 "so that",
77 "so",
78 "than",
79 "that",
80 "though",
81 "till",
82 "unless",
83 "until",
84 "when",
85 "whenever",
86 "where as",
87 "wherever",
88 "whether",
89 "while",
90 "where",
91 "yet",
92 // prepositions
93 "about",
94 "above",
95 "according to",
96 "across",
97 "after",
98 "against",
99 "along with",
100 "along",
101 "among",
102 "apart from",
103 "around",
104 "as for",
105 "as",
106 "at",
107 "because of",
108 "before",
109 "behind",
110 "below",
111 "beneath",
112 "beside",
113 "between",
114 "beyond",
115 "but",
116 "by means of",
117 "by",
118 "concerning",
119 "despite",
120 "down",
121 "during",
122 "except for",
123 "excepting",
124 "except",
125 "for",
126 "from",
127 "in addition to",
128 "in back of",
129 "in case of",
130 "in front of",
131 "in place of",
132 "inside",
133 "in spite of",
134 "instead of",
135 "into",
136 "in",
137 "like",
138 "near",
139 "next",
140 "off",
141 "onto",
142 "on top of",
143 "out out of",
144 "outside",
145 "over",
146 "past",
147 "regarding",
148 "round",
149 "on",
150 "of",
151 "since",
152 "through",
153 "throughout",
154 "till",
155 "toward",
156 "under",
157 "underneath",
158 "unlike",
159 "until",
160 "upon",
161 "up to",
162 "up",
163 "to",
164 "with",
165 "within",
166 "without",
167 // articles
168 "a",
169 "an",
170 "few",
171 "some",
172 "the",
173 "one",
174 "this",
175 "that"
176 ]
177 ];
178
179
186 public function __construct(string $language_key, Factory $factory)
187 {
188 $this->language_key = $language_key;
189 $this->factory = $factory;
190 }
191
192
198 public function transform($from)
199 {
200 if (!is_string($from)) {
201 throw new InvalidArgumentException(__METHOD__ . " the argument is not a string.");
202 }
203
204 if (empty($this->language_key)) {
205 throw new LogicException("Please specify a language for the title capitalization");
206 }
207
208 if (!isset($this->not_capitalize[$this->language_key])) {
209 throw new LogicException("Language " . $this->language_key . " is not supported for the title capitalization");
210 }
211
212 // First write the first letter of each word to uppercase
213 $to = ucwords(strtolower($from));
214
215 // Then replace all special words and write it again to lowercase
216 $to = preg_replace_callback_array($this->buildPatterns($this->not_capitalize[$this->language_key]), $to);
217
218 // Finally the first letter of the whole string muss be always uppercase
219 $to = ucfirst($to);
220
221 return $to;
222 }
223
224
228 public function __invoke($from)
229 {
230 return $this->transform($from);
231 }
232
233
239 protected function buildPatterns(array $words) : array
240 {
241 return array_reduce($words, function (array $patterns, string $word) : array {
242 $patterns[$this->buildPattern($word)] = [ $this, "replaceHelper" ];
243
244 return $patterns;
245 }, []);
246 }
247
248
254 protected function buildPattern(string $word) : string
255 {
256 // Before the word muss be the start of the string or a space
257 // After the word muss be the end of the string or a space
258 // Ignore case to include the uppercase in the first step before
259 return "/(\s|^)" . preg_quote($word) . "(\s|$)/i";
260 }
261
262
268 protected function replaceHelper(array $result) : string
269 {
270 return strtolower($result[0] ?? "");
271 }
272}
$result
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
__construct(string $language_key, Factory $factory)
CaseOfLabel constructor.
A transformation is a function from one datatype to another.