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()];
}
/**