Completed
Push — master ( 93f1d6...93c8d2 )
by Ryosuke
03:15
created

CoOption::validateBool()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
3
namespace mpyw\Co\Internal;
4
5
class CoOption implements \ArrayAccess
6
{
7
    /**
8
     * Field types.
9
     */
10
    const TYPES = [
11
        'throw' => 'Bool', // Throw CURLExceptions?
12
        'pipeline' => 'Bool', // Use HTTP/1.1 pipelining?
13
        'multiplex' => 'Bool', // Use HTTP/2 multiplexing?
14
        'interval' => 'NaturalFloat', // curl_multi_select() timeout
15
        'concurrency' => 'NaturalInt', // Limit of TCP connections
16
    ];
17
18
    /**
19
     * Default values.
20
     * @var array
21
     */
22
    private static $defaults = [
23
        'throw' => true,
24
        'pipeline' => false,
25
        'multiplex' => true,
26
        'interval' => 0.5,
27
        'concurrency' => 6,
28
    ];
29
30
    /**
31
     * Actual values.
32
     * @var array
33
     */
34
    private $options;
35
36
    /**
37
     * Set default options.
38
     * @param array $options
39
     */
40 8
    public static function setDefault(array $options)
41 8
    {
42 8
        self::$defaults = self::validateOptions($options) + self::$defaults;
43 8
    }
44
45
    /**
46
     * Get default options.
47
     * @return array $options
48
     */
49 8
    public static function getDefault()
50 8
    {
51 8
        return self::$defaults;
52
    }
53
54
    /**
55
     * Constructor.
56
     * @param array $options
57
     */
58 31
    public function __construct(array $options = [])
59 31
    {
60 31
        $this->options = self::validateOptions($options) + self::$defaults;
61 31
    }
62
63
    /**
64
     * Reconfigure to get new instance.
65
     * @return CoOption
66
     */
67 5
    public function reconfigure(array $options)
68 5
    {
69 5
        return new self($options + $this->options);
70
    }
71
72
    /**
73
     * Implemention of ArrayAccess.
74
     * @param  mixed $offset
75
     * @return bool
76
     */
77 1
    public function offsetExists($offset)
78 1
    {
79 1
        return isset($this->options[$offset]);
80
    }
81
82
    /**
83
     * Implemention of ArrayAccess.
84
     * @param  mixed $offset
85
     * @return mixed
86
     */
87 22
    public function offsetGet($offset)
88 22
    {
89 22
        if (!isset($this->options[$offset])) {
90 1
            throw new \DomainException('Undefined field: ' + $offset);
91
        }
92 21
        return $this->options[$offset];
93
    }
94
95
    /**
96
     * Implemention of ArrayAccess.
97
     * @param  mixed $offset
98
     * @param  mixed $value
99
     * @throws BadMethodCallException
100
     */
101 1
    public function offsetSet($offset, $value)
102 1
    {
103 1
        throw new \BadMethodCallException('The instance of CoOptions is immutable.');
104
    }
105
106
    /**
107
     * Implemention of ArrayAccess.
108
     * @param  mixed $offset
109
     * @throws BadMethodCallException
110
     */
111 1
    public function offsetUnset($offset)
112 1
    {
113 1
        throw new \BadMethodCallException('The instance of CoOptions is immutable.');
114
    }
115
116
    /**
117
     * Validate options.
118
     * @param  array $options
119
     * @return array
120
     */
121 36
    private static function validateOptions(array $options)
122 36
    {
123 36
        foreach ($options as $key => $value) {
124 20
            if (!isset(self::TYPES[$key])) {
125 1
                throw new \InvalidArgumentException("Unknown option: $key");
126
            }
127 20
            $validator = [__CLASS__, 'validate' . self::TYPES[$key]];
128 20
            $options[$key] = $validator($key, $value);
129
        }
130 36
        return $options;
131
    }
132
133
    /**
134
     * Validate bool value.
135
     * @param  string $key
136
     * @param  mixed  $value
137
     * @throws InvalidArgumentException
138
     * @return bool
139
     */
140 16
    private static function validateBool($key, $value)
141 16
    {
142 16
        $value = filter_var($value, FILTER_VALIDATE_BOOLEAN, [
143 16
            'flags' => FILTER_NULL_ON_FAILURE,
144
        ]);
145 16
        if ($value === null) {
146 1
            throw new \InvalidArgumentException("Option[$key] must be boolean.");
147
        }
148 16
        return $value;
149
    }
150
151
    /**
152
     * Validate natural float value.
153
     * @param  string $key
154
     * @param  mixed  $value
155
     * @throws InvalidArgumentException
156
     * @return float
157
     */
158 11 View Code Duplication
    private static function validateNaturalFloat($key, $value)
159 11
    {
160 11
        $value = filter_var($value, FILTER_VALIDATE_FLOAT);
161 11
        if ($value === false || $value < 0.0) {
162 1
            throw new \InvalidArgumentException("Option[$key] must be positive float or zero.");
163
        }
164 11
        return $value;
165
    }
166
167
    /**
168
     * Validate natural int value.
169
     * @param  string $key
170
     * @param  mixed  $value
171
     * @throws InvalidArgumentException
172
     * @return int
173
     */
174 15 View Code Duplication
    private static function validateNaturalInt($key, $value)
175 15
    {
176 15
        $value = filter_var($value, FILTER_VALIDATE_INT);
177 15
        if ($value === false || $value < 0) {
178 1
            throw new \InvalidArgumentException("Option[$key] must be positive integer or zero.");
179
        }
180 15
        return $value;
181
    }
182
}
183