1. Go to this page and download the library: Download tightenco/overload library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tightenco / overload example snippets
class Ticket extends Model
{
// ...
public function holdUntil(DateTime $dateTime)
{
$this->update(['hold_until' => $dateTime]);
}
// ...
}
class Ticket extends Model
{
// ...
public function holdUntil($dateTime)
{
if (is_string($dateTime)) {
$dateTime = Carbon::parse($dateTime);
}
$this->update(['hold_until' => $dateTime]);
}
// ...
}
class Ticket extends Model
{
use Overloadable;
// ...
public function holdUntil(...$args)
{
return $this->overload($args, [
function (string $dateTime) {
$this->update(['hold_until' => Carbon::parse($dateTime)]);
},
function (DateTime $dateTime) {
$this->update(['hold_until' => $dateTime]);
},
]);
}
// ...
}
class Ticket extends Model
{
use Overloadable;
// ...
public function holdUntil(...$args)
{
return $this->overload($args, [
function (string $dateTime) {
$this->holdUntil(Carbon::parse($dateTime));
},
function (DateTime $dateTime) {
$this->update(['hold_until' => $dateTime]);
},
]);
}
// ...
}
public function store()
{
// Super grim! 😭
// ⬇️ ⬇️
$this->validate($request, $rules, [], [], function ($errors) {
return response()->json([
'someOtherInfo' => 'toInclude',
'errors' => $errors
], 422);
});
}
public function store()
{
$this->validate($request, $rules, function ($errors) {
return response()->json([
'someOtherInfo' => 'toInclude',
'errors' => $errors
], 422);
});
}
trait ValidatesRequests
{
// ...
public function validate(...$args)
{
return $this->overload($args, [
function ($request, $rules, Closure $callback) {
return $this->validateRequest($request, $rules, [], [], $callback);
},
function ($request, $rules, $messages, Closure $callback) {
return $this->validateRequest($request, $rules, $messages, [], $callback);
},
'validateRequest',
]);
}
// Move the real logic into a new private function...
protected function validateRequest(Request $request, array $rules, array $messages = [], array $customAttributes = [], Closure $onErrorCallback = null)
{
$validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes);
if ($validator->fails()) {
$this->throwValidationException($request, $validator, $onErrorCallback);
}
}
// ...
}
class SomeOverloadable
{
use Overloadable;
public function someMethod(...$args)
{
return $this->overload($args, [
// Call this closure if two args are passed and the first is an int
function (int $a, $b) {
return 'From the Closure';
},
// Call this method if the args match the args of `methodA` (uses reflection)
'methodA',
// Call this method if the args match the args of `methodB` (uses reflection)
'methodB',
// Call methodC if exactly 2 arguments of any type are passed
'methodC' => ['*', '*'],
// Call methodD if 3 args are passed and the first is an array
'methodD' => ['array', '*', '*'],
// Call methodE if 3 args are passed and the last is a closure
'methodE' => ['*', '*', Closure::class],
]);
}
private function methodA($arg1)
{
return 'Method A';
}
private function methodB(\DateTime $arg1, array $arg2, int $arg3)
{
return 'Method B';
}
private function methodC($arg1, $arg2)
{
return 'Method C';
}
private function methodD($arg1, $arg2, $arg3)
{
return 'Method D';
}
private function methodE($arg1, $arg2, $arg3)
{
return 'Method E';
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.