diff --git a/DataStorage/Database/Pool.php b/DataStorage/Database/Pool.php index 9083e8ea1..7e4434ca0 100644 --- a/DataStorage/Database/Pool.php +++ b/DataStorage/Database/Pool.php @@ -86,7 +86,7 @@ class Pool public function get($key = 'core') { if (!isset($this->pool[$key])) { - return false; + return false; /* todo: return nullconnection */ } return $this->pool[$key]; @@ -119,18 +119,20 @@ class Pool * @param mixed $key Database key * @param array $config Database config data * - * @return void + * @return \bool * * @since 1.0.0 * @author Dennis Eichhorn */ - public function create($key, array $config) + public function create($key, array $config) : \bool { if (isset($this->pool[$key])) { - return; + return false; } $this->pool[$key] = ConnectionFactory::create($config); + + return true; } } diff --git a/Datatypes/Address.php b/Datatypes/Address.php index e9ebfb029..3a485dfe1 100644 --- a/Datatypes/Address.php +++ b/Datatypes/Address.php @@ -37,7 +37,7 @@ class Address implements JsonableInterface * @var \string * @since 1.0.0 */ - private $recipient = null; + private $recipient = ''; /** * Sub of the address. @@ -45,7 +45,7 @@ class Address implements JsonableInterface * @var \string * @since 1.0.0 */ - private $fao = null; + private $fao = ''; /** * Location. diff --git a/Datatypes/Enum.php b/Datatypes/Enum.php index 820cf210f..07201effc 100644 --- a/Datatypes/Enum.php +++ b/Datatypes/Enum.php @@ -31,14 +31,6 @@ namespace phpOMS\Datatypes; abstract class Enum { - /** - * Caching constant values. - * - * @var array - * @since 1.0.0 - */ - private static $constCache = null; - /** * Checking enum name. * @@ -55,7 +47,7 @@ abstract class Enum { $constants = self::getConstants(); - return array_key_exists($name, $constants); + return isset($constants[$name]); } /** @@ -68,12 +60,9 @@ abstract class Enum */ public static function getConstants() : array { - if (!isset(self::$constCache)) { - $reflect = new \ReflectionClass(get_called_class()); - self::$constCache = $reflect->getConstants(); - } + $reflect = new \ReflectionClass(get_called_class()); - return self::$constCache; + return $reflect->getConstants(); } /** @@ -92,7 +81,7 @@ abstract class Enum { $values = array_values(self::getConstants()); - return in_array($value, $values, $strict = true); + return in_array($value, $values, true); } } diff --git a/Datatypes/EnumArray.php b/Datatypes/EnumArray.php index a015d6bee..7b98ce84c 100644 --- a/Datatypes/EnumArray.php +++ b/Datatypes/EnumArray.php @@ -47,7 +47,7 @@ abstract class EnumArray { $constants = self::getConstants(); - return array_key_exists($name, $constants); + return isset($constants[$name]); } /** @@ -80,7 +80,7 @@ abstract class EnumArray { $constants = self::getConstants(); - return in_array($value, $constants, $strict = true); + return in_array($value, $constants, true); } /** @@ -97,7 +97,7 @@ abstract class EnumArray */ public static function get($key) { - $constants = self::getConstants()[$key]; + $constants = self::getConstants(); if (!isset($constants[$key])) { throw new \OutOfBoundsException('Key "' . $key . '" is not valid.'); diff --git a/Datatypes/Location.php b/Datatypes/Location.php index 3e7537723..2993fcfdb 100644 --- a/Datatypes/Location.php +++ b/Datatypes/Location.php @@ -37,7 +37,7 @@ class Location implements JsonableInterface * @var \string * @since 1.0.0 */ - private $postal = null; + private $postal = ''; /** * Name of city. @@ -45,7 +45,7 @@ class Location implements JsonableInterface * @var \string * @since 1.0.0 */ - private $city = null; + private $city = ''; /** * Name of the country. @@ -53,7 +53,7 @@ class Location implements JsonableInterface * @var \string * @since 1.0.0 */ - private $country = null; + private $country = ''; /** * Street & district. @@ -61,7 +61,7 @@ class Location implements JsonableInterface * @var \string * @since 1.0.0 */ - private $address = null; + private $address = ''; /** * State. @@ -69,7 +69,7 @@ class Location implements JsonableInterface * @var \string * @since 1.0.0 */ - private $state = null; + private $state = ''; /** * Geo coordinates. @@ -77,7 +77,7 @@ class Location implements JsonableInterface * @var \float[] * @since 1.0.0 */ - private $geo = null; + private $geo = ['lat' => 0, 'long' => 0]; /** * Constructor. diff --git a/Datatypes/SmartDateTime.php b/Datatypes/SmartDateTime.php index d6ca053f9..24af3cca6 100644 --- a/Datatypes/SmartDateTime.php +++ b/Datatypes/SmartDateTime.php @@ -70,16 +70,18 @@ class SmartDateTime extends \DateTime * @param \int $d Day * @param \int $calendar Calendar * - * @return void + * @return SmartDateTime * * @since 1.0.0 * @author Dennis Eichhorn */ - public function smartModify(\int $y, \int $m = 0, \int $d = 0, \int $calendar = CAL_GREGORIAN) + public function smartModify(\int $y, \int $m = 0, \int $d = 0, \int $calendar = CAL_GREGORIAN) : SmartDateTime { - $y_new = (int) $this->format('Y') + $y; + $y_change = floor(((int) $this->format('m') + $m) / 12); + $y_change = ((int) $this->format('m') + $m) < 0 && ((int) $this->format('m') + $m) % 12 === 0 ? $y_change - 1 : $y_change; + $y_new = (int) $this->format('Y') + $y + $y_change; $m_new = ((int) $this->format('m') + $m) % 12; - $m_new = $m_new === 0 ? 12 : $m_new; + $m_new = $m_new === 0 ? 12 : $m_new < 0 ? 12 + $m_new : $m_new; $d_month_old = cal_days_in_month($calendar, (int) $this->format('m'), (int) $this->format('Y')); $d_month_new = cal_days_in_month($calendar, $m_new, $y_new); $d_old = (int) $this->format('d'); @@ -97,6 +99,8 @@ class SmartDateTime extends \DateTime if ($d !== 0) { $this->modify($d . ' day'); } + + return $this; } } diff --git a/Dispatcher/Dispatcher.php b/Dispatcher/Dispatcher.php index 8a83c5353..2d67e0ee9 100644 --- a/Dispatcher/Dispatcher.php +++ b/Dispatcher/Dispatcher.php @@ -19,6 +19,7 @@ use phpOMS\ApplicationAbstract; use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; use phpOMS\Module\ModuleAbstract; +use phpOMS\System\FilePathException; use phpOMS\Views\ViewLayout; /** @@ -84,7 +85,7 @@ class Dispatcher $views = []; $type = ViewLayout::UNDEFINED; - if (isset($controller['type'])) { + if (is_array($controller) && isset($controller['type'])) { $type = $controller['type']; $controller = $controller['dest']; } @@ -119,20 +120,22 @@ class Dispatcher * * @param \string $controller Controller string * - * @return \bool + * @return mixed * * @since 1.0.0 * @author Dennis Eichhorn */ - public function get(\string $controller) : \bool + public function get(\string $controller) { if (!isset($this->controllers[$controller])) { - $this->controllers[$controller] = new $controller($this->app); + if (realpath($path = ROOT_PATH . '/' . str_replace('\\', '/', $controller) . '.php') === false) { + throw new FilePathException($path); + } - return true; + $this->controllers[$controller] = new $controller($this->app); } - return false; + return $this->controllers[$controller]; } /** diff --git a/Install/Backup.php b/Install/Backup.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Install/DummyFactory.php b/Install/DummyFactory.php deleted file mode 100644 index 88a6d9e0c..000000000 --- a/Install/DummyFactory.php +++ /dev/null @@ -1,55 +0,0 @@ - - * @author Dennis Eichhorn - * @copyright 2013 Dennis Eichhorn - * @license OMS License 1.0 - * @version 1.0.0 - * @link http://orange-management.com - */ -namespace phpOMS\Install; - -/** - * Dummy data factory. - * - * Used in order to install dummy data of modules - * - * @category Install - * @package Framework - * @author OMS Development Team - * @author Dennis Eichhorn - * @license OMS License 1.0 - * @link http://orange-management.com - * @since 1.0.0 - */ -class DummyFactory -{ - - /** - * Generate dummy data. - * - * @param \phpOMS\DataStorage\Database\Pool $db Database instance - * @param \string $module Module name (= directory name) - * @param \int $amount Amount of dummy data - * - * @return void - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public static function generate($db, $module, $amount = 9997) - { - if (file_exists(__DIR__ . '/../../Modules/' . $module . '/Admin/Dummy.class.php')) { - /** @var \phpOMS\Install\DummyInterface $class */ - $class = '\\Modules\\' . $module . '\\Admin\\Dummy'; - $class::generate($db, $amount); - } - } - -} diff --git a/Install/DummyInterface.php b/Install/DummyInterface.php deleted file mode 100644 index a21b55e55..000000000 --- a/Install/DummyInterface.php +++ /dev/null @@ -1,47 +0,0 @@ - - * @author Dennis Eichhorn - * @copyright 2013 Dennis Eichhorn - * @license OMS License 1.0 - * @version 1.0.0 - * @link http://orange-management.com - */ -namespace phpOMS\Install; - -/** - * Dummy data interface. - * - * Used in order to install dummy data of modules - * - * @category Install - * @package Framework - * @author OMS Development Team - * @author Dennis Eichhorn - * @license OMS License 1.0 - * @link http://orange-management.com - * @since 1.0.0 - */ -interface DummyInterface -{ - - /** - * Generate dummy data. - * - * @param \phpOMS\DataStorage\Database\Pool $db Database instance - * @param \int $amount Amount of dummy data - * - * @return void - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public static function generate($db, $amount); - -} diff --git a/Install/Library.php b/Install/Library.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Install/README.md b/Install/README.md deleted file mode 100644 index 57ed17d74..000000000 --- a/Install/README.md +++ /dev/null @@ -1 +0,0 @@ -# Install # diff --git a/Install/Uninstall.php b/Install/Uninstall.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/Install/Update.php b/Install/Update.php deleted file mode 100644 index 8aa879a75..000000000 --- a/Install/Update.php +++ /dev/null @@ -1,44 +0,0 @@ - - * @author Dennis Eichhorn - * @copyright 2013 Dennis Eichhorn - * @license OMS License 1.0 - * @version 1.0.0 - * @link http://orange-management.com - */ -namespace phpOMS\Install; - -/** - * Update class. - * - * @category Install - * @package Framework - * @author OMS Development Team - * @author Dennis Eichhorn - * @license OMS License 1.0 - * @link http://orange-management.com - * @since 1.0.0 - */ -class Update -{ - /* TODO: remove relative elements (security) and only allow paths in this application */ - - public static function replace($old, $new) - { - unlink($old); - rename('somewhere_in_temp_folder/' . $new, $old); - } - - public static function remove($old) - { - unlink($old); - } - -} diff --git a/Localization/Localization.php b/Localization/Localization.php index 310124884..8fa3d0225 100644 --- a/Localization/Localization.php +++ b/Localization/Localization.php @@ -14,7 +14,6 @@ * @link http://orange-management.com */ namespace phpOMS\Localization; -use phpOMS\Datatypes\Exception\InvalidEnumName; use phpOMS\Datatypes\Exception\InvalidEnumValue; /** @@ -37,7 +36,7 @@ class Localization * @var \string * @since 1.0.0 */ - public $country = null; + private $country = ISO3166Enum::_US; /** * Timezone. @@ -45,7 +44,7 @@ class Localization * @var \string * @since 1.0.0 */ - public $timezone = null; + private $timezone = 'America/New_York'; /** * Language ISO code. @@ -53,7 +52,7 @@ class Localization * @var \string * @since 1.0.0 */ - public $language = 'en'; + private $language = ISO639Enum::_EN; /** * Currency. @@ -61,7 +60,7 @@ class Localization * @var \string * @since 1.0.0 */ - public $currency = null; + private $currency = ISO4217Enum::C_USD; /** * Number format. @@ -69,7 +68,15 @@ class Localization * @var \string * @since 1.0.0 */ - public $numberformat = null; + private $decimal = '.'; + + /** + * Number format. + * + * @var \string + * @since 1.0.0 + */ + private $thousands = ','; /** * Time format. @@ -77,7 +84,7 @@ class Localization * @var \string * @since 1.0.0 */ - public $datetime = null; + private $datetime = 'Y-m-d H:i:s'; /** * Language array. @@ -85,7 +92,7 @@ class Localization * @var \string[] * @since 1.0.0 */ - public $lang = []; + private $lang = []; /** * Constructor. @@ -174,8 +181,8 @@ class Localization */ public function setLanguage(\string $language) { - if(!ISO639EnumArray::isValidName($language)) { - throw new InvalidEnumName($language); + if(!ISO639Enum::isValidValue($language)) { + throw new InvalidEnumValue($language); } $this->language = $language; @@ -226,8 +233,8 @@ class Localization */ public function setCurrency(\string $currency) { - if(!ISO4217EnumArray::isValidName($currency)) { - throw new InvalidEnumName($currency); + if(!ISO4217Enum::isValidValue($currency)) { + throw new InvalidEnumValue($currency); } $this->currency = $currency; @@ -263,21 +270,45 @@ class Localization * @since 1.0.0 * @author Dennis Eichhorn */ - public function getNumberformat() : \string + public function getDecimal() : \string { - return $this->numberformat; + return $this->decimal; } /** - * @param \string $numberformat + * @param \string $decimal * * @return \string * * @since 1.0.0 * @author Dennis Eichhorn */ - public function setNumberformat(\string $numberformat) : \string + public function setDecimal(\string $decimal) { - $this->numberformat = $numberformat; + $this->decimal = $decimal; + } + + /** + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getThousands() : \string + { + return $this->thousands; + } + + /** + * @param \string $thousands + * + * @return \string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setThousands(\string $thousands) + { + $this->thousands = $thousands; } } diff --git a/Log/FileLogger.php b/Log/FileLogger.php index 52bbdb1e4..819ff36b7 100644 --- a/Log/FileLogger.php +++ b/Log/FileLogger.php @@ -50,7 +50,7 @@ class FileLogger implements LoggerInterface /** * Instance. * - * @var \phpOMS\DataStorage\Cache\CacheManager + * @var \phpOMS\DataStorage\Cache\Pool * @since 1.0.0 */ protected static $instance = null; @@ -85,25 +85,31 @@ class FileLogger implements LoggerInterface */ public function __construct(\string $lpath) { - if (!file_exists($lpath)) { - mkdir($lpath); - } - $path = realpath($lpath); - if ($path === false || Validator::startsWith($path, ROOT_PATH) === false) { + if ($path !== false && Validator::startsWith($path, ROOT_PATH) === false) { throw new FilePathException($lpath); } - if (!file_exists($path)) { - mkdir($path, 0644, true); + if (is_dir($lpath)) { + if (!file_exists($lpath)) { + mkdir($lpath, 0644, true); + } + + if (!file_exists($lpath . '/' . date('Y-m-d') . '.log')) { + touch($lpath . '/' . date('Y-m-d') . '.log'); + } + + $path = realpath($lpath . '/' . date('Y-m-d') . '.log'); + } else { + if (!file_exists($lpath)) { + touch($lpath); + } + + $path = realpath($lpath); } - $this->path = $path . '/' . date('Y-m-d') . '.log'; - - if (!file_exists($path)) { - touch($path); - } + $this->path = $path; } /** @@ -237,8 +243,8 @@ class FileLogger implements LoggerInterface $replace['{backtrace}'] = str_replace(str_replace('\\', '\\\\', ROOT_PATH), '', $backtrace); $replace['{datetime}'] = sprintf('%--19s', (new \DateTime('NOW'))->format('Y-m-d H:i:s')); $replace['{level}'] = sprintf('%--12s', $level); - $replace['{path}'] = $_SERVER['REQUEST_URI']; - $replace['{ip}'] = sprintf('%--15s', $_SERVER['REMOTE_ADDR']); + $replace['{path}'] = $_SERVER['REQUEST_URI'] ?? 'REQUEST_URI'; + $replace['{ip}'] = sprintf('%--15s', $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0'); $replace['{version}'] = sprintf('%--15s', PHP_VERSION); $replace['{os}'] = sprintf('%--15s', PHP_OS); @@ -533,15 +539,16 @@ class FileLogger implements LoggerInterface while (($line = fgetcsv($this->fp, 0, ';')) !== false) { $id++; - $offset--; if ($offset > 0) { + $offset--; continue; } if ($limit <= 0) { - $logs = array_reverse($logs, true); - array_pop($logs); + + reset($logs); + unset($logs[key($logs)]); } foreach ($line as &$value) { @@ -550,11 +557,11 @@ class FileLogger implements LoggerInterface $logs[$id] = $line; $limit--; + ksort($logs); } fseek($this->fp, 0, SEEK_END); fclose($this->fp); - asort($logs); } return $logs; @@ -583,16 +590,16 @@ class FileLogger implements LoggerInterface continue; } - $log['datetime'] = $line[0] ?? ''; - $log['level'] = $line[1] ?? ''; - $log['ip'] = $line[2] ?? ''; - $log['line'] = $line[3] ?? ''; - $log['version'] = $line[4] ?? ''; - $log['os'] = $line[5] ?? ''; - $log['path'] = $line[6] ?? ''; - $log['message'] = $line[7] ?? ''; - $log['file'] = $line[8] ?? ''; - $log['backtrace'] = $line[9] ?? ''; + $log['datetime'] = trim($line[0] ?? ''); + $log['level'] = trim($line[1] ?? ''); + $log['ip'] = trim($line[2] ?? ''); + $log['line'] = trim($line[3] ?? ''); + $log['version'] = trim($line[4] ?? ''); + $log['os'] = trim($line[5] ?? ''); + $log['path'] = trim($line[6] ?? ''); + $log['message'] = trim($line[7] ?? ''); + $log['file'] = trim($line[8] ?? ''); + $log['backtrace'] = trim($line[9] ?? ''); break; }