|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. |
|
4
|
|
|
* |
|
5
|
|
|
* You are hereby granted a non-exclusive, worldwide, royalty-free license to |
|
6
|
|
|
* use, copy, modify, and distribute this software in source code or binary |
|
7
|
|
|
* form for use in connection with the web services and APIs provided by |
|
8
|
|
|
* Facebook. |
|
9
|
|
|
* |
|
10
|
|
|
* As with any software that integrates with the Facebook platform, your use |
|
11
|
|
|
* of this software is subject to the Facebook Developer Principles and |
|
12
|
|
|
* Policies [http://developers.facebook.com/policy/]. This copyright notice |
|
13
|
|
|
* shall be included in all copies or substantial portions of the software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
|
18
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
20
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|
21
|
|
|
* DEALINGS IN THE SOFTWARE. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace FacebookAds\Enum; |
|
26
|
|
|
|
|
27
|
|
|
abstract class AbstractEnum implements EnumInstanceInterface { |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array|null |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $map = null; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array|null |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $names = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array|null |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $values = null; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array|null |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $valuesMap = null; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var AbstractEnum[] |
|
51
|
|
|
*/ |
|
52
|
|
|
protected static $instances = array(); |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
2 |
|
static function className() { |
|
|
|
|
|
|
58
|
2 |
|
return get_called_class(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return AbstractEnum |
|
63
|
|
|
*/ |
|
64
|
30 |
|
public static function getInstance() { |
|
65
|
30 |
|
$fqn = get_called_class(); |
|
66
|
30 |
|
if (!array_key_exists($fqn, static::$instances)) { |
|
67
|
2 |
|
static::$instances[$fqn] = new static(); |
|
68
|
2 |
|
} |
|
69
|
|
|
|
|
70
|
30 |
|
return static::$instances[$fqn]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
4 |
|
public function getArrayCopy() { |
|
77
|
4 |
|
if ($this->map === null) { |
|
78
|
1 |
|
$this->map = (new \ReflectionClass(get_called_class())) |
|
79
|
1 |
|
->getConstants(); |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
4 |
|
return $this->map; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
2 |
|
public function getNames() { |
|
89
|
2 |
|
if ($this->names === null) { |
|
90
|
1 |
|
$this->names = array_keys($this->getArrayCopy()); |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
return $this->names; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function getValues() { |
|
100
|
2 |
|
if ($this->values === null) { |
|
101
|
1 |
|
$this->values = array_values($this->getArrayCopy()); |
|
102
|
1 |
|
} |
|
103
|
|
|
|
|
104
|
2 |
|
return $this->values; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
4 |
|
public function getValuesMap() { |
|
111
|
4 |
|
if ($this->valuesMap === null) { |
|
112
|
1 |
|
$this->valuesMap = array_fill_keys($this->getValues(), null); |
|
113
|
1 |
|
} |
|
114
|
|
|
|
|
115
|
4 |
|
return $this->valuesMap; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string|int|float $name |
|
120
|
|
|
* @return mixed |
|
121
|
|
|
*/ |
|
122
|
2 |
|
public function getValueForName($name) { |
|
123
|
2 |
|
$copy = $this->getArrayCopy(); |
|
124
|
2 |
|
return array_key_exists($name, $copy) |
|
125
|
2 |
|
? $copy[$name] |
|
126
|
2 |
|
: null; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string|int|float $name |
|
131
|
|
|
* @return mixed |
|
132
|
|
|
* @throws \InvalidArgumentException |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function assureValueForName($name) { |
|
135
|
1 |
|
$value = $this->getValueForName($name); |
|
136
|
1 |
|
if ($value === null) { |
|
137
|
1 |
|
throw new \InvalidArgumentException( |
|
138
|
1 |
|
'Unknown name "'.$name.'" in '.static::className()); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
1 |
|
return $value; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param string|int|float $name |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
1 |
|
public function isValid($name) { |
|
149
|
1 |
|
return array_key_exists($name, $this->getArrayCopy()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param string|int|float $name |
|
154
|
|
|
* @throws \InvalidArgumentException |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public function assureIsValid($name) { |
|
157
|
1 |
|
if (!array_key_exists($name, $this->getArrayCopy())) { |
|
158
|
1 |
|
throw new \InvalidArgumentException( |
|
159
|
1 |
|
'Unknown name "'.$name.'" in '.static::className()); |
|
160
|
|
|
} |
|
161
|
1 |
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param string|int|float $value |
|
165
|
|
|
* @return bool |
|
166
|
|
|
*/ |
|
167
|
2 |
|
public function isValidValue($value) { |
|
168
|
2 |
|
return array_key_exists($value, $this->getValuesMap()); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param mixed $value |
|
173
|
|
|
* @throws \InvalidArgumentException |
|
174
|
|
|
*/ |
|
175
|
1 |
|
public function assureIsValidValue($value) { |
|
176
|
1 |
|
if (!$this->isValidValue($value)) { |
|
177
|
1 |
|
throw new \InvalidArgumentException( |
|
178
|
1 |
|
'"'.$value.'", not a valid value in '.static::className()); |
|
179
|
|
|
} |
|
180
|
1 |
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.