-
Notifications
You must be signed in to change notification settings - Fork 2
/
Set.php
169 lines (141 loc) · 3.91 KB
/
Set.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/*
* Copyright (c) 2015 Mihai Stancu <[email protected]>
*
* This source file is subject to the license that is bundled with this source
* code in the LICENSE.md file.
*/
namespace MS\DoctrineTypes;
use Doctrine\DBAL\Exception\InvalidArgumentException;
class Set extends Enum
{
/**
* @param string $name
* @param string|int $value
* @param int $position
*
* @return array
*/
protected static function processValue($name, $value, $position)
{
return array($value, pow(2, $position));
}
/**
* @param bool $asInteger
*
* @return array|string[]|int
*/
public function get($asInteger = false)
{
$values = (array) $this->value;
if ($asInteger) {
$result = 0;
foreach ($values as $value) {
$result += array_search($value, static::getValues());
}
return $result;
}
return $values;
}
/**
* @param array|string|int $values,...
*
* @throws InvalidArgumentException
*/
public function set($values = null)
{
if (func_num_args() > 1) {
$this->set(func_num_args());
return;
}
if (null === $values || '' === $values || 0 === $values || array() === $values) {
$this->value = null;
return;
}
if ($values instanceof self) {
$this->set($values->get());
}
if (is_array($values) && array_filter($values, 'is_int') === $values) {
$this->value = $this->parseInteger(array_sum($values));
return;
}
if (is_int($values)) {
$this->value = $this->parseInteger($values);
return;
}
if (is_array($values) && $values === array_filter($values, 'is_string')) {
$this->value = $this->parseString(implode(',', $values));
return;
}
if (is_string($values)) {
$this->value = $this->parseString($values);
return;
}
}
/**
* @param int $values
*
* @throws InvalidArgumentException
*
* @return array
*/
private function parseInteger($values)
{
$newValues = array();
$total = 0;
foreach (static::getValues() as $integer => $string) {
if ($integer & $values) {
$newValues[$integer] = static::$values[static::class][$integer];
$total += $integer;
}
}
if ($total != $values) {
throw new InvalidArgumentException(
vsprintf(
'Values "%1$s" are not in the list of allowed values: "%2$s".',
array(
$values,
implode('", "', array_keys(static::getValues())),
)
)
);
}
return $newValues;
}
/**
* @param string $value
*
* @throws InvalidArgumentException
*
* @return array
*/
private function parseString($value)
{
$values = explode(',', $value);
$newValues = array();
foreach ($values as $value) {
$index = array_search($value, static::getValues());
if ($index !== false) {
$newValues[$index] = $value;
} else {
throw new InvalidArgumentException(
vsprintf(
'Value "%1$s" is not in the list of allowed values: "%2$s".',
array(
$value,
implode('", "', static::getValues()),
)
)
);
}
}
return $newValues;
}
/**
* @return string
*/
public function __toString()
{
return implode(', ', (array) $this->value);
}
}