This repository was archived by the owner on Dec 26, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -56,7 +56,7 @@ protected function __construct($memberName)
56
56
*
57
57
* @param string $memberName enum key
58
58
* @param array $args ignored
59
- * @return mixed
59
+ * @return self
60
60
*/
61
61
public static function __callStatic ($ memberName , array $ args )
62
62
{
@@ -155,4 +155,25 @@ private function get($memberName)
155
155
156
156
return self ::$ members [get_called_class ()][$ memberName ];
157
157
}
158
+
159
+ /**
160
+ * @param mixed $value
161
+ * @return self
162
+ * @throws EnumException
163
+ */
164
+ public static function findByValue ($ value )
165
+ {
166
+ foreach (self ::getMembers () as $ n => $ v ) {
167
+ if ($ value === $ v ) {
168
+ return self ::__callStatic ($ n , []);
169
+ }
170
+ }
171
+ throw new EnumException (
172
+ sprintf (
173
+ "Value not found in %s " ,
174
+ get_called_class ()
175
+ ),
176
+ EnumException::OUT_OF_RANGE
177
+ );
178
+ }
158
179
}
Original file line number Diff line number Diff line change @@ -78,4 +78,26 @@ public function testGetMembers()
78
78
$ this ->assertArrayHasKey ("MY_FALSE " , $ members );
79
79
$ this ->assertEquals (2 , $ members ["MY_FALSE " ]);
80
80
}
81
+
82
+ /**
83
+ * @dataProvider dataFindByValue
84
+ * @param mixed $value
85
+ * @param mixed $expected
86
+ */
87
+ public function testFindByValue ($ value , $ expected )
88
+ {
89
+ if ($ expected instanceof \Exception) {
90
+ $ this ->setExpectedException (get_class ($ expected ));
91
+ }
92
+ $ this ->assertSame ($ expected , MyBoolean::findByValue ($ value ));
93
+ }
94
+
95
+ public function dataFindByValue ()
96
+ {
97
+ return [
98
+ [1 , MyBoolean::MY_TRUE ()],
99
+ [2 , MyBoolean::MY_FALSE ()],
100
+ [3 , new EnumException ()]
101
+ ];
102
+ }
81
103
}
You can’t perform that action at this time.
0 commit comments