Created
October 17, 2019 06:17
-
-
Save n1215/3da10901a8604a29e494cfc8403be775 to your computer and use it in GitHub Desktop.
str builder
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 | |
declare(strict_types=1); | |
namespace App\Support; | |
use Illuminate\Support\Str; | |
class StrBuilder | |
{ | |
/** | |
* @var string | |
*/ | |
private $value; | |
public function __construct(string $value) | |
{ | |
$this->value = $value; | |
} | |
public function get(): string | |
{ | |
return $this->value; | |
} | |
public function __call($name, $arguments) | |
{ | |
if (!method_exists(Str::class, $name)) { | |
throw new \InvalidArgumentException('method not found'); | |
} | |
$callable = [Str::class, $name]; | |
$methodArguments = $arguments; | |
if (Str::startsWith($name, 'replace')) { | |
array_push($methodArguments, $this->value); | |
} else { | |
array_unshift($methodArguments, $this->value); | |
} | |
$newValue = call_user_func_array($callable, $methodArguments); | |
if (is_string($newValue)) { | |
return new self($newValue); | |
} | |
return $newValue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment