Skip to content

Commit

Permalink
added __DEFAULT method
Browse files Browse the repository at this point in the history
  • Loading branch information
osorioramirez committed Jul 9, 2016
1 parent 0e4bc5d commit 7f10cd6
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 11 deletions.
61 changes: 61 additions & 0 deletions Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@
/**
* Enum class.
*
* @method static static __DEFAULT()
*
* @author Karel Osorio Ramírez <osorioramirez@gmail.com>
*/
abstract class Enum extends BaseEnum implements EquatableInterface
{
/**
* @var array
*/
protected static $defaultCache = array();

/**
* @param mixed $value
*
Expand All @@ -38,4 +45,58 @@ public function equals($other)
{
return \get_class($this) === \get_class($other) && $this->getValue() === $other->getValue();
}

/**
* @param string $name
* @param array $arguments
*
* @return static
*
* @throws \BadMethodCallException
*/
public static function __callStatic($name, $arguments)
{
$array = static::toArray();
if (\strtoupper($name) === '__DEFAULT' && isset(static::$defaultCache[static::class])) {
return new static(static::$defaultCache[static::class]);
}

if (isset($array[$name])) {
return new static($array[$name]);
}

throw new \BadMethodCallException("No static method or enum constant '$name' in class ".static::class);
}

/**
* @return array
*/
public static function toArray()
{
if (!isset(static::$cache[static::class])) {
static::$cache[static::class] = static::constants();
if (!isset(static::$defaultCache[static::class]) && !empty(static::$cache[static::class])) {
static::$defaultCache[static::class] = \array_values(static::$cache[static::class])[0];
}
}

return static::$cache[static::class];
}

/**
* @return array
*/
private static function constants()
{
$reflection = new \ReflectionClass(static::class);
$constants = $reflection->getConstants();
foreach ($constants as $name => $value) {
if (\strtoupper($name) === '__DEFAULT') {
static::$defaultCache[static::class] = $value;
unset($constants[$name]);
}
}

return $constants;
}
}
29 changes: 29 additions & 0 deletions Tests/Fixtures/BadDefaultEnumFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\Enum\Tests\Fixtures;

use Cubiche\Core\Enum\Enum;

/**
* Test Enum.
*
* @method static DefaultEnumFixture FOO()
* @method static DefaultEnumFixture BAR()
*
* @author Karel Osorio Ramírez <osorioramirez@gmail.com>
*/
final class BadDefaultEnumFixture extends Enum
{
const __DEFAULT = 'baz';

const FOO = 'foo';
const BAR = 'bar';
}
29 changes: 29 additions & 0 deletions Tests/Fixtures/DefaultEnumFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\Enum\Tests\Fixtures;

use Cubiche\Core\Enum\Enum;

/**
* Test Enum.
*
* @method static DefaultEnumFixture FOO()
* @method static DefaultEnumFixture BAR()
*
* @author Karel Osorio Ramírez <osorioramirez@gmail.com>
*/
final class DefaultEnumFixture extends Enum
{
const __DEFAULT = self::BAR;

const FOO = 'foo';
const BAR = 'bar';
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,20 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\Enum\Tests\Units\Fixtures;
namespace Cubiche\Core\Enum\Tests\Fixtures;

use Cubiche\Core\Enum\Enum;

/**
* Test Enum.
*
* @method static EnumFixture FOO()
* @method static EnumFixture BAR()
*
* @author Karel Osorio Ramírez <osorioramirez@gmail.com>
*/
final class EnumFixture extends Enum
{
const FOO = 'foo';
const BAR = 'bar';
const BAZ = 'baz';
const _DO = 'do';
const RE = 're';
const MI = 'mi';
const FA = 'fa';
const SOL = 'sol';
const LA = 'la';
const SI = 'si';
}
39 changes: 37 additions & 2 deletions Tests/Units/EnumTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cubiche\Core\Enum\Tests\Units;

use Cubiche\Core\Enum\Tests\Units\Fixtures\EnumFixture;
use Cubiche\Core\Enum\Tests\Fixtures\EnumFixture;
use Cubiche\Core\Enum\Tests\Fixtures\DefaultEnumFixture;
use Cubiche\Core\Enum\Tests\Fixtures\BadDefaultEnumFixture;

/**
* Enum Tests Class.
Expand Down Expand Up @@ -42,4 +43,38 @@ public function testIs()
->isFalse()
;
}

/**
* Test __DEFAULT method.
*/
public function testDefault()
{
$this
->when($default = EnumFixture::__DEFAULT())
->then()
->object($default)
->isEqualTo(EnumFixture::FOO())
;

$this
->when($default = DefaultEnumFixture::__DEFAULT())
->then()
->object($default)
->isEqualTo(DefaultEnumFixture::BAR())
;

$this
->exception(function () {
BadDefaultEnumFixture::__DEFAULT();
})
->isInstanceof(\UnexpectedValueException::class)
;

$this
->exception(function () {
BadDefaultEnumFixture::BAZ();
})
->isInstanceof(\BadMethodCallException::class)
;
}
}

0 comments on commit 7f10cd6

Please sign in to comment.