Passed
Push — master ( 1db8b5...5d2efa )
by Php Easy Api
02:51
created

Macro::setValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Support;
4
5
use Resta\Contracts\MacroAbleContracts;
6
use Resta\Foundation\ApplicationProvider;
7
8
class Macro extends ApplicationProvider
9
{
10
    /**
11
     * @var bool $isMacro
12
     */
13
    protected $isMacro = false;
14
15
    /**
16
     * @var $macro
0 ignored issues
show
Documentation Bug introduced by
The doc comment $macro at position 0 could not be parsed: Unknown type name '$macro' at position 0 in $macro.
Loading history...
17
     */
18
    protected $macro;
19
20
    /**
21
     * @var $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment $class at position 0 could not be parsed: Unknown type name '$class' at position 0 in $class.
Loading history...
22
     */
23
    protected $class;
24
25
    /**
26
     * @var string
27
     */
28
    protected $values = null;
29
30
    /**
31
     * check conditions for macro
32
     *
33
     * @param bool $static
34
     * @return bool
35
     */
36
    protected function checkMacroConditions($static=false)
37
    {
38
        return is_string($this->macro) &&
39
            Utils::isNamespaceExists($this->macro) &&
40
            $this->checkMacroInstanceOf($static);
41
    }
42
43
    /**
44
     * check macro instanceOf or static class
45
     *
46
     * @param bool $static
47
     * @return bool
48
     */
49
    protected function checkMacroInstanceOf($static=false)
50
    {
51
        if($static){
52
            return true;
53
        }
54
        return $this->app->resolve($this->macro) instanceof MacroAbleContracts;
55
    }
56
57
    /**
58
     * get macro object
59
     *
60
     * @param null|string $method
61
     * @param callable $callback
62
     * @return mixed
63
     */
64
    public function get($method=null,callable $callback)
65
    {
66
        if($this->isMacro){
67
68
            if(is_null($method) && Utils::isNamespaceExists($this->macro)){
69
                return $this->app->resolve($this->macro)($this->getValues());
70
            }
71
72
            if(method_exists($resolve = $this->app->resolve($this->macro),$method)){
73
                return $resolve->macro($this->class);
74
            }
75
        }
76
        return call_user_func($callback);
77
    }
78
79
    /**
80
     * get values for macro
81
     *
82
     * @return mixed
83
     */
84
    public function getValues()
85
    {
86
        return $this->values;
87
    }
88
89
    /**
90
     * is availability macro for class
91
     *
92
     * @param bool $static
93
     * @param $class
94
     * @return $this
95
     */
96
    public function isMacro($class,$static=false)
97
    {
98
        // if the macro class is a valid object,
99
        // then this macro will return a boolean value if it has the specified methode.
100
        if($this->checkMacroConditions($static)){
101
102
            $this->isMacro  = true;
103
            $this->class    = $class;
104
        }
105
106
        return $this;
107
    }
108
109
    /**
110
     * set values for macro
111
     *
112
     * @param $values
113
     * @return mixed
114
     */
115
    public function setValues($values)
116
    {
117
        return $this->values = $values;
118
    }
119
120
    /**
121
     * check been runnable with which macro
122
     *
123
     * @param $macro
124
     * @param $concrete
125
     * @param $method
126
     * @return mixed
127
     */
128
    public function with($macro,$concrete,$method=null)
129
    {
130
        if($this->macro === null){
131
            return $this($macro)->isMacro($concrete)->get($method,function() use($concrete){
132
                return $concrete;
133
            });
134
        }
135
    }
136
137
    /**
138
     * check been runnable with which static macro
139
     *
140
     * @param $macro
141
     * @param $concrete
142
     * @param $method
143
     * @return mixed
144
     */
145
    public function withStatic($macro,$concrete)
146
    {
147
        return $this($macro)->isMacro($concrete,true)->get(null,is_callable($concrete) ?
148
            $concrete : function() use($concrete){
149
                return $concrete;
150
            });
151
    }
152
153
    /**
154
     * invoke construct for macro
155
     *
156
     * @param null $macro
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $macro is correct as it would always require null to be passed?
Loading history...
157
     * @return $this
158
     */
159
    public function __invoke($macro=null)
160
    {
161
        if($macro!==null){
0 ignored issues
show
introduced by
The condition $macro !== null is always false.
Loading history...
162
            $this->macro = $macro;
163
        }
164
        return $this;
165
    }
166
}