1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Bernardo Secades |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in all |
16
|
|
|
* copies or substantial portions of the Software. |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace BernardoSecades\Json; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @author bernardosecades <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class ArrayOption implements \ArrayAccess, \Countable |
32
|
|
|
{ |
33
|
|
|
protected $options = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param mixed $index |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
1 |
|
public function offsetExists($index) |
40
|
|
|
{ |
41
|
1 |
|
return isset($this->options[$index]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param mixed $index |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
1 |
|
public function offsetGet($index) |
49
|
|
|
{ |
50
|
1 |
|
if ($this->offsetExists($index)) { |
51
|
1 |
|
return $this->options[$index]; |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param mixed $index |
59
|
|
|
* @param mixed $value |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
10 |
|
public function offsetSet($index, $value) |
63
|
|
|
{ |
64
|
10 |
|
if (!$value instanceof Option) { |
65
|
1 |
|
throw new \UnexpectedValueException('Only allowed value type Option'); |
66
|
|
|
} |
67
|
|
|
|
68
|
9 |
|
if (null !== $index) { |
69
|
1 |
|
$this->options[$index] = $value; |
70
|
1 |
|
} else { |
71
|
9 |
|
$this->options[] = $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
9 |
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param mixed $index |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
1 |
|
public function offsetUnset($index) |
82
|
|
|
{ |
83
|
1 |
|
unset($this->options[$index]); |
84
|
|
|
|
85
|
1 |
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
9 |
|
public function count() |
92
|
|
|
{ |
93
|
9 |
|
return count($this->options); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
8 |
|
public function getValueOptions() |
100
|
|
|
{ |
101
|
8 |
|
if (empty($this->options)) { |
102
|
1 |
|
return Option::JSON_NONE; |
103
|
|
|
} |
104
|
|
|
|
105
|
7 |
|
if (1 == $this->count()) { |
106
|
|
|
/** @var Option $option */ |
107
|
6 |
|
$option = $this->options[0]; |
108
|
6 |
|
return $option->getValue(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** @var Option $firstOption */ |
112
|
1 |
|
$firstOption = $this->options[0]; |
113
|
1 |
|
$bitwiseResult = $firstOption->getValue(); |
114
|
1 |
|
$options = array_slice($this->options, 1); |
115
|
|
|
|
116
|
|
|
/** @var Option $option */ |
117
|
1 |
|
foreach ($options as $option) { |
118
|
1 |
|
if (Option::JSON_NONE === $option->getValue()) { |
119
|
1 |
|
continue; |
120
|
|
|
} |
121
|
1 |
|
$bitwiseResult = $bitwiseResult | $option->getValue(); |
122
|
1 |
|
} |
123
|
|
|
|
124
|
1 |
|
return $bitwiseResult; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|