Completed
Push — master ( a9d702...411729 )
by Korotkov
04:16 queued 01:48
created

ContainerReflectionTrait::hasBinding()   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
declare(strict_types=1);
4
5
/**
6
 * @author    : Korotkov Danila <[email protected]>
7
 * @copyright Copyright (c) 2018, Korotkov Danila
8
 * @license   http://www.gnu.org/licenses/gpl.html GNU GPLv3.0
9
 */
10
11
namespace Rudra\Traits;
12
13
use \ReflectionClass;
14
use \ReflectionMethod;
15
16
/**
17
 * Trait ContainerReflectionTrait
18
 * @package Rudra
19
 */
20
trait ContainerReflectionTrait
21
{
22
23
    /**
24
     * @var array
25
     */
26
    protected $objects = [];
27
    /**
28
     * @var array
29
     */
30
    protected $bind = [];
31
32
    /**
33
     * @param string $key
34
     * @param        $object
35
     */
36
    protected function rawSet(string $key, $object)
37
    {
38
        $this->objects[$key] = $object;
39
    }
40
41
    /**
42
     * @param string $key
43
     * @param        $object
44
     * @param null   $params
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $params is correct as it would always require null to be passed?
Loading history...
45
     */
46
    protected function iOc(string $key, $object, $params = null): void
47
    {
48
        $reflection  = new ReflectionClass($object);
49
        $constructor = $reflection->getConstructor();
50
51
        if ($constructor && $constructor->getNumberOfParameters()) {
52
            $paramsIoC           = $this->getParamsIoC($constructor, $params);
53
            $this->objects[$key] = $reflection->newInstanceArgs($paramsIoC);
54
            return;
55
        }
56
57
        $this->objects[$key] = new $object;
58
    }
59
60
    /**
61
     * @param      $object
62
     * @param null $params
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $params is correct as it would always require null to be passed?
Loading history...
63
     * @return mixed|object
64
     */
65
    public function new($object, $params = null)
66
    {
67
        $reflection  = new ReflectionClass($object);
68
        $constructor = $reflection->getConstructor();
69
70
        if ($constructor && $constructor->getNumberOfParameters()) {
71
            $paramsIoC = $this->getParamsIoC($constructor, $params);
72
73
            return $reflection->newInstanceArgs($paramsIoC);
74
        }
75
76
        return new $object;
77
    }
78
79
    /**
80
     * @param ReflectionMethod $constructor
81
     * @param                  $params
82
     * @return array
83
     */
84
    protected function getParamsIoC(ReflectionMethod $constructor, $params): array
85
    {
86
        $paramsIoC = [];
87
        foreach ($constructor->getParameters() as $key => $value) {
88
89
            if (isset($value->getClass()->name) && $this->hasBinding($value->getClass()->name)) {
90
                $className       = $this->getBinding($value->getClass()->name);
91
                $paramsIoC[$key] = (is_object($className)) ? $className : new $className;
92
                continue;
93
            }
94
95
            if ($value->isDefaultValueAvailable() && !isset($params[$value->getName()])) {
96
                $paramsIoC[$key] = $value->getDefaultValue();
97
                continue;
98
            }
99
100
            $paramsIoC[$key] = $params[$value->getName()];
101
        }
102
103
        return $paramsIoC;
104
    }
105
106
107
    /**
108
     * @param string|null $key
109
     * @return array|mixed
110
     */
111
    public function get(string $key = null)
112
    {
113
        return (empty($key)) ? $this->objects : $this->objects[$key];
114
    }
115
116
    /**
117
     * @param string $key
118
     * @param        $object
119
     * @param null   $params
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $params is correct as it would always require null to be passed?
Loading history...
120
     * @return object|void
121
     */
122
    public function set(string $key, $object, $params = null)
123
    {
124
        ('raw' == $params) ? $this->rawSet($key, $object) : $this->iOc($key, $object, $params);
125
    }
126
127
    /**
128
     * @param string $key
129
     * @return bool
130
     */
131
    public function has(string $key): bool
132
    {
133
        return isset($this->objects[$key]);
134
    }
135
136
    /**
137
     * @param string $key
138
     * @param string $param
139
     * @return mixed
140
     */
141
    public function getParam(string $key, string $param)
142
    {
143
        if ($this->has($key) && isset($this->get($key)->$param)) {
144
            return $this->get($key)->$param;
145
        }
146
    }
147
148
    /**
149
     * @param string $key
150
     * @param string $param
151
     * @param        $value
152
     */
153
    public function setParam(string $key, string $param, $value): void
154
    {
155
        if (isset($this->objects[$key])) {
156
            $this->get($key)->$param = $value;
157
        }
158
    }
159
160
    /**
161
     * @param string $key
162
     * @param string $param
163
     * @return bool
164
     */
165
    public function hasParam(string $key, string $param)
166
    {
167
        if ($this->has($key)) {
168
            return isset($this->get($key)->$param);
169
        }
170
    }
171
172
    /**
173
     * @param string $key
174
     * @return mixed|string
175
     */
176
    public function getBinding(string $key)
177
    {
178
        return $this->bind[$key] ?? $key;
179
    }
180
181
    /**
182
     * @param string $key
183
     * @return bool
184
     */
185
    public function hasBinding(string $key): bool
186
    {
187
        return array_key_exists($key, $this->bind);
188
    }
189
190
    /**
191
     * @param string $key
192
     * @param        $value
193
     */
194
    public function setBinding(string $key, $value): void
195
    {
196
        $this->bind[$key] = $value;
197
    }
198
}
199