FEATURE: add baseclass for each enumeration
This commit is contained in:
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Enumerations;
|
||||||
|
|
||||||
|
use App\Enumerations\Exceptions\EnumerationExistsException;
|
||||||
|
|
||||||
|
abstract class BaseEnumeration
|
||||||
|
{
|
||||||
|
public static $items = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add new item into enumeration
|
||||||
|
*
|
||||||
|
* @param $key
|
||||||
|
* @param $translation
|
||||||
|
* @param bool|false $overwrite
|
||||||
|
*
|
||||||
|
* @throws EnumerationExistsException
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add to collection
|
||||||
|
self::$items[$key] = $translation;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return count of all items in collection
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public static function getCount()
|
||||||
|
{
|
||||||
|
return count(self::$items);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter for all enumeration items in iven collection.
|
||||||
|
*
|
||||||
|
* Return value is associative array indexed by item "name". Value of each field is its translation (translation
|
||||||
|
* string, e.g. admin/mainMenu.something)
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function getWithTranslations()
|
||||||
|
{
|
||||||
|
return self::$items;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter for list of all enumeration items.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function getSimple()
|
||||||
|
{
|
||||||
|
return array_keys(self::getWithTranslations());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Enumerations\Exceptions;
|
||||||
|
|
||||||
|
class EnumerationExistsException extends \Exception {
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user