ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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)
 
- Public Member Functions inherited from ILIAS\Refinery\Transformation
 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 25 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 189 of file CaseOfLabel.php.

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

190  {
191  $this->language_key = $language_key;
192  $this->factory = $factory;
193  }

Member Function Documentation

◆ buildPattern()

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

Definition at line 247 of file CaseOfLabel.php.

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

247  : string
248  {
249  // Before the word muss be the start of the string or a space
250  // After the word muss be the end of the string or a space
251  // Ignore case to include the uppercase in the first step before
252  return "/(\s|^)" . preg_quote($word) . "(\s|$)/i";
253  }
+ 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 232 of file CaseOfLabel.php.

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

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

232  : array
233  {
234  return array_reduce($words, function (array $patterns, string $word) : array {
235  $patterns[$this->buildPattern($word)] = [ $this, "replaceHelper" ];
236 
237  return $patterns;
238  }, []);
239  }
+ 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 261 of file CaseOfLabel.php.

261  : string
262  {
263  return strtolower($result[0] ?? "");
264  }
$result

◆ transform()

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

Exceptions
LogicException

Implements ILIAS\Refinery\Transformation.

Definition at line 201 of file CaseOfLabel.php.

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

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

Field Documentation

◆ $factory

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

Definition at line 37 of file CaseOfLabel.php.

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

◆ $language_key

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

Definition at line 33 of file CaseOfLabel.php.

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

◆ $not_capitalize

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

Definition at line 41 of file CaseOfLabel.php.


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