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