Value::getConstructorForValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Colada\X;
4
5
use ArrayAccess;
6
use BadMethodCallException;
7
8
/**
9
 * Immutable value wrapper
10
 *
11
 * @author Alexey Shokov <[email protected]>
12
 */
13
class Value implements ArrayAccess, ValueWrapper
14
{
15
    /**
16
     * @var mixed
17
     */
18
    private $value;
19
20
    /**
21
     * @return \Closure
22
     */
23
    public static function getConstructorForValue()
24
    {
25
        return function ($value) {
26
            return new static($value);
27
        };
28
    }
29
30
    /**
31
     * @param mixed $value
32
     */
33
    public function __construct($value = null)
34
    {
35
        $this->value = $value;
36
    }
37
38
    /**
39
     * @return mixed
40
     */
41
    public function __getWrappedValue()
42
    {
43
        return $this->value;
44
    }
45
46
    /**
47
     * @param mixed $key
48
     *
49
     * @return static
50
     */
51 View Code Duplication
    public function offsetGet($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        if (is_array_accessible($this->value)) {
54
            return new static($this->value[$key]);
55
        }
56
57
        throw new BadMethodCallException('ArrayAccess unsupported for current value.');
58
    }
59
60
    /**
61
     * @param mixed $key
62
     *
63
     * @return void
64
     */
65
    public function offsetUnset($key)
66
    {
67
        if (is_array_accessible($this->value)) {
68
            unset($this->value[$key]);
69
70
            return;
71
        }
72
73
        throw new BadMethodCallException('ArrayAccess unsupported for current value.');
74
    }
75
76
    /**
77
     * @param mixed $key
78
     *
79
     * @return bool
80
     */
81
    public function offsetExists($key)
82
    {
83
        if (is_array_accessible($this->value)) {
84
            return isset($this->value[$key]);
85
        }
86
87
        throw new BadMethodCallException('ArrayAccess unsupported for current value.');
88
    }
89
90
    /**
91
     * @param mixed $key
92
     * @param mixed $value
93
     *
94
     * @return void
95
     */
96 View Code Duplication
    public function offsetSet($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        if (is_array_accessible($this->value)) {
99
            $this->value[$key] = $value;
100
101
            return;
102
        }
103
104
        throw new BadMethodCallException('ArrayAccess unsupported for current value.');
105
    }
106
107
    /**
108
     * @param string $field
109
     *
110
     * @return static
111
     */
112
    public function __get($field)
113
    {
114
        if (is_object($this->value)) {
115
            return new static($this->value->$field);
116
        }
117
118
        throw new BadMethodCallException('__get() is unsupported for current value.');
119
    }
120
121
    /**
122
     * @param string $field
123
     * @param mixed $value
124
     *
125
     * @return void Result is always the assigned value.
126
     */
127
    public function __set($field, $value)
128
    {
129
        if (is_object($this->value)) {
130
            $this->value->$field = $value;
131
        }
132
133
        throw new BadMethodCallException('__set() is unsupported for current value.');
134
    }
135
136
    public function __isset($field)
137
    {
138
        if (is_object($this->value)) {
139
            return isset($this->value->$field);
140
        }
141
142
        throw new BadMethodCallException('__isset() is unsupported for current value.');
143
    }
144
145
    public function __unset($field)
146
    {
147
        if (is_object($this->value)) {
148
            unset($this->value->$field);
149
150
            return;
151
        }
152
153
        throw new BadMethodCallException('__unset() is unsupported for current value.');
154
    }
155
156
    /**
157
     * @param string $name
158
     * @param array $arguments
159
     *
160
     * @return static
161
     */
162
    public function __call($name, $arguments)
163
    {
164
        if (is_object($this->value) && is_callable([$this->value, $name])) {
165
            $method = [$this->value, $name];
166
        } else {
167
            throw new BadMethodCallException('Unknown method "' . $name . '"');
168
        }
169
170
        $result = call_user_func_array($method, $arguments);
171
172
        return new static($result);
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    public function __toString()
179
    {
180
        // For objects without __toString() an exception will be thrown. This is OK, because the same will happen if
181
        // __toString() is called directly on them.
182
        return (string) $this->value;
183
    }
184
}
185