-
-
Save NoxArt/1615024 to your computer and use it in GitHub Desktop.
Maite\String
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 | |
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