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