ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 36 of file CaseOfLabel.php.

Constructor & Destructor Documentation

◆ __construct()

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

Definition at line 184 of file CaseOfLabel.php.

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

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

Member Function Documentation

◆ buildPattern()

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

Definition at line 229 of file CaseOfLabel.php.

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

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 220 of file CaseOfLabel.php.

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

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 237 of file CaseOfLabel.php.

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

◆ transform()

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

@inheritDoc

Exceptions
LogicException

Implements ILIAS\Refinery\Transformation.

Definition at line 194 of file CaseOfLabel.php.

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

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 41 of file CaseOfLabel.php.

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

◆ $not_capitalize

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

Definition at line 43 of file CaseOfLabel.php.


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