-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
162 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Utility\Object; | ||
|
||
use Utility\Exception\NonStaticCallException; | ||
|
||
/** | ||
* Class Dictionary | ||
* | ||
* @category Object | ||
* @package Utility\Object | ||
* @author Alexandr Ivanov <misantron@gmail.com> | ||
* @license https://github.com/misantron/php-utils/blob/master/LICENSE (MIT License) | ||
* @link https://github.com/misantron/php-utils/blob/master/src/UTime.php | ||
*/ | ||
abstract class Dictionary | ||
{ | ||
/** @var array */ | ||
protected static $titleMapping = []; | ||
|
||
/** @var array|null */ | ||
protected static $cache; | ||
|
||
/** | ||
* @throws NonStaticCallException | ||
*/ | ||
function __construct() | ||
{ | ||
throw new NonStaticCallException('Non static call is disabled.'); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
final public static function getKeys() | ||
{ | ||
if(static::$cache === null){ | ||
$class = new \ReflectionClass(get_called_class()); | ||
static::$cache = array_values($class->getConstants()); | ||
} | ||
return static::$cache; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
final public static function getTitles() | ||
{ | ||
return static::$titleMapping; | ||
} | ||
|
||
/** | ||
* @param string|int $key | ||
* @return string | ||
*/ | ||
public static function getTitle($key) | ||
{ | ||
if(!isset(static::$titleMapping[$key])){ | ||
throw new \InvalidArgumentException('Element with key "' . (string)$key . '" not found.'); | ||
} | ||
return static::$titleMapping[$key]; | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace Utility\Tests\Fixture; | ||
|
||
use Utility\Object\Dictionary; | ||
|
||
class UserRoleDictionary extends Dictionary | ||
{ | ||
const | ||
ROLE_USER = 1, | ||
ROLE_MODERATOR = 2, | ||
ROLE_ADMIN = 3 | ||
; | ||
|
||
protected static $titleMapping = [ | ||
self::ROLE_USER => 'User', | ||
self::ROLE_MODERATOR => 'Moderator', | ||
self::ROLE_ADMIN => 'Admin' | ||
]; | ||
} |
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,75 @@ | ||
<?php | ||
|
||
namespace Utility\Tests\Object; | ||
|
||
use Utility\Exception\NonStaticCallException; | ||
use Utility\Tests\Fixture\UserRoleDictionary; | ||
use Utility\Tests\TestCase; | ||
|
||
class DictionaryTest extends TestCase | ||
{ | ||
public function testConstructor() | ||
{ | ||
try { | ||
new UserRoleDictionary(); | ||
$this->fail('Expected exception not thrown'); | ||
} catch(NonStaticCallException $e){ | ||
$this->assertInstanceOf('\\Utility\\Exception\\NonStaticCallException', $e); | ||
$this->assertEquals('Non static call is disabled.', $e->getMessage()); | ||
} | ||
} | ||
|
||
public function testGetKeys() | ||
{ | ||
$actual = UserRoleDictionary::getKeys(); | ||
|
||
$this->assertInternalType('array', $actual); | ||
$this->assertContains(UserRoleDictionary::ROLE_USER, $actual); | ||
$this->assertContains(UserRoleDictionary::ROLE_MODERATOR, $actual); | ||
$this->assertContains(UserRoleDictionary::ROLE_ADMIN, $actual); | ||
$this->assertCount(3, $actual); | ||
} | ||
|
||
public function testGetTitles() | ||
{ | ||
$actual = UserRoleDictionary::getTitles(); | ||
|
||
$this->assertInternalType('array', $actual); | ||
$this->assertArrayHasKey(UserRoleDictionary::ROLE_USER, $actual); | ||
$this->assertArrayHasKey(UserRoleDictionary::ROLE_MODERATOR, $actual); | ||
$this->assertArrayHasKey(UserRoleDictionary::ROLE_ADMIN, $actual); | ||
$this->assertEquals(3, sizeof($actual)); | ||
|
||
$this->assertEquals('User', $actual[UserRoleDictionary::ROLE_USER]); | ||
$this->assertEquals('Moderator', $actual[UserRoleDictionary::ROLE_MODERATOR]); | ||
$this->assertEquals('Admin', $actual[UserRoleDictionary::ROLE_ADMIN]); | ||
} | ||
|
||
public function testGetTitle() | ||
{ | ||
try { | ||
UserRoleDictionary::getTitle('asd'); | ||
$this->fail('Expected exception not thrown'); | ||
} catch(\InvalidArgumentException $e){ | ||
$this->assertInstanceOf('\\InvalidArgumentException', $e); | ||
$this->assertEquals('Element with key "asd" not found.', $e->getMessage()); | ||
} | ||
|
||
try { | ||
UserRoleDictionary::getTitle(null); | ||
$this->fail('Expected exception not thrown'); | ||
} catch(\InvalidArgumentException $e){ | ||
$this->assertInstanceOf('\\InvalidArgumentException', $e); | ||
$this->assertEquals('Element with key "" not found.', $e->getMessage()); | ||
} | ||
|
||
$actual = UserRoleDictionary::getTitle(UserRoleDictionary::ROLE_USER); | ||
$this->assertEquals('User', $actual); | ||
|
||
$actual = UserRoleDictionary::getTitle(UserRoleDictionary::ROLE_MODERATOR); | ||
$this->assertEquals('Moderator', $actual); | ||
|
||
$actual = UserRoleDictionary::getTitle(UserRoleDictionary::ROLE_ADMIN); | ||
$this->assertEquals('Admin', $actual); | ||
} | ||
} |