Completed
Push — master ( 7b06bd...eed05d )
by James Ekow Abaka
01:23
created

Bindings::setProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace ntentan\panie;
4
5
/**
6
 * Holds all the bindings for the dependency injection.
7
 */
8
class Bindings
9
{
10
11
    /**
12
     * An array of all the bindings.
13
     * @var array
14
     */
15
    private $bindings = [];
16
    
17
    /**
18
     * An active key that would be altered with subsequent calls to the bindings object.
19
     * @var string 
20
     */
21
    private $activeKey;
22
23 4
    public function setActiveKey($activeKey)
24
    {
25 4
        $this->activeKey = $activeKey;
26 4
        return $this;
27
    }
28
29
    public function call($method, $parameters = [])
30
    {
31
        $this->bindings[$this->activeKey]['calls'][] = ['setter' => $method, 'parameters' => $parameters];
32
    }
33
34
    public function setProperty($property, $binding)
35
    {
36
        $this->bindings[$this->activeKey]['sets'][] = ['property' => $property, 'binding' => $binding];
37
    }
38
39 4
    public function to($value)
40
    {
41 4
        $this->bindings[$this->activeKey] = ['binding' => $value, 'calls'=>[], 'properties' => []];
42 4
        return $this;
43
    }
44
45 4
    public function get($key)
46
    {
47 4
        return $this->bindings[$key];
48
    }
49
50 6
    public function has($key)
51
    {
52 6
        return isset($this->bindings[$key]);
53
    }
54
55
    public function merge($bindings, $replace = true)
56
    {
57
        foreach($bindings as $key => $binding) {
58
            if(isset($this->bindings[$key]) && !$replace) {
59
                continue;
60
            }
61
            if(is_array($binding)) {
62
                $this->bindings[$key] = ['binding' => $binding[0]];
63
                if(isset($binding['singleton'])) {
64
                    $this->bindings[$key]['singleton'] = $binding['singleton'];
65
                }
66
            } else {
67
                $this->bindings[$key] = ['binding' => $binding];
68
            }
69
        }
70
    }
71
72
}
73