Skip to content

Instantly share code, notes, and snippets.

@vojtech-dobes
Created June 19, 2012 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vojtech-dobes/2953528 to your computer and use it in GitHub Desktop.
Save vojtech-dobes/2953528 to your computer and use it in GitHub Desktop.
Fluent Strings Helper for Nette Framework
<?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