-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove the commerceguys/enum dependency. Fixes #98.
- Loading branch information
Showing
9 changed files
with
161 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace CommerceGuys\Addressing; | ||
|
||
/** | ||
* Base class for enumerations. | ||
*/ | ||
abstract class AbstractEnum | ||
{ | ||
/** | ||
* Static cache of available values, shared with all subclasses. | ||
* | ||
* @var array | ||
*/ | ||
protected static $values = []; | ||
|
||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* Gets all available values. | ||
* | ||
* @return array The available values, keyed by constant. | ||
*/ | ||
public static function getAll() | ||
{ | ||
$class = get_called_class(); | ||
if (!isset(static::$values[$class])) { | ||
$reflection = new \ReflectionClass($class); | ||
static::$values[$class] = $reflection->getConstants(); | ||
} | ||
|
||
return static::$values[$class]; | ||
} | ||
|
||
/** | ||
* Gets the key of the provided value. | ||
* | ||
* @param string $value The value. | ||
* | ||
* @return bool The key if found, false otherwise. | ||
*/ | ||
public static function getKey($value) | ||
{ | ||
return array_search($value, static::getAll(), true); | ||
} | ||
|
||
/** | ||
* Checks whether the provided value is defined. | ||
* | ||
* @param string $value The value. | ||
* | ||
* @return bool True if the value is defined, false otherwise. | ||
*/ | ||
public static function exists($value) | ||
{ | ||
return in_array($value, static::getAll(), true); | ||
} | ||
|
||
/** | ||
* Asserts that the provided value is defined. | ||
* | ||
* @param string $value The value. | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public static function assertExists($value) | ||
{ | ||
if (static::exists($value) == false) { | ||
$class = substr(strrchr(get_called_class(), '\\'), 1); | ||
throw new \InvalidArgumentException(sprintf('"%s" is not a valid %s value.', $value, $class)); | ||
} | ||
} | ||
|
||
/** | ||
* Asserts that all provided valus are defined. | ||
* | ||
* @param array $values The values. | ||
*/ | ||
public static function assertAllExist(array $values) | ||
{ | ||
foreach ($values as $value) { | ||
static::assertExists($value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace CommerceGuys\Addressing\Tests; | ||
|
||
use CommerceGuys\Addressing\Subdivision\PatternType; | ||
|
||
/** | ||
* @coversDefaultClass \CommerceGuys\Addressing\AbstractEnum | ||
*/ | ||
class AbstractEnumTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @covers ::getAll | ||
*/ | ||
public function testGetAll() | ||
{ | ||
$expectedValues = ['FULL' => 'full', 'START' => 'start']; | ||
$values = PatternType::getAll(); | ||
$this->assertEquals($expectedValues, $values); | ||
} | ||
|
||
/** | ||
* @covers ::getKey | ||
*/ | ||
public function testGetKey() | ||
{ | ||
$key = PatternType::getKey('full'); | ||
$this->assertEquals('FULL', $key); | ||
|
||
$key = PatternType::getKey('invalid'); | ||
$this->assertEquals(false, $key); | ||
} | ||
|
||
/** | ||
* @covers ::exists | ||
*/ | ||
public function testExists() | ||
{ | ||
$result = PatternType::exists('start'); | ||
$this->assertEquals(true, $result); | ||
|
||
$result = PatternType::exists('invalid'); | ||
$this->assertEquals(false, $result); | ||
} | ||
|
||
/** | ||
* @covers ::assertExists | ||
* | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage "invalid" is not a valid PatternType value. | ||
*/ | ||
public function testAssertExists() | ||
{ | ||
$result = PatternType::assertExists('invalid'); | ||
} | ||
|
||
/** | ||
* @covers ::assertAllExist | ||
* | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage "invalid" is not a valid PatternType value. | ||
*/ | ||
public function testAssertAllExist() | ||
{ | ||
$result = PatternType::assertAllExist(['start', 'invalid']); | ||
} | ||
} |