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 = [];
|
||||
|
||||
/**
|
||||
* 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()];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user