Passed
Pull Request — master (#10)
by Korotkov
01:41
created

ContainerReflectionTrait::setParam()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 3
dl 0
loc 4
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
            if (isset($value->getClass()->name)) {
89
                $className       = $this->getBinding($value->getClass()->name);
90
                $paramsIoC[$key] = (is_object($className)) ? $className : new $className;
91
                continue;
92
            }
93
94
            if ($value->isDefaultValueAvailable()) {
95
                $paramsIoC[$key] = $value->getDefaultValue();
96
                continue;
97
            }
98
99
            $paramsIoC[$key] = $params[$value->getName()];
100
        }
101
102
        return $paramsIoC;
103
    }
104
105
106
    /**
107
     * @param string|null $key
108
     * @return array|mixed
109
     */
110
    public function get(string $key = null)
111
    {
112
        return (empty($key)) ? $this->objects : $this->objects[$key];
113
    }
114
115
    /**
116
     * @param string $key
117
     * @param        $object
118
     * @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...
119
     * @return object|void
120
     */
121
    public function set(string $key, $object, $params = null)
122
    {
123
        ('raw' == $params) ? $this->rawSet($key, $object) : $this->iOc($key, $object, $params);
124
    }
125
126
    /**
127
     * @param string $key
128
     * @return bool
129
     */
130
    public function has(string $key): bool
131
    {
132
        return isset($this->objects[$key]);
133
    }
134
135
    /**
136
     * @param string $key
137
     * @param string $param
138
     * @return mixed
139
     */
140
    public function getParam(string $key, string $param)
141
    {
142
        if ($this->has($key) && isset($this->get($key)->$param)) {
143
            return $this->get($key)->$param;
144
        }
145
    }
146
147
    /**
148
     * @param string $key
149
     * @param string $param
150
     * @param        $value
151
     */
152
    public function setParam(string $key, string $param, $value): void
153
    {
154
        if (isset($this->objects[$key])) {
155
            $this->get($key)->$param = $value;
156
        }
157
    }
158
159
    /**
160
     * @param string $key
161
     * @param string $param
162
     * @return bool
163
     */
164
    public function hasParam(string $key, string $param)
165
    {
166
        if ($this->has($key)) {
167
            return isset($this->get($key)->$param);
168
        }
169
    }
170
171
    /**
172
     * @param string $key
173
     * @return mixed|string
174
     */
175
    public function getBinding(string $key)
176
    {
177
        return $this->bind[$key] ?? $key;
178
    }
179
180
    /**
181
     * @param string $key
182
     * @param        $value
183
     */
184
    public function setBinding(string $key, $value): void
185
    {
186
        $this->bind[$key] = $value;
187
    }
188
}
189