Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Support;
4
 
5
class HigherOrderWhenProxy
6
{
7
    /**
8
     * The target being conditionally operated on.
9
     *
10
     * @var mixed
11
     */
12
    protected $target;
13
 
14
    /**
15
     * The condition for proxying.
16
     *
17
     * @var bool
18
     */
19
    protected $condition;
20
 
21
    /**
22
     * Indicates whether the proxy has a condition.
23
     *
24
     * @var bool
25
     */
26
    protected $hasCondition = false;
27
 
28
    /**
29
     * Determine whether the condition should be negated.
30
     *
31
     * @var bool
32
     */
33
    protected $negateConditionOnCapture;
34
 
35
    /**
36
     * Create a new proxy instance.
37
     *
38
     * @param  mixed  $target
39
     * @return void
40
     */
41
    public function __construct($target)
42
    {
43
        $this->target = $target;
44
    }
45
 
46
    /**
47
     * Set the condition on the proxy.
48
     *
49
     * @param  bool  $condition
50
     * @return $this
51
     */
52
    public function condition($condition)
53
    {
54
        [$this->condition, $this->hasCondition] = [$condition, true];
55
 
56
        return $this;
57
    }
58
 
59
    /**
60
     * Indicate that the condition should be negated.
61
     *
62
     * @return $this
63
     */
64
    public function negateConditionOnCapture()
65
    {
66
        $this->negateConditionOnCapture = true;
67
 
68
        return $this;
69
    }
70
 
71
    /**
72
     * Proxy accessing an attribute onto the target.
73
     *
74
     * @param  string  $key
75
     * @return mixed
76
     */
77
    public function __get($key)
78
    {
79
        if (! $this->hasCondition) {
80
            $condition = $this->target->{$key};
81
 
82
            return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition);
83
        }
84
 
85
        return $this->condition
86
            ? $this->target->{$key}
87
            : $this->target;
88
    }
89
 
90
    /**
91
     * Proxy a method call on the target.
92
     *
93
     * @param  string  $method
94
     * @param  array  $parameters
95
     * @return mixed
96
     */
97
    public function __call($method, $parameters)
98
    {
99
        if (! $this->hasCondition) {
100
            $condition = $this->target->{$method}(...$parameters);
101
 
102
            return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition);
103
        }
104
 
105
        return $this->condition
106
            ? $this->target->{$method}(...$parameters)
107
            : $this->target;
108
    }
109
}