Completed
Branch master (b454c5)
by Ryuichi
13:04
created

Injector   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 65
rs 10
c 1
b 0
f 0
1
<?php
2
namespace WebStream\DI;
3
4
/**
5
 * Injector
6
 * @author Ryuichi TANAKA.
7
 * @since 2015/12/26
8
 * @version 0.7
9
 */
10
trait Injector
11
{
12
    /**
13
     * @var array<string> プロパティマップ
14
     */
15
    private $__propertyMap;
16
17
    /**
18
     * オブジェクトを注入する
19
     * @param string プロパティ名
20
     * @param mixed オブジェクト
21
     * @return Injector
22
     */
23
    public function inject($name, $object)
24
    {
25
        $this->{$name} = $object;
26
27
        return $this;
28
    }
29
30
    /**
31
     * overload setter
32
     */
33
    public function __set($name, $value)
34
    {
35
        if ($this->__propertyMap === null) {
36
            $this->__propertyMap = [];
37
        }
38
        $this->__propertyMap[$name] = $value;
39
    }
40
41
    /**
42
     * overload setter
43
     */
44
    public function __get($name)
45
    {
46
        return $this->__propertyMap !== null && array_key_exists($name, $this->__propertyMap) ? $this->__propertyMap[$name] : null;
47
    }
48
49
    /**
50
     * overload isset
51
     */
52
    public function __isset($name)
53
    {
54
        return $this->__propertyMap === null && array_key_exists($name, $this->__propertyMap) === false;
55
    }
56
57
    /**
58
     * overload unset
59
     */
60
    public function __unset($name)
61
    {
62
        if ($this->__propertyMap !== null && array_key_exists($name, $this->__propertyMap)) {
63
            unset($this->__propertyMap[$name]);
64
        }
65
    }
66
67
    /**
68
     * コンテナクリア
69
     */
70
    public function __clear()
71
    {
72
        $this->__propertyMap = null;
73
    }
74
}
75