ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
ILIAS\Refinery\String\CaseOfLabel Class Reference
+ Inheritance diagram for ILIAS\Refinery\String\CaseOfLabel:
+ Collaboration diagram for ILIAS\Refinery\String\CaseOfLabel:

Public Member Functions

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

Protected Member Functions

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

Protected Attributes

 $language_key
 
 $factory
 
 $not_capitalize
 

Detailed Description

Definition at line 24 of file CaseOfLabel.php.

Constructor & Destructor Documentation

◆ __construct()

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

CaseOfLabel constructor.

Parameters
string$language_key
Factory$factory

Definition at line 186 of file CaseOfLabel.php.

187 {
188 $this->language_key = $language_key;
189 $this->factory = $factory;
190 }

References ILIAS\Refinery\String\CaseOfLabel\$factory, and ILIAS\Refinery\String\CaseOfLabel\$language_key.

Member Function Documentation

◆ __invoke()

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

@inheritDoc

Implements ILIAS\Refinery\Transformation.

Definition at line 228 of file CaseOfLabel.php.

229 {
230 return $this->transform($from);
231 }

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

+ Here is the call graph for this function:

◆ buildPattern()

ILIAS\Refinery\String\CaseOfLabel::buildPattern ( string  $word)
protected
Parameters
string$word
Returns
string

Definition at line 254 of file CaseOfLabel.php.

254 : string
255 {
256 // Before the word muss be the start of the string or a space
257 // After the word muss be the end of the string or a space
258 // Ignore case to include the uppercase in the first step before
259 return "/(\s|^)" . preg_quote($word) . "(\s|$)/i";
260 }

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

+ Here is the caller graph for this function:

◆ buildPatterns()

ILIAS\Refinery\String\CaseOfLabel::buildPatterns ( array  $words)
protected
Parameters
array$words
Returns
array

Definition at line 239 of file CaseOfLabel.php.

239 : array
240 {
241 return array_reduce($words, function (array $patterns, string $word) : array {
242 $patterns[$this->buildPattern($word)] = [ $this, "replaceHelper" ];
243
244 return $patterns;
245 }, []);
246 }

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)
protected
Parameters
array$result
Returns
string

Definition at line 268 of file CaseOfLabel.php.

268 : string
269 {
270 return strtolower($result[0] ?? "");
271 }
$result

References $result.

◆ transform()

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

@inheritDoc

Exceptions
LogicException

Implements ILIAS\Refinery\Transformation.

Definition at line 198 of file CaseOfLabel.php.

199 {
200 if (!is_string($from)) {
201 throw new InvalidArgumentException(__METHOD__ . " the argument is not a string.");
202 }
203
204 if (empty($this->language_key)) {
205 throw new LogicException("Please specify a language for the title capitalization");
206 }
207
208 if (!isset($this->not_capitalize[$this->language_key])) {
209 throw new LogicException("Language " . $this->language_key . " is not supported for the title capitalization");
210 }
211
212 // First write the first letter of each word to uppercase
213 $to = ucwords(strtolower($from));
214
215 // Then replace all special words and write it again to lowercase
216 $to = preg_replace_callback_array($this->buildPatterns($this->not_capitalize[$this->language_key]), $to);
217
218 // Finally the first letter of the whole string muss be always uppercase
219 $to = ucfirst($to);
220
221 return $to;
222 }

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

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

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

Field Documentation

◆ $factory

ILIAS\Refinery\String\CaseOfLabel::$factory
protected

Definition at line 34 of file CaseOfLabel.php.

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

◆ $language_key

ILIAS\Refinery\String\CaseOfLabel::$language_key
protected

Definition at line 30 of file CaseOfLabel.php.

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

◆ $not_capitalize

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

Definition at line 38 of file CaseOfLabel.php.


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