Skip to content

Added new methods and changed source structure to be PSR4 compatible #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 14 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"
}
}
6 changes: 2 additions & 4 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./vendor/autoload.php">

<testsuites>
<testsuite name="Enum Test Suite">
<directory>./tests</directory>
<testsuite name="PHP Enum Test Suite">
<directory suffix=".php">./tests</directory>
</testsuite>
</testsuites>

</phpunit>
88 changes: 80 additions & 8 deletions src/MyCLabs/Enum/Enum.php → src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,38 @@
*
* Create an enum by implementing this class and adding class constants.
*
* @package MyCLabs\Enum
* @author Matthieu Napoli <[email protected]>
*/
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;
}

Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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());
}
}
23 changes: 23 additions & 0 deletions tests/EnumFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* @link http://github.com/myclabs/php-enum
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
*/

namespace MyCLabs\Enum;

/**
* Class EnumFixture
*
* @package MyCLabs\Enum
* @method static EnumFixture FOO()
* @method static EnumFixture BAR()
* @method static EnumFixture NUMBER()
* @author Daniel Costa <[email protected]
*/
class EnumFixture extends Enum
{
const FOO = "foo";
const BAR = "bar";
const NUMBER = 42;
}
78 changes: 58 additions & 20 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
*/

namespace UnitTest\MyCLabs\Enum\Enum;

use MyCLabs\Enum\Enum;
namespace MyCLabs\Enum;

/**
* Enum test
*
* @package MyCLabs\Enum
* @author Matthieu Napoli <[email protected]>
*/
class EnumTest extends \PHPUnit_Framework_TestCase
Expand All @@ -30,26 +29,36 @@ 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");
}

/**
* @expectedException \UnexpectedValueException
*/
public function testInvalidValue2()
public function testInvalidValueInt()
{
new EnumFixture(1234);
}

/**
* @expectedException \UnexpectedValueException
*/
public function testInvalidValue3()
public function testInvalidValueEmpty()
{
new EnumFixture(null);
}
Expand All @@ -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()
*/
Expand Down Expand Up @@ -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'));
}
}