Skip to content

Commit

Permalink
Remove the commerceguys/enum dependency. Fixes #98.
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Feb 16, 2018
1 parent 65720d7 commit f257d08
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 8 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"license": "MIT",
"require": {
"php": ">=5.5.0",
"doctrine/collections": "~1.0",
"commerceguys/enum": "~1.0"
"doctrine/collections": "~1.0"
},
"require-dev": {
"symfony/validator": ">=3.2",
Expand Down
87 changes: 87 additions & 0 deletions src/AbstractEnum.php
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);
}
}
}
2 changes: 1 addition & 1 deletion src/AddressFormat/AddressField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CommerceGuys\Addressing\AddressFormat;

use CommerceGuys\Enum\AbstractEnum;
use CommerceGuys\Addressing\AbstractEnum;

/**
* Enumerates available address fields.
Expand Down
2 changes: 1 addition & 1 deletion src/AddressFormat/AdministrativeAreaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CommerceGuys\Addressing\AddressFormat;

use CommerceGuys\Enum\AbstractEnum;
use CommerceGuys\Addressing\AbstractEnum;

/**
* Enumerates available administrative area types.
Expand Down
2 changes: 1 addition & 1 deletion src/AddressFormat/DependentLocalityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CommerceGuys\Addressing\AddressFormat;

use CommerceGuys\Enum\AbstractEnum;
use CommerceGuys\Addressing\AbstractEnum;

/**
* Enumerates available dependent locality types.
Expand Down
2 changes: 1 addition & 1 deletion src/AddressFormat/LocalityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CommerceGuys\Addressing\AddressFormat;

use CommerceGuys\Enum\AbstractEnum;
use CommerceGuys\Addressing\AbstractEnum;

/**
* Enumerates available locality types.
Expand Down
2 changes: 1 addition & 1 deletion src/AddressFormat/PostalCodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CommerceGuys\Addressing\AddressFormat;

use CommerceGuys\Enum\AbstractEnum;
use CommerceGuys\Addressing\AbstractEnum;

/**
* Enumerates available postal code types.
Expand Down
2 changes: 1 addition & 1 deletion src/Subdivision/PatternType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CommerceGuys\Addressing\Subdivision;

use CommerceGuys\Enum\AbstractEnum;
use CommerceGuys\Addressing\AbstractEnum;

/**
* Enumerates available pattern types.
Expand Down
67 changes: 67 additions & 0 deletions tests/AbstractEnumTest.php
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']);
}
}

0 comments on commit f257d08

Please sign in to comment.