ILIAS  release_8 Revision v8.24
ILIAS\Refinery\String\CaseOfLabel Class Reference

Class CaseOfLabel. More...

+ Inheritance diagram for ILIAS\Refinery\String\CaseOfLabel:
+ Collaboration diagram for ILIAS\Refinery\String\CaseOfLabel:

Public Member Functions

 __construct (string $language_key)
 
 transform ($from)
 @inheritDoc More...
 
- Public Member Functions inherited from ILIAS\Refinery\Transformation
 transform ($from)
 Perform the transformation. More...
 
 applyTo (Result $result)
 Perform the transformation and reify possible failures. More...
 
 __invoke ($from)
 Transformations should be callable. More...
 

Protected Attributes

array $not_capitalize
 

Private Member Functions

 buildPatterns (array $words)
 
 buildPattern (string $word)
 
 replaceHelper (array $result)
 

Private Attributes

string $language_key
 

Detailed Description

Class CaseOfLabel.

Format a text for the title capitalization presentation (Specification at https://docu.ilias.de/goto_docu_pg_1430_42.html)

Throws a LogicException in the transform method, if a not supported language is passed

Definition at line 37 of file CaseOfLabel.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Refinery\String\CaseOfLabel::__construct ( string  $language_key)

Definition at line 185 of file CaseOfLabel.php.

186 {
187 $this->language_key = $language_key;
188 }

References ILIAS\Refinery\String\CaseOfLabel\$language_key.

Member Function Documentation

◆ buildPattern()

ILIAS\Refinery\String\CaseOfLabel::buildPattern ( string  $word)
private

Definition at line 230 of file CaseOfLabel.php.

230 : 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 }

Referenced by ILIAS\Refinery\String\CaseOfLabel\buildPatterns().

+ Here is the caller graph for this function:

◆ buildPatterns()

ILIAS\Refinery\String\CaseOfLabel::buildPatterns ( array  $words)
private

Definition at line 221 of file CaseOfLabel.php.

221 : 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 }

References ILIAS\Refinery\String\CaseOfLabel\buildPattern().

Referenced by ILIAS\Refinery\String\CaseOfLabel\transform().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ replaceHelper()

ILIAS\Refinery\String\CaseOfLabel::replaceHelper ( array  $result)
private

Definition at line 238 of file CaseOfLabel.php.

238 : string
239 {
240 return strtolower($result[0] ?? "");
241 }

◆ transform()

ILIAS\Refinery\String\CaseOfLabel::transform (   $from)

@inheritDoc

Exceptions
LogicException

Implements ILIAS\Refinery\Transformation.

Definition at line 195 of file CaseOfLabel.php.

195 : 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 }

References ILIAS\Refinery\String\CaseOfLabel\buildPatterns().

+ Here is the call graph for this function:

Field Documentation

◆ $language_key

string ILIAS\Refinery\String\CaseOfLabel::$language_key
private

Definition at line 42 of file CaseOfLabel.php.

Referenced by ILIAS\Refinery\String\CaseOfLabel\__construct().

◆ $not_capitalize

array ILIAS\Refinery\String\CaseOfLabel::$not_capitalize
protected

Definition at line 44 of file CaseOfLabel.php.


The documentation for this class was generated from the following file: