Skip to content

Commit ab50cb7

Browse files
committed
Added compare function 'is'
1 parent 41f3e2f commit ab50cb7

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/MabeEnum/Enum.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ final public function getOrdinal()
120120
return $ordinal;
121121
}
122122

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+
123134
/**
124135
* Get an enum of the given value
125136
*

tests/MabeEnumTest/EnumTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,21 @@ public function testGetAllValues()
132132
}
133133
}
134134

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+
135150
public function testCallingGetOrdinalTwoTimesWillResultTheSameValue()
136151
{
137152
$enum = EnumBasic::get(EnumBasic::TWO);

0 commit comments

Comments
 (0)