Validation::push($data)
->validateElementIsNumber()
->validateFirstIsZero()
->toJson();
/**
* Dynamically handle calls to the class.
*
* @param string $method
* @param array $parameters
* @return mixed
*
* @throws \BadMethodCallException
*/
public static function __callStatic($method, $parameters)
{
if (! static::hasMacro($method)) {
throw new BadMethodCallException(sprintf(
'Method %s::%s does not exist.', static::class, $method
));
}
$macro = static::$macros[$method];
if ($macro instanceof Closure) {
$macro = $macro->bindTo(null, static::class);
}
return $macro(...$parameters);
}
/**
* Dynamically handle calls to the class.
*
* @param string $method
* @param array $parameters
* @return mixed
*
* @throws \BadMethodCallException
*/
public function __call($method, $parameters)
{
if (! static::hasMacro($method)) {
throw new BadMethodCallException(sprintf(
'Method %s::%s does not exist.', static::class, $method
));
}
$macro = static::$macros[$method];
if ($macro instanceof Closure) {
$macro = $macro->bindTo($this, static::class);
}
return $macro(...$parameters);
}
快速讓 class 可以擴展 function
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Lang;
Collection::macro('toLocale', function ($locale) {
return $this->map(function ($value) use ($locale) {
return Lang::get($value, [], $locale);
});
});
$collection = collect(['first', 'second']);
$translated = $collection->toLocale('es');
Method Override: __call/__callStatic
<?php
class Dog
{
public function bark()
{
print("One!\n");
}
public function __call(string $name, array $arguments)
{
if ($name == 'meow')
{
print("Meow!\n");
}
if ($name == 'bark') // 因為有定義過,所以不會執行
{
print("AARRR\n");
}
}
}
(new Dog())->bark();
(new Dog())->meow();
class Validation {
use Macroable;
}
Validation::macro('toLocale', function ($locale) {
return $this->map(function ($value) use ($locale) {
return Lang::get($value, [], $locale);
});
});
Validation::push($data)
->validateElementIsNumber()
->validateFirstIsZero()
->toLocale();
快速建立 Factory
class Magician
{
public function getFromHat($name)
{
$func = 'create' . $name;
$this->$func();
}
public function createApple()
{
print('apple');
}
public function createBanana()
{
print('banana');
}
public function createRabbit()
{
print('rabbit');
}
}
public function driver($driver = null)
{
$driver = $driver ?: $this->getDefaultDriver();
if (is_null($driver)) {
throw new InvalidArgumentException(sprintf(
'Unable to resolve NULL driver for [%s].', static::class
));
}
// If the given driver has not been created before, we will create the instances
// here and cache it so we can return it next time very quickly. If there is
// already a driver created by this name, we'll just return that instance.
if (! isset($this->drivers[$driver])) {
$this->drivers[$driver] = $this->createDriver($driver);
}
return $this->drivers[$driver];
}
protected function createDriver($driver)
{
// First, we will determine if a custom driver creator exists for the given driver and
// if it does not we will check for a creator method for the driver. Custom creator
// callbacks allow developers to build their own "drivers" easily using Closures.
if (isset($this->customCreators[$driver])) {
return $this->callCustomCreator($driver);
} else {
$method = 'create'.Str::studly($driver).'Driver';
if (method_exists($this, $method)) {
return $this->$method();
}
}
throw new InvalidArgumentException("Driver [$driver] not supported.");
}
<?php
namespace Illuminate\Hashing;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Support\Manager;
class HashManager extends Manager implements Hasher
{
/**
* Create an instance of the Bcrypt hash Driver.
*
* @return \Illuminate\Hashing\BcryptHasher
*/
public function createBcryptDriver()
{
return new BcryptHasher($this->config->get('hashing.bcrypt') ?? []);
}
/**
* Create an instance of the Argon2i hash Driver.
*
* @return \Illuminate\Hashing\ArgonHasher
*/
public function createArgonDriver()
{
return new ArgonHasher($this->config->get('hashing.argon') ?? []);
}
// ...
/**
* Get the default driver name.
*
* @return string
*/
public function getDefaultDriver()
{
return $this->config->get('hashing.driver', 'bcrypt');
}
}
現成的責任鏈小工具
app(Pipeline::class)
->send([
'user' => $user,
'account' => $account,
])->through([
OneClass::class,
TwoClass::class,
ThreeClass::class,
])
->thenReturn();
<?php
use Closure;
class OneClass
{
public function handle(array $context, Closure $next)
{
// handle 1
$next($context);
// handle 6
return $context;
}
}
class TwoClass
{
public function handle(array $context, Closure $next)
{
// handle 2
$next($context);
// handle 5
return $context;
}
}
class ThreeClass
{
public function handle(array $context, Closure $next)
{
// handle 3
$next($context);
// handle 4
return $context;
}
}
OneClass
TwoClass
ThreeClass
handle 1
handle 2
handle 3 、 4
handle 5
handle 6
class 1
class 2
class 3
$one = new HandleOne();
$two = new HandleTwo();
$three = new HandleThree();
$one.setNext($two);
$two.setNext($three);
$one.handle($request);
public function then(Closure $destination)
{
// $one->handle($request, $two->handle($request, $three->handle($request, ...)))
$pipeline = array_reduce(
array_reverse($this->pipes()), $this->carry(), $this->prepareDestination($destination)
);
return $pipeline($this->passable);
}
protected function carry()
{
return function ($stack, $pipe) {
return function ($passable) use ($stack, $pipe) {
try {
return $pipe($passable, $stack);
} catch (Throwable $e) {
return $this->handleException($passable, $e);
}
};
};
}
如何三秒寫出一個 Pipeline
class AwesomeClass
{
publuc function handle1() {}
publuc function handle2() {}
publuc function handle3() {}
publuc function handle4() {}
publuc function handle5() {}
publuc function handle6() {}
}
function main ()
{
$awesome = new AwesomeClass();
$awesome->handle1();
$awesome->handle2();
$awesome->handle3();
$awesome->handle4();
$awesome->handle5();
$awesome->handle6();
}
單一功能原則 (SRP)
多個物件處理同一個請求
請求者和發送者解耦合
鏈式語法的小工具
DB::query()
->from('user')
->select('created_at', '<', Carbon::now())
->select('is_active', true)
->get();
$query = User::query();
if ($request->has('account')) {
$query->where('account', $request->input('account'));
}
if ($request->has('created_at')) {
$query->where('created_at', >, $request->input('created_at'));
}
if ($request->has('is_active')) {
$query->where('is_active', $request->input('is_active'));
}
$query->get();
trait BuildsQueries
{
use Conditionable;
)
User::query()
->when($request->has('account'), function($query) {
$query->where('account', $request->input('account'));
})
->when($request->has('created_at'), function($query) {
$query->where('created_at', >, $request->input('created_at'));
})
->when($request->has('is_active'), function($query) {
$query->where('is_active', $request->input('is_active'));
})
->get();
public function when($value = null, callable $callback = null, callable $default = null)
{
$value = $value instanceof Closure ? $value($this) : $value;
if ($value) {
return $callback($this, $value) ?? $this;
} elseif ($default) {
return $default($this, $value) ?? $this;
}
return $this;
}
trait Tappable
{
/**
* Call the given Closure with this instance then return the instance.
*
* @param callable|null $callback
* @return $this|\Illuminate\Support\HigherOrderTapProxy
*/
public function tap($callback = null)
{
return tap($this, $callback);
}
}
class AwesomeChain
{
use Tappable;
}
function main ()
{
(new AwesomeChain())->tap(fn() => print('awesome'));
}
public function when($value = null, callable $callback = null, callable $default = null)
{
$value = $value instanceof Closure ? $value($this) : $value;
if (func_num_args() === 0) {
return new HigherOrderWhenProxy($this);
}
if (func_num_args() === 1) {
return (new HigherOrderWhenProxy($this))->condition($value);
}
// ...
}
HigherOrderWhenProxy
public function condition($condition)
{
[$this->condition, $this->hasCondition] = [$condition, true];
return $this;
}
public function __get($key)
{
if (! $this->hasCondition) {
$condition = $this->target->{$key};
return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition);
}
return $this->condition
? $this->target->{$key}
: $this->target;
}
public function __call($method, $parameters)
{
if (! $this->hasCondition) {
$condition = $this->target->{$method}(...$parameters);
return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition);
}
return $this->condition
? $this->target->{$method}(...$parameters)
: $this->target;
}
User::query()
->when()
->condition($request->has('account'))
->where('account', $request->input('account'))
->get();
User::query()
->when($request->has('account'))
->where('account', $request->input('account'))
->get();
動態為類別添加額外職責的模式
HigherOrderProxy
User::query()
->when($request->has('account'), function($query) {
$query->where('account', $request->input('account'));
})
->when($request->has('created_at'), function($query) {
$query->where('created_at', >, $request->input('created_at'));
})
->when($request->has('is_active'), function($query) {
$query->where('is_active', $request->input('is_active'));
})
->get();
use EloquentFilter\ModelFilter;
class UserFilter extends ModelFilter
{
public function account($account)
{
return $this->where('account', $account);
}
public function createdAt($createdAt)
{
return $this->where('created_at', '>', $createdAt);
}
public function isActive($isActive)
{
return $this->where('is_active', $isActive);
}
}
User::query()
->filter($request->all(), UserFilter::class)
->get();
是不是覺得程式碼變得簡潔了呢owo
因為我們把又臭又長的地方移走了