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:
@@ -7,6 +7,20 @@ abstract class BaseEnumeration
|
|||||||
{
|
{
|
||||||
public static $items = [];
|
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
|
* Add new item into enumeration
|
||||||
*
|
*
|
||||||
@@ -19,12 +33,17 @@ abstract class BaseEnumeration
|
|||||||
public static function push($key, $translation, $overwrite = false)
|
public static function push($key, $translation, $overwrite = false)
|
||||||
{
|
{
|
||||||
// item exists and we do not want to overwrite items
|
// item exists and we do not want to overwrite items
|
||||||
if (!$overwrite && isset(self::$items[$key])) {
|
if (!$overwrite && isset(self::$items[get_called_class()][$key])) {
|
||||||
throw new EnumerationExistsException($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
|
// 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()
|
public static function getCount()
|
||||||
{
|
{
|
||||||
return count(self::$items);
|
return count(self::getWithTranslations());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,7 +66,11 @@ abstract class BaseEnumeration
|
|||||||
*/
|
*/
|
||||||
public static function getWithTranslations()
|
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()];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -3,8 +3,12 @@ namespace App\Enumerations;
|
|||||||
|
|
||||||
use App\Models\CustomFieldType;
|
use App\Models\CustomFieldType;
|
||||||
|
|
||||||
class CustomFieldTypes extends BaseEnumeration {
|
class CustomFieldTypes extends BaseEnumeration
|
||||||
public static function getWithTranslations() {
|
{
|
||||||
|
public static $items = [];
|
||||||
|
|
||||||
|
public static function init()
|
||||||
|
{
|
||||||
|
|
||||||
// load items from database
|
// load items from database
|
||||||
$types = CustomFieldType::all(['id', 'name']);
|
$types = CustomFieldType::all(['id', 'name']);
|
||||||
@@ -13,7 +17,5 @@ class CustomFieldTypes extends BaseEnumeration {
|
|||||||
foreach ($types as $type) {
|
foreach ($types as $type) {
|
||||||
self::push($type->id, $type->name);
|
self::push($type->id, $type->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return parent::getWithTranslations();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,9 +5,16 @@ use App\Models\User;
|
|||||||
|
|
||||||
class UserRoles extends BaseEnumeration
|
class UserRoles extends BaseEnumeration
|
||||||
{
|
{
|
||||||
public static $items = [
|
public static function init()
|
||||||
User::ROLE_ADMIN => 'admin/userManagement.role_' . User::ROLE_ADMIN,
|
{
|
||||||
User::ROLE_EDITOR => 'admin/userManagement.role_' . User::ROLE_EDITOR,
|
$roles = [
|
||||||
User::ROLE_VISITOR => 'admin/userManagement.role_' . User::ROLE_VISITOR
|
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,9 +12,16 @@ use App\Models\User;
|
|||||||
|
|
||||||
class UserStates extends BaseEnumeration
|
class UserStates extends BaseEnumeration
|
||||||
{
|
{
|
||||||
public static $items = [
|
public static function init()
|
||||||
User::STATE_DELETED => 'admin/userManagement.status_' . User::STATE_DELETED,
|
{
|
||||||
User::STATE_BLOCKED => 'admin/userManagement.status_' . User::STATE_BLOCKED,
|
$items = [
|
||||||
User::STATE_ACTIVE => 'admin/userManagement.status_' . User::STATE_ACTIVE
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user