diff --git a/README.md b/README.md index 2082dec..38183c5 100755 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ Static methods: - `toArray()` method Returns all possible values as an array (constant name in key, constant value in value) - `keys()` Returns the names (keys) of all constants in the Enum class +- `values()` Returns instances of the Enum class of all Enum constants (constant name in key, Enum instance in value) - `isValid()` Check if tested value is valid on enum set - `isValidKey()` Check if tested key is valid on enum set - `search()` Return key for searched value diff --git a/src/Enum.php b/src/Enum.php index deac0ed..f44fdbd 100755 --- a/src/Enum.php +++ b/src/Enum.php @@ -83,6 +83,22 @@ public static function keys() return array_keys(self::toArray()); } + /** + * Returns instances of the Enum class of all Enum constants + * + * @return array Constant name in key, Enum instance in value + */ + public static function values() + { + $values = array(); + + foreach (self::toArray() as $key => $value) { + $values[$key] = new static($value); + } + + return $values; + } + /** * Returns all possible values as an array * diff --git a/tests/EnumTest.php b/tests/EnumTest.php index 88b2c5f..5f16ee0 100755 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -98,6 +98,25 @@ public function testKeys() $this->assertSame($expectedValues, $values); } + /** + * values() + */ + public function testValues() + { + $values = EnumFixture::values(); + $expectedValues = array( + "FOO" => new EnumFixture(EnumFixture::FOO), + "BAR" => new EnumFixture(EnumFixture::BAR), + "NUMBER" => new EnumFixture(EnumFixture::NUMBER), + "PROBLEMATIC_NUMBER" => new EnumFixture(EnumFixture::PROBLEMATIC_NUMBER), + "PROBLEMATIC_NULL" => new EnumFixture(EnumFixture::PROBLEMATIC_NULL), + "PROBLEMATIC_EMPTY_STRING" => new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING), + "PROBLEMATIC_BOOLEAN_FALSE" => new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE), + ); + + $this->assertEquals($expectedValues, $values); + } + /** * toArray() */