-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionable.php
More file actions
88 lines (75 loc) · 2.62 KB
/
Conditionable.php
File metadata and controls
88 lines (75 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* This file is part of Blitz PHP framework.
*
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace BlitzPHP\Traits;
use BlitzPHP\Traits\Mixins\HigherOrderWhenProxy;
use Closure;
trait Conditionable
{
/**
* Appliquez le callback si la "valeur" donnée est (ou se résout) véridique.
*
* @template TWhenParameter
* @template TWhenReturnType
*
* @param (Closure($this): TWhenParameter)|TWhenParameter|null $value
* @param (callable($this, TWhenParameter): TWhenReturnType)|null $callback
* @param (callable($this, TWhenParameter): TWhenReturnType)|null $default
* @param mixed|null $value
*
* @return $this|TWhenReturnType
*/
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);
}
if ($value) {
return $callback($this, $value) ?? $this;
}
if ($default) {
return $default($this, $value) ?? $this;
}
return $this;
}
/**
* Appliquez le callback si la "valeur" donnée est (ou se résout) fausse.
*
* @template TUnlessParameter
* @template TUnlessReturnType
*
* @param (Closure($this): TUnlessParameter)|TUnlessParameter|null $value
* @param (callable($this, TUnlessParameter): TUnlessReturnType)|null $callback
* @param (callable($this, TUnlessParameter): TUnlessReturnType)|null $default
* @param mixed|null $value
*
* @return $this|TUnlessReturnType
*/
public function unless($value = null, ?callable $callback = null, ?callable $default = null)
{
$value = $value instanceof Closure ? $value($this) : $value;
if (func_num_args() === 0) {
return (new HigherOrderWhenProxy($this))->negateConditionOnCapture();
}
if (func_num_args() === 1) {
return (new HigherOrderWhenProxy($this))->condition(! $value);
}
if (! $value) {
return $callback($this, $value) ?? $this;
}
if ($default) {
return $default($this, $value) ?? $this;
}
return $this;
}
}