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