Issues (2)

Container.php (2 issues)

1
<?php
2
3
namespace WebStream\Container;
4
5
use WebStream\Exception\Extend\InvalidArgumentException;
0 ignored issues
show
The type WebStream\Exception\Exte...nvalidArgumentException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
/**
8
 * Containerクラス
9
 * @author Ryuichi TANAKA.
10
 * @since 2013/01/12
11
 * @version 0.4
12
 */
13
class Container
14
{
15
    /**
16
     * @var array<string> パラメータMap
17
     */
18
    protected $values = [];
19
20
    /**
21
     * @var bool strict container flag
22
     */
23
    private $isStrict;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 15
    public function __construct($isStrict = true)
29
    {
30 15
        $this->isStrict = $isStrict;
31
    }
32
33
    /**
34
     * magic method of set
35
     * @param  string $key   キー
36
     * @param  mixed  $value 値
37
     * @return void
38
     */
39 6
    public function __set($key, $value)
40
    {
41 6
        if ($value instanceof \Closure) {
42 1
            call_user_func_array([$this, 'registerAsLazy'], [$key, $value]);
43
        } else {
44 5
            $this->set($key, $value);
45
        }
46
    }
47
48
    /**
49
     * magic method of get
50
     * @param  string $key キー
51
     * @return mixed  値
52
     */
53 14
    public function __get($key)
54
    {
55 14
        return $this->get($key);
56
    }
57
58
    /**
59
     * 未定義のメソッドを処理
60
     * @param  string $name      メソッド名
61
     * @param  array  $arguments 引数リスト
62
     * @return void
63
     */
64
    public function __call($name, $arguments)
65
    {
66
        if ($arguments[0] instanceof \Closure) {
67
            array_unshift($arguments, $name);
68
            call_user_func_array([$this, 'registerAsLazy'], $arguments);
69
        } else {
70
            $this->set($name, $arguments[0]);
71
        }
72
    }
73
74
    /**
75
     * キーの値を設定する
76
     * @param  string $name      メソッド名
77
     * @param  array  $arguments 引数リスト
78
     * @return void
79
     */
80 9
    public function set($key, $value)
81
    {
82 9
        $this->values[$key] = $value;
83
    }
84
85
    /**
86
     * 格納した値を取得
87
     * @param  string                   $key キー
88
     * @throws InvalidArgumentException 引数例外
89
     * @return mixed                    格納値
90
     */
91 14
    public function get($key)
92
    {
93 14
        if (!isset($this->values[$key])) {
94 2
            if ($this->isStrict) {
95 1
                throw new InvalidArgumentException("The value of the specified key does not exist: $key");
96
            } else {
97 1
                return null;
98
            }
99
        }
100 12
        if ($this->values[$key] instanceof ValueProxy) {
0 ignored issues
show
$this->values[$key] is never a sub-type of WebStream\Container\ValueProxy.
Loading history...
101 4
            return $this->values[$key]->fetch();
102
        } else {
103 9
            return $this->values[$key];
104
        }
105
    }
106
107
    /**
108
     * 要素の格納数を返却するを設定する
109
     * @return integer 格納数
110
     */
111 1
    public function length()
112
    {
113 1
        return count($this->values);
114
    }
115
116
    /**
117
     * 格納された値を削除する
118
     * @param  string $key キー
119
     * @return void
120
     */
121 1
    public function remove($key)
122
    {
123 1
        unset($this->values[$key]);
124
    }
125
126
    /**
127
     * 値を登録する
128
     * @param  string $key   キー
129
     * @param  string $value 値
130
     * @return void
131
     */
132 4
    public function register($key, $value)
133
    {
134 4
        $this->set($key, $value);
135
    }
136
137
    /**
138
     * 即時実行した値を登録する
139
     * @param  string   $key      キー
140
     * @param  callable $callback クロージャ
141
     * @param  array    $context  クロージャの引数リスト
142
     * @return void
143
     */
144 2
    public function registerAsDynamic($key, $callback, $context = [])
145
    {
146 2
        $valueObject = new ValueProxy($callback, $context, true);
147 2
        $this->values[$key] = $valueObject->fetch();
148
    }
149
150
    /**
151
     * 遅延評価の値を登録する
152
     * @param  string   $key      キー
153
     * @param  callable $callback クロージャ
154
     * @param  array    $context  クロージャの引数リスト
155
     * @return void
156
     */
157 3
    public function registerAsLazy($key, $callback, $context = [])
158
    {
159 3
        $this->values[$key] = new ValueProxy($callback, $context, true);
160
    }
161
162
    /**
163
     * 遅延評価の値を登録する
164
     * 繰り返し実行されたときにキャッシュしない
165
     * @param  string   $key      キー
166
     * @param  callable $callback クロージャ
167
     * @param  array    $context  クロージャの引数リスト
168
     * @return void
169
     */
170 1
    public function registerAsLazyUnCached($key, $callback, $context = [])
171
    {
172 1
        $this->values[$key] = new ValueProxy($callback, $context, false);
173
    }
174
}
175