Created
June 19, 2012 10:57
-
-
Save vojtech-dobes/2953528 to your computer and use it in GitHub Desktop.
Fluent Strings Helper for Nette Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Fluent Strings Helper for Nette Framework | |
* | |
* @author Vojtěch Dobeš <me@vojtechdobes.com> | |
* @license WTFPL | |
*/ | |
class StringObject | |
{ | |
/** @var array */ | |
public static $modifies = array( | |
'capitalize', | |
'firstUpper', | |
'upper', | |
'lower', | |
'indent', | |
'truncate', | |
'webalize', | |
'toAscii', | |
'normalize', | |
'substring', | |
'trim', | |
'padLeft', | |
'padRight', | |
'reverse', | |
'replace', | |
'fixEncoding', | |
); | |
/** @var array */ | |
public static $evaluates = array( | |
'checkEncoding', | |
'startsWith', | |
'endsWith', | |
'contains', | |
'compare', | |
'length', | |
'split', | |
'match', | |
'matchAll', | |
); | |
/** @var string */ | |
private $value; | |
/** | |
* @param string whatever stringifyable | |
*/ | |
public function __construct($value) | |
{ | |
$this->value = (string) $value; | |
} | |
/** | |
* @return string | |
*/ | |
public function get() | |
{ | |
return $this->__toString(); | |
} | |
/** | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
return $this->value; | |
} | |
public function __call($name, $args) | |
{ | |
if (!($modifies = in_array($name, self::$modifies)) && !in_array($name, self::$evaluates)) return parent::__call($name, $args); | |
array_unshift($args, $this->value); | |
$result = call_user_func_array(array('Nette\Utils\Strings', $name), $args); | |
if ($modifies) { | |
$this->value = $result; | |
return $this; | |
} else { | |
return $result; | |
} | |
} | |
} | |
/** | |
* @param string whatever stringifyable | |
* @return StringObject | |
*/ | |
function string($value) | |
{ | |
return new StringObject($value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment