Skip to content

Instantly share code, notes, and snippets.

@n1215
Created October 17, 2019 06:17
Show Gist options
  • Save n1215/3da10901a8604a29e494cfc8403be775 to your computer and use it in GitHub Desktop.
Save n1215/3da10901a8604a29e494cfc8403be775 to your computer and use it in GitHub Desktop.
str builder
<?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