Skip to content

Commit 527b4be

Browse files
jeremykendallmnapoli
authored andcommitted
Adds Enum::equals()
Adds equals method to ease comparison between instances.
1 parent d7e8e72 commit 527b4be

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/Enum.php

+10
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ public function __toString()
7373
return (string)$this->value;
7474
}
7575

76+
/**
77+
* Compares one Enum with another.
78+
*
79+
* @return bool True if Enums are equal, false if not equal
80+
*/
81+
public function equals(Enum $enum)
82+
{
83+
return $this->getValue() === $enum->getValue();
84+
}
85+
7686
/**
7787
* Returns the names (keys) of all constants in the Enum class
7888
*

tests/EnumTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,13 @@ public function searchProvider() {
213213
array(array(), false),
214214
);
215215
}
216+
217+
public function testEquals()
218+
{
219+
$enum = new EnumFixture(EnumFixture::FOO);
220+
$this->assertTrue($enum->equals(EnumFixture::FOO()));
221+
222+
$enum = new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE);
223+
$this->assertFalse($enum->equals(EnumFixture::PROBLEMATIC_EMPTY_STRING()));
224+
}
216225
}

0 commit comments

Comments
 (0)