FEATURE: change directory structure
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
namespace App\CMS\Enumerations;
|
||||
|
||||
use App\CMS\Exceptions\Enumerations\EnumerationExistsException;
|
||||
|
||||
abstract class BaseEnumeration
|
||||
{
|
||||
protected static $initiliazed = [];
|
||||
public static $items = [];
|
||||
const ORDER_BY_KEY = 1;
|
||||
const ORDER_BY_KEY_DESC = 2;
|
||||
const ORDER_BY_TEXT = 4;
|
||||
const ORDER_BY_TEXT_DESC = 8;
|
||||
|
||||
/**
|
||||
* Init enumerations
|
||||
*/
|
||||
protected static function init()
|
||||
{
|
||||
$className = get_called_class();
|
||||
|
||||
if (!in_array($className, self::$initiliazed)) {
|
||||
|
||||
// try to call init() method
|
||||
if (method_exists($className, 'init')) {
|
||||
$className::init();
|
||||
}
|
||||
|
||||
// it can happen that even after init no enumerations are available
|
||||
if (empty(self::$items[$className])) {
|
||||
self::$items[$className] = [];
|
||||
}
|
||||
|
||||
self::$initiliazed[] = $className;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 && !is_null($key) && 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[get_called_class()][$key] = trans($translation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return count of all items in collection
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function getCount()
|
||||
{
|
||||
return count(self::getWithTranslations());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* @param string $orderBy
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getWithTranslations($orderBy = self::ORDER_BY_KEY)
|
||||
{
|
||||
// if not initialized yet, init this enumerration
|
||||
if (!in_array(get_called_class(), self::$initiliazed)) {
|
||||
self::init();
|
||||
}
|
||||
|
||||
$items = self::$items[get_called_class()];
|
||||
|
||||
// order results
|
||||
switch ($orderBy) {
|
||||
case self::ORDER_BY_KEY:
|
||||
ksort($items);
|
||||
break;
|
||||
case self::ORDER_BY_KEY_DESC:
|
||||
krsort($items);
|
||||
break;
|
||||
case self::ORDER_BY_TEXT:
|
||||
sort($items);
|
||||
break;
|
||||
case self::ORDER_BY_TEXT_DESC:
|
||||
rsort($items);
|
||||
break;
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for list of all enumeration items.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getSimple()
|
||||
{
|
||||
return array_keys(self::getWithTranslations());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user