-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement PassesClaimConditions rule for custom conditional logic. (#25)
- Loading branch information
1 parent
7b7276b
commit 9402c8b
Showing
5 changed files
with
247 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Enjin\Platform\Beam\Rules; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Validation\DataAwareRule; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Collection; | ||
use Laravel\SerializableClosure\Support\ReflectionClosure; | ||
|
||
class PassesClaimConditions implements DataAwareRule, ValidationRule | ||
{ | ||
protected static array $functions = []; | ||
|
||
protected array $data = []; | ||
|
||
/** | ||
* Create new rule instance. | ||
*/ | ||
public function __construct( | ||
protected bool $singleUse | ||
) { | ||
} | ||
|
||
public static function addConditionalFunctions(Closure|array|Collection $functions): void | ||
{ | ||
if ($functions instanceof Closure) { | ||
$functions = Arr::wrap($functions); | ||
} elseif ($functions instanceof Collection) { | ||
$functions = $functions->toArray(); | ||
} | ||
|
||
static::$functions = array_merge(static::$functions, $functions); | ||
} | ||
|
||
public static function getConditionalFunctions(): array | ||
{ | ||
return static::$functions; | ||
} | ||
|
||
public static function removeConditionalFunctions(Closure|array|Collection $functions): void | ||
{ | ||
if ($functions instanceof Closure) { | ||
$functions = Arr::wrap($functions); | ||
} elseif ($functions instanceof Collection) { | ||
$functions = $functions->toArray(); | ||
} | ||
|
||
$staticFunctions = collect(static::$functions)->mapWithKeys(function ($function) { | ||
$hash = sha1((new ReflectionClosure($function))->getCode()); | ||
|
||
return [$hash => $function]; | ||
}); | ||
|
||
$diffFunctions = collect($functions)->mapWithKeys(function ($function) { | ||
$hash = sha1((new ReflectionClosure($function))->getCode()); | ||
|
||
return [$hash => $function]; | ||
}); | ||
|
||
static::$functions = $staticFunctions->diffKeys($diffFunctions)->values()->toArray(); | ||
} | ||
|
||
public static function clearConditionalFunctions(): void | ||
{ | ||
static::$functions = []; | ||
} | ||
|
||
/** | ||
* Get the validation error message. | ||
*/ | ||
public function message() | ||
{ | ||
return __('enjin-platform-beam::validation.passes_conditions'); | ||
} | ||
|
||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
$conditions = collect(static::$functions); | ||
|
||
if (!$conditions->every(fn ($function) => $function($attribute, $value, $this->singleUse, $this->data))) { | ||
$fail(__('enjin-platform-beam::validation.passes_conditions')); | ||
} | ||
} | ||
|
||
public function setData(array $data) | ||
{ | ||
$this->data = $data; | ||
|
||
return $this; | ||
} | ||
} |
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