diff --git a/.travis.yml b/.travis.yml
index a7c1f90..87c9a52 100755
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,9 +4,13 @@ php:
- 5.3
- 5.4
- 5.5
+ - 5.6
+ - hhvm
before_script:
- - wget http://getcomposer.org/composer.phar
- - php composer.phar install
+ - composer self-update
+ - composer install --no-interaction --dev
-script: phpunit --configuration phpunit.xml tests
+script:
+ - vendor/bin/phpunit
+ - vendor/bin/phpcs --standard=PSR2 ./src/
diff --git a/README.md b/README.md
index fe0efdf..b9b9f6f 100755
--- a/README.md
+++ b/README.md
@@ -56,7 +56,12 @@ function setAction(Action $action) {
- `__construct()` The constructor checks that the value exist in the enum
- `__toString()` You can `echo $myValue`, it will display the enum value (value of the constant)
- `getValue()` Returns the current value of the enum
-- `toArray()` (static) Returns an array of all possible values (constant name in key, constant value in value)
+- `getKey()` Returns the key of the current value on Enum
+- `keys()` (@static) Returns the names (keys) of all constants in the Enum class
+- `toArray()` (@static) method Returns all possible values as an array (constant name in key, constant value in value)
+- `isValid()` (@static) Check if tested value is valid on enum set
+- `isValidKey()` (@static) Check if tested key is valid on enum set
+- `search()` Return key for searched value
### Static methods
diff --git a/composer.json b/composer.json
index 9f4b589..0a925ba 100755
--- a/composer.json
+++ b/composer.json
@@ -5,9 +5,22 @@
"keywords": ["enum"],
"homepage": "http://github.com/myclabs/php-enum",
"license": "MIT",
+ "authors": [
+ {
+ "name": "PHP Enum contributors",
+ "homepage": "https://github.com/myclabs/php-enum/graphs/contributors"
+ }
+ ],
"autoload": {
- "psr-0": {
+ "psr-4": {
"MyCLabs\\Enum\\": "src/"
}
+ },
+ "require": {
+ "php": ">=5.3"
+ },
+ "require-dev": {
+ "phpunit/phpunit": "4.*",
+ "squizlabs/php_codesniffer": "1.*"
}
}
diff --git a/phpunit.xml b/phpunit.xml
index 387cab7..af5293b 100755
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -12,11 +12,9 @@
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./vendor/autoload.php">
-
-
- ./tests
+
+ ./tests
-
diff --git a/src/MyCLabs/Enum/Enum.php b/src/Enum.php
similarity index 55%
rename from src/MyCLabs/Enum/Enum.php
rename to src/Enum.php
index 5e6b148..939606c 100755
--- a/src/MyCLabs/Enum/Enum.php
+++ b/src/Enum.php
@@ -11,33 +11,38 @@
*
* Create an enum by implementing this class and adding class constants.
*
+ * @package MyCLabs\Enum
* @author Matthieu Napoli
*/
abstract class Enum
{
/**
* Enum value
+ *
* @var mixed
*/
protected $value;
/**
* Store existing constants in a static cache per object.
+ *
* @var array
*/
- private static $constantsCache = array();
+ private static $cache = array();
/**
* Creates a new value of some type
+ *
* @param mixed $value
+ *
* @throws \UnexpectedValueException if incompatible type is given.
*/
public function __construct($value)
{
- $possibleValues = self::toArray();
- if (! in_array($value, $possibleValues)) {
+ if (!in_array($value, self::toArray())) {
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
}
+
$this->value = $value;
}
@@ -49,6 +54,16 @@ public function getValue()
return $this->value;
}
+ /**
+ * Returns the key of the current value on Enum
+ *
+ * @return mixed
+ */
+ public function getKey()
+ {
+ return self::search($this->value);
+ }
+
/**
* @return string
*/
@@ -57,24 +72,80 @@ public function __toString()
return (string) $this->value;
}
+ /**
+ * Returns the names (keys) of all constants in the Enum class
+ *
+ * @return array
+ */
+ public static function keys()
+ {
+ return array_keys(static::toArray());
+ }
+
/**
* Returns all possible values as an array
+ *
* @return array Constant name in key, constant value in value
*/
public static function toArray()
{
- $calledClass = get_called_class();
- if(!array_key_exists($calledClass, self::$constantsCache)) {
- $reflection = new \ReflectionClass($calledClass);
- self::$constantsCache[$calledClass] = $reflection->getConstants();
+ $class = get_called_class();
+ if (!array_key_exists($class, self::$cache)) {
+ $reflection = new \ReflectionClass($class);
+ self::$cache[$class] = $reflection->getConstants();
}
- return self::$constantsCache[$calledClass];
+
+ return self::$cache[$class];
+ }
+
+ /**
+ * Check if is valid enum value
+ *
+ * @static
+ *
+ * @param $value
+ *
+ * @return bool
+ */
+ public static function isValid($value)
+ {
+ return in_array($value, self::toArray());
+ }
+
+ /**
+ * Check if is valid enum key
+ *
+ * @static
+ *
+ * @param $key
+ *
+ * @return bool
+ */
+ public static function isValidKey($key)
+ {
+ return in_array($key, self::keys());
+ }
+
+ /**
+ * Return key for value
+ *
+ * @static
+ *
+ * @param $value
+ *
+ * @return mixed
+ */
+ public static function search($value)
+ {
+ return array_search($value, array_combine(self::keys(), self::toArray()));
}
/**
* Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
+ *
* @param string $name
* @param array $arguments
+ *
* @return static
* @throws \BadMethodCallException
*/
@@ -83,6 +154,7 @@ public static function __callStatic($name, $arguments)
if (defined("static::$name")) {
return new static(constant("static::$name"));
}
+
throw new \BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class());
}
}
diff --git a/tests/EnumFixture.php b/tests/EnumFixture.php
new file mode 100644
index 0000000..24b60d1
--- /dev/null
+++ b/tests/EnumFixture.php
@@ -0,0 +1,23 @@
+
*/
class EnumTest extends \PHPUnit_Framework_TestCase
@@ -30,10 +29,20 @@ public function testGetValue()
$this->assertEquals(EnumFixture::NUMBER, $value->getValue());
}
+ /**
+ * getKey()
+ */
+ public function testGetKey()
+ {
+ $value = new EnumFixture(EnumFixture::FOO);
+ $this->assertEquals('FOO', $value->getKey());
+ $this->assertNotEquals('BA', $value->getKey());
+ }
+
/**
* @expectedException \UnexpectedValueException
*/
- public function testInvalidValue1()
+ public function testInvalidValueString()
{
new EnumFixture("test");
}
@@ -41,7 +50,7 @@ public function testInvalidValue1()
/**
* @expectedException \UnexpectedValueException
*/
- public function testInvalidValue2()
+ public function testInvalidValueInt()
{
new EnumFixture(1234);
}
@@ -49,7 +58,7 @@ public function testInvalidValue2()
/**
* @expectedException \UnexpectedValueException
*/
- public function testInvalidValue3()
+ public function testInvalidValueEmpty()
{
new EnumFixture(null);
}
@@ -69,6 +78,21 @@ public function testToString()
$this->assertEquals((string) EnumFixture::NUMBER, (string) $value);
}
+ /**
+ * keys()
+ */
+ public function testKeys()
+ {
+ $values = EnumFixture::keys();
+ $this->assertInternalType("array", $values);
+ $expectedValues = array(
+ "FOO",
+ "BAR",
+ "NUMBER",
+ );
+ $this->assertEquals($expectedValues, $values);
+ }
+
/**
* toArray()
*/
@@ -96,24 +120,38 @@ public function testStaticAccess()
/**
* @expectedException \BadMethodCallException
- * @expectedExceptionMessage No static method or enum constant 'UNKNOWN' in class UnitTest\MyCLabs\Enum\Enum\EnumFixture
+ * @expectedExceptionMessage No static method or enum constant 'UNKNOWN' in class
+ * UnitTest\MyCLabs\Enum\Enum\EnumFixture
*/
public function testBadStaticAccess()
{
EnumFixture::UNKNOWN();
}
-}
-/**
- * Fixture class
- *
- * @method static EnumFixture FOO()
- * @method static EnumFixture BAR()
- * @method static EnumFixture NUMBER()
- */
-class EnumFixture extends Enum
-{
- const FOO = "foo";
- const BAR = "bar";
- const NUMBER = 42;
+ /**
+ * isValid()
+ */
+ public function testIsValid()
+ {
+ $this->assertTrue(EnumFixture::isValid('foo'));
+ $this->assertFalse(EnumFixture::isValid('baz'));
+ }
+
+ /**
+ * ssValidKey()
+ */
+ public function testIsValidKey()
+ {
+ $this->assertTrue(EnumFixture::isValidKey('FOO'));
+ $this->assertFalse(EnumFixture::isValidKey('BAZ'));
+ }
+
+ /**
+ * search()
+ */
+ public function testSearch()
+ {
+ $this->assertEquals('FOO', EnumFixture::search('foo'));
+ $this->assertNotEquals('FOO', EnumFixture::isValidKey('baz'));
+ }
}