File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -120,6 +120,17 @@ final public function getOrdinal()
120
120
return $ ordinal ;
121
121
}
122
122
123
+ /**
124
+ * Compare this enum against another enum and check if it's the same value
125
+ *
126
+ * @param mixed $value
127
+ * @return boolean
128
+ */
129
+ final public function is ($ enum )
130
+ {
131
+ return $ this ->value === $ enum || ($ enum instanceof static && $ this ->value === $ enum ->getValue ());
132
+ }
133
+
123
134
/**
124
135
* Get an enum of the given value
125
136
*
Original file line number Diff line number Diff line change @@ -132,6 +132,21 @@ public function testGetAllValues()
132
132
}
133
133
}
134
134
135
+ public function testIsBasic ()
136
+ {
137
+ $ enum = EnumBasic::ONE ();
138
+
139
+ // by value
140
+ $ this ->assertTrue ($ enum ->is (EnumBasic::ONE )); // same
141
+ $ this ->assertFalse ($ enum ->is ('1 ' )); // wrong value by strict comparison
142
+
143
+ // by instance
144
+ $ this ->assertTrue ($ enum ->is (EnumBasic::ONE ())); // same
145
+ $ this ->assertFalse ($ enum ->is (EnumBasic::TWO ())); // not the same
146
+ $ this ->assertTrue ($ enum ->is (EnumInheritance::ONE ())); // same by extended instance
147
+ $ this ->assertFalse ($ enum ->is (EnumBasic2::ONE ())); // same value but different instance
148
+ }
149
+
135
150
public function testCallingGetOrdinalTwoTimesWillResultTheSameValue ()
136
151
{
137
152
$ enum = EnumBasic::get (EnumBasic::TWO );
You can’t perform that action at this time.
0 commit comments