Skip to content

Instantly share code, notes, and snippets.

@vojkny
Created January 15, 2012 02:02
Show Gist options
  • Save vojkny/1613895 to your computer and use it in GitHub Desktop.
Save vojkny/1613895 to your computer and use it in GitHub Desktop.
Maite\String
<?php
namespace Maite;
class String {
/** @var string */
protected static $classInjection = 'Maite\Utils\Strings';
/** @var string */
protected $string = '';
public function __construct($string = '') {
$this->string = (string) $string;
}
public static function from($string) {
return new String($string);
}
public function replace($search, $replace = null) {
if (is_array($search) and is_null($replace)) {
$search = array_keys($search);
$replace = array_values($search);
}
return self::from(str_replace($search, $replace, $this->string));
}
public function iconv($in_charset, $out_charset) {
return self::from(iconv($in_charset, $out_charset, $this->string));
}
public function pregReplace($search, $replace) {
return self::from(preg_replace($search, $replace, $this->string));
}
public function __toString() {
return $this->string;
}
/**
* Magically calls methods of injected string class;
* @param string
* @param array
*/
public function __call($name, $arguments) {
array_unshift($arguments, $this->string);
return self::from(call_user_func_array(self::$classInjection.'::'.$name, $arguments));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment