FIX: fix multiple enumeration classes

Enumeration classes shared one static variable which caused sharing data. Main problem was with conflicts in names. This commit divides enumeration into "namespaces". Used namespace is return value from "get_called_class()" function.
This commit is contained in:
Michal Kváček
2016-01-26 23:19:42 +01:00
parent dc5821649a
commit bc2bdd38b9
4 changed files with 58 additions and 19 deletions
+28 -5
View File
@@ -7,6 +7,20 @@ abstract class BaseEnumeration
{
public static $items = [];
/**
* Init enumerations
*/
protected static function init()
{
if (!isset(self::$items[get_called_class()]) || empty(self::$items[get_called_class()])) {
$className = get_called_class();
if (method_exists($className, 'init')) {
$className::init();
}
}
}
/**
* Add new item into enumeration
*
@@ -19,12 +33,17 @@ abstract class BaseEnumeration
public static function push($key, $translation, $overwrite = false)
{
// item exists and we do not want to overwrite items
if (!$overwrite && isset(self::$items[$key])) {
throw new EnumerationExistsException($key);
if (!$overwrite && isset(self::$items[get_called_class()][$key])) {
throw new EnumerationExistsException(get_called_class() . ': ' . $key . ' already in ' . $translation);
}
// if given collection does not exists, create empty one
if (!isset(self::$items[get_called_class()])) {
self::$items[get_called_class()] = [];
}
// add to collection
self::$items[$key] = $translation;
self::$items[get_called_class()][$key] = $translation;
}
/**
@@ -34,7 +53,7 @@ abstract class BaseEnumeration
*/
public static function getCount()
{
return count(self::$items);
return count(self::getWithTranslations());
}
/**
@@ -47,7 +66,11 @@ abstract class BaseEnumeration
*/
public static function getWithTranslations()
{
return self::$items;
if (!isset(self::$items[get_called_class()]) || empty(self::$items[get_called_class()])) {
self::init();
}
return self::$items[get_called_class()];
}
/**
+6 -4
View File
@@ -3,8 +3,12 @@ namespace App\Enumerations;
use App\Models\CustomFieldType;
class CustomFieldTypes extends BaseEnumeration {
public static function getWithTranslations() {
class CustomFieldTypes extends BaseEnumeration
{
public static $items = [];
public static function init()
{
// load items from database
$types = CustomFieldType::all(['id', 'name']);
@@ -13,7 +17,5 @@ class CustomFieldTypes extends BaseEnumeration {
foreach ($types as $type) {
self::push($type->id, $type->name);
}
return parent::getWithTranslations();
}
}
+12 -5
View File
@@ -5,9 +5,16 @@ use App\Models\User;
class UserRoles extends BaseEnumeration
{
public static $items = [
User::ROLE_ADMIN => 'admin/userManagement.role_' . User::ROLE_ADMIN,
User::ROLE_EDITOR => 'admin/userManagement.role_' . User::ROLE_EDITOR,
User::ROLE_VISITOR => 'admin/userManagement.role_' . User::ROLE_VISITOR
];
public static function init()
{
$roles = [
User::ROLE_ADMIN => 'admin/userManagement.role_' . User::ROLE_ADMIN,
User::ROLE_EDITOR => 'admin/userManagement.role_' . User::ROLE_EDITOR,
User::ROLE_VISITOR => 'admin/userManagement.role_' . User::ROLE_VISITOR
];
foreach ($roles as $role => $translation) {
self::push($role, $translation);
}
}
}
+12 -5
View File
@@ -12,9 +12,16 @@ use App\Models\User;
class UserStates extends BaseEnumeration
{
public static $items = [
User::STATE_DELETED => 'admin/userManagement.status_' . User::STATE_DELETED,
User::STATE_BLOCKED => 'admin/userManagement.status_' . User::STATE_BLOCKED,
User::STATE_ACTIVE => 'admin/userManagement.status_' . User::STATE_ACTIVE
];
public static function init()
{
$items = [
User::STATE_DELETED => 'admin/userManagement.status_' . User::STATE_DELETED,
User::STATE_BLOCKED => 'admin/userManagement.status_' . User::STATE_BLOCKED,
User::STATE_ACTIVE => 'admin/userManagement.status_' . User::STATE_ACTIVE
];
foreach ($items as $state => $translation) {
self::push($state, $translation);
}
}
}