Skip to content

Commit

Permalink
Dictionary object class added
Browse files Browse the repository at this point in the history
  • Loading branch information
misantron committed Jun 30, 2015
1 parent f95bafd commit 14f24f7
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 4 deletions.
63 changes: 63 additions & 0 deletions src/Object/Dictionary.php
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];
}
}
2 changes: 1 addition & 1 deletion src/UAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @category Utility
* @package Utility
* @author Alexandr Ivanov <misantron@gmail.com>
* @license MIT https://github.com/misantron/php-utils/blob/master/LICENSE
* @license https://github.com/misantron/php-utils/blob/master/LICENSE (MIT License)
* @link https://github.com/misantron/php-utils/blob/master/src/UTime.php
*/
class UAbstract
Expand Down
2 changes: 1 addition & 1 deletion src/UArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @category Utility
* @package Utility
* @author Alexandr Ivanov <misantron@gmail.com>
* @license MIT https://github.com/misantron/php-utils/blob/master/LICENSE
* @license https://github.com/misantron/php-utils/blob/master/LICENSE (MIT License)
* @link https://github.com/misantron/php-utils/blob/master/src/UTime.php
*/
class UArray extends UAbstract
Expand Down
2 changes: 1 addition & 1 deletion src/UString.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @category Utility
* @package Utility
* @author Alexandr Ivanov <misantron@gmail.com>
* @license MIT https://github.com/misantron/php-utils/blob/master/LICENSE
* @license https://github.com/misantron/php-utils/blob/master/LICENSE (MIT License)
* @link https://github.com/misantron/php-utils/blob/master/src/UTime.php
*/
class UString extends UAbstract
Expand Down
2 changes: 1 addition & 1 deletion src/UTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @category Utility
* @package Utility
* @author Alexandr Ivanov <misantron@gmail.com>
* @license MIT https://github.com/misantron/php-utils/blob/master/LICENSE
* @license https://github.com/misantron/php-utils/blob/master/LICENSE (MIT License)
* @link https://github.com/misantron/php-utils/blob/master/src/UTime.php
*/
class UTime extends UAbstract
Expand Down
20 changes: 20 additions & 0 deletions tests/Fixture/UserRoleDictionary.php
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'
];
}
75 changes: 75 additions & 0 deletions tests/Object/DictionaryTest.php
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);
}
}

0 comments on commit 14f24f7

Please sign in to comment.