Skip to content

Instantly share code, notes, and snippets.

@NoxArt
Forked from vojkny/string.php
Created January 15, 2012 08:11
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 NoxArt/1615024 to your computer and use it in GitHub Desktop.
Save NoxArt/1615024 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 static($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