Merge pull request #155 from Orange-Management/develop

code style fixes
This commit is contained in:
Dennis Eichhorn 2018-02-03 14:56:19 +01:00 committed by GitHub
commit 4bb778faf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1317 changed files with 26307 additions and 14016 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.log

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Account
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -26,10 +25,9 @@ use phpOMS\Validation\Network\Email;
* The account class is the base model for accounts. This model contains the most common account
* information. This model is not comparable to a profile which contains much more information.
*
* @category Framework
* @package phpOMS\Account
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Account implements ArrayableInterface, \JsonSerializable
@ -136,7 +134,7 @@ class Account implements ArrayableInterface, \JsonSerializable
/**
* Account type.
*
* @var AccountType|int
* @var int
* @since 1.0.0
*/
protected $type = AccountType::USER;
@ -144,7 +142,7 @@ class Account implements ArrayableInterface, \JsonSerializable
/**
* Account status.
*
* @var AccountStatus|int
* @var int
* @since 1.0.0
*/
protected $status = AccountStatus::INACTIVE;
@ -331,7 +329,8 @@ class Account implements ArrayableInterface, \JsonSerializable
&& ($p->getType() === $type || $p->getType() === null || !isset($type))
&& ($p->getElement() === $element || $p->getElement() === null || !isset($element))
&& ($p->getComponent() === $component || $p->getComponent() === null || !isset($component))
&& ($p->getPermission() | $permission) === $p->getPermission()) {
&& ($p->getPermission() | $permission) === $p->getPermission()
) {
return true;
}
}
@ -556,11 +555,17 @@ class Account implements ArrayableInterface, \JsonSerializable
*
* @return void
*
* @throws \Exception
*
* @since 1.0.0
*/
public function generatePassword(string $password) /* : void */
{
$this->password = password_hash($password, PASSWORD_DEFAULT);
$this->password = \password_hash($password, \PASSWORD_DEFAULT);
if ($this->password === false) {
throw new \Exception();
}
}
/**
@ -625,7 +630,7 @@ class Account implements ArrayableInterface, \JsonSerializable
/**
* Json serialize.
*
* @return string
* @return array
*
* @since 1.0.0
*/
@ -633,5 +638,4 @@ class Account implements ArrayableInterface, \JsonSerializable
{
return $this->toArray();
}
}

View File

@ -4,19 +4,17 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Account
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\Account;
use phpOMS\Auth\Auth;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Session\SessionInterface;
/**
@ -24,10 +22,9 @@ use phpOMS\DataStorage\Session\SessionInterface;
*
* The account manager is used to manage multiple accounts.
*
* @category Framework
* @package phpOMS\Account
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class AccountManager implements \Countable
@ -49,14 +46,6 @@ class AccountManager implements \Countable
*/
private $session = null;
/**
* Authenticator.
*
* @var Auth
* @since 1.0.0
*/
private $auth = null;
/**
* Constructor.
*
@ -67,7 +56,6 @@ class AccountManager implements \Countable
public function __construct(SessionInterface $session)
{
$this->session = $session;
$this->auth = new Auth($this->session);
}
/**
@ -82,7 +70,7 @@ class AccountManager implements \Countable
public function get(int $id = 0) : Account
{
if ($id === 0) {
$account = new Account($this->auth->authenticate());
$account = new Account(Auth::authenticate($this->session));
if (!isset($this->accounts[$account->getId()])) {
$this->accounts[$account->getId()] = $account;
@ -94,18 +82,6 @@ class AccountManager implements \Countable
return $this->accounts[$id] ?? new NullAccount();
}
/**
* Returns the authentication manager
*
* @return Auth
*
* @since 1.0.0
*/
public function getAuth() : Auth
{
return $this->auth;
}
/**
* Add account.
*
@ -157,5 +133,4 @@ class AccountManager implements \Countable
{
return count($this->accounts);
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Account
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\Stdlib\Base\Enum;
/**
* Account status enum.
*
* @category Framework
* @package phpOMS\Account
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class AccountStatus extends Enum

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Account
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\Stdlib\Base\Enum;
/**
* Account type enum.
*
* @category Framework
* @package phpOMS\Account
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class AccountType extends Enum

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Account
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\Contract\ArrayableInterface;
/**
* Account group class.
*
* @category Framework
* @package phpOMS\Account
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Group implements ArrayableInterface, \JsonSerializable
@ -123,6 +121,8 @@ class Group implements ArrayableInterface, \JsonSerializable
*
* @param string $name Group name
*
* @return void
*
* @since 1.0.0
*/
public function setName(string $name) /* : void */
@ -147,6 +147,8 @@ class Group implements ArrayableInterface, \JsonSerializable
*
* @param string $description Group description
*
* @return void
*
* @since 1.0.0
*/
public function setDescription(string $description) /* : void */
@ -171,11 +173,18 @@ class Group implements ArrayableInterface, \JsonSerializable
*
* @param int $status Group status
*
* @return void
*
* @throws InvalidEnumValue
*
* @since 1.0.0
*/
public function setStatus(int $status) /* : void */
{
// todo: check valid
if (!GroupStatus::isValidValue($status)) {
throw new InvalidEnumValue($status);
}
$this->status = $status;
}
@ -208,7 +217,7 @@ class Group implements ArrayableInterface, \JsonSerializable
/**
* Json serialize.
*
* @return string
* @return array
*
* @since 1.0.0
*/

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Account
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\Stdlib\Base\Enum;
/**
* Accept status enum.
*
* @category Framework
* @package phpOMS\Account
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class GroupStatus extends Enum

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Account
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,10 +17,9 @@ namespace phpOMS\Account;
/**
* Null account class.
*
* @category Framework
* @package phpOMS\Account
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class NullAccount extends Account

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Account
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -21,10 +20,9 @@ namespace phpOMS\Account;
* This permission abstract is the basis for all permissions. Contrary to it's name it is not an
* abstract class and can be used directly if needed.
*
* @category Framework
* @package phpOMS\Account
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class PermissionAbstract
@ -340,7 +338,7 @@ class PermissionAbstract
*
* @param int $permission Permission
*
* @return void
* @return bool
*
* @since 1.0.0
*/

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Account
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\Stdlib\Base\Enum;
/**
* Permission type enum.
*
* @category Framework
* @package phpOMS\Account
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class PermissionType extends Enum

View File

@ -1,32 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
declare(strict_types=1);
namespace phpOMS\Algorithm;
use phpOMS\Stdlib\Base\Enum;
/**
* Task status enum.
*
* @category Tasks
* @package Modules
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
abstract class AlgorithmType extends Enum
{
/* public */ const BRUTEFORCE = 0;
}

View File

@ -1,89 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
declare(strict_types=1);
namespace phpOMS\Algorithm\Knappsack;
use phpOMS\Algorithm\AlgorithmType;
/**
* Knappsack algorithm implementations
*
* @category Framework
* @package phpOMS\Auth
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Backpack
{
private $costMaximum = 0;
private $value = 0;
private $cost = 0;
private $items = [];
private $population = [];
public function __construct(float $costMaximum)
{
$this->costMaximum = $costMaximum;
}
public function addPopulationItem(ItemInterface $item) : bool
{
if(isset($this->population[$item->getId()])) {
return false;
}
$this->population[$item->getId()] = $item;
return true;
}
public function setPopulationItem(ItemInterface $item) /* : void */
{
$this->population[$item->getId()] = $item;
}
public function setCostCalculation(\Closure $callback) /* : void */
{
}
public function setValueCalculation(\Closure $callback) /* : void */
{
}
public function setTestPopulationBuilder(\Closure $callback) /* : void */
{
}
public function pack(int $type)
{
switch($type) {
case AlgorithmType::BRUTEFORCE:
return $this->bruteforce();
default:
throw new \Exception('Invalid algorithm type');
}
}
public function bruteforce()
{
}
}

View File

@ -1,30 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
declare(strict_types=1);
namespace phpOMS\Algorithm\Knappsack;
/**
* Shape interface.
*
* @category Framework
* @package phpOMS\Math
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
interface ItemInterface
{
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,10 +17,27 @@ namespace phpOMS;
/**
* Application class.
*
* @category Framework
* @package Framework
* This class contains all necessary application members. Access to them
* is restricted to write once in order to prevent manipulation
* and afterwards read only.
*
* @property mixed orgId
* @property string appName
* @property \phpOMS\DataStorage\Database\DatabasePool dbPool
* @property \phpOMS\Localization\L11nManager l11nManager
* @property \phpOMS\Router\Router router
* @property \phpOMS\DataStorage\Session\SessionInterface sessionManager
* @property \phpOMS\Module\ModuleManager moduleManager
* @property \phpOMS\Dispatcher\Dispatcher dispatcher
* @property \phpOMS\DataStorage\Cache\CachePool cachePool
* @property \Model\CoreSettings appSettings
* @property \phpOMS\Event\EventManager eventManager
* @property \phpOMS\Account\AccountManager accountManager
* @property \phpOMS\Log\FileLogger logger
*
* @package phpOMS
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class ApplicationAbstract

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Asset
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,10 +17,9 @@ namespace phpOMS\Asset;
/**
* Asset manager class.
*
* @category Framework
* @package phpOMS\Asset
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class AssetManager implements \Countable
@ -115,5 +113,4 @@ class AssetManager implements \Countable
{
return count($this->assets);
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Asset
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\Stdlib\Base\Enum;
/**
* Asset types enum.
*
* @category Framework
* @package phpOMS\Asset
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class AssetType extends Enum

View File

@ -4,18 +4,16 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Auth
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\Auth;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Session\SessionInterface;
/**
@ -23,44 +21,35 @@ use phpOMS\DataStorage\Session\SessionInterface;
*
* Responsible for authenticating and initializing the connection
*
* @category Framework
* @package phpOMS\Auth
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Auth
{
/**
* Session instance.
*
* @var SessionInterface
* @since 1.0.0
*/
private $session = null;
/**
* Constructor.
*
* @param SessionInterface $session Session
*
* @since 1.0.0
* @codeCoverageIgnore
*/
public function __construct(SessionInterface $session)
private function __construct()
{
$this->session = $session;
}
/**
* Authenticates user.
*
* @param SessionInterface $session Session
*
* @return int
*
* @since 1.0.0
*/
public function authenticate() : int
public static function authenticate(SessionInterface $session) : int
{
$uid = $this->session->get('UID');
$uid = $session->get('UID');
return empty($uid) ? 0 : $uid;
}
@ -68,15 +57,14 @@ class Auth
/**
* Logout the given user.
*
* @param int $uid User ID
* @param SessionInterface $session Session
*
* @return void
*
* @since 1.0.0
*/
public function logout(int $uid = null) /* : void */
public static function logout(SessionInterface $session) /* : void */
{
// TODO: logout other users? If admin wants to kick a user for updates etc.
$this->session->remove('UID');
$session->remove('UID');
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Auth
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -22,10 +21,9 @@ use phpOMS\Stdlib\Base\Enum;
*
* These are possible answers to authentications.
*
* @category Framework
* @package phpOMS\Auth
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class LoginReturnType extends Enum

View File

@ -4,24 +4,24 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS;
/**
* Permission exception class.
* Autoloader exception
*
* @category Framework
* @package phpOMS\System\File
* This exception is thrown if a file couldn't be autoloaded
*
* @package phpOMS
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class AutoloadException extends \RuntimeException
@ -31,7 +31,7 @@ class AutoloadException extends \RuntimeException
*
* @param string $message Exception message
* @param int $code Exception code
* @param \Exception Previous exception
* @param \Exception $previous Previous exception
*
* @since 1.0.0
*/

View File

@ -4,26 +4,24 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS;
spl_autoload_register('\phpOMS\Autoloader::default_autoloader');
spl_autoload_register('\phpOMS\Autoloader::defaultAutoloader');
/**
* Autoloader class.
*
* @category Framework
* @package Framework
* @package phpOMS
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Autoloader
@ -34,7 +32,7 @@ class Autoloader
*
* @param string $class Class path
*
* @example Autoloader::default_autoloader('\phpOMS\Autoloader') // void
* @example Autoloader::defaultAutoloader('\phpOMS\Autoloader') // void
*
* @return void
*
@ -42,7 +40,7 @@ class Autoloader
*
* @since 1.0.0
*/
public static function default_autoloader(string $class) /* : void */
public static function defaultAutoloader(string $class) /* : void */
{
$class = ltrim($class, '\\');
$class = str_replace(['_', '\\'], '/', $class);
@ -73,5 +71,4 @@ class Autoloader
return file_exists(__DIR__ . '/../' . $class . '.php');
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Business\Finance
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Business\Finance
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -21,11 +20,13 @@ use phpOMS\Math\Matrix\Exception\InvalidDimensionException;
/**
* Finance class.
*
* @category Log
* @package Framework
* @package phpOMS\Business\Finance
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*
* @SuppressWarnings(PHPMD.CamelCaseParameterName)
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
*/
class FinanceFormulas
{
@ -44,7 +45,7 @@ class FinanceFormulas
*/
public static function getAnnualPercentageYield(float $r, int $n) : float
{
return pow(1 + $r / $n, $n) - 1;
return (float) pow(1 + $r / $n, $n) - 1;
}
/**
@ -61,7 +62,7 @@ class FinanceFormulas
*/
public static function getStateAnnualInterestRateOfAPY(float $apy, int $n) : float
{
return (pow($apy + 1, 1 / $n) - 1) * $n;
return (float) (pow($apy + 1, 1 / $n) - 1) * $n;
}
/**
@ -112,7 +113,6 @@ class FinanceFormulas
return $fva / ((pow(1 + $r, $n) - 1) / $r);
}
/**
* Annuity - Future Value w/ Continuous Compounding
*
@ -754,6 +754,20 @@ class FinanceFormulas
return log(2) / log(1 + $r);
}
/**
* Get rate to dobule
*
* @param float $t Time in which to double investment
*
* @return float
*
* @since 1.0.0
*/
public static function getDoublingRate(float $t) : float
{
return exp(log(2) / $t) - 1;
}
/**
* Doubling Time - Continuous Compounding
*
@ -896,7 +910,7 @@ class FinanceFormulas
*/
public static function getFutureValueFactor(float $r, int $n) : float
{
return pow(1 + $r, $n);
return (float) pow(1 + $r, $n);
}
/**
@ -1392,5 +1406,4 @@ class FinanceFormulas
{
return $ownSales / $competitorSales;
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\Business\Finance\Forecasting;

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\Business\Finance\Forecasting;

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\Business\Finance\Forecasting;

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\Business\Finance\Forecasting;
@ -19,6 +18,7 @@ use phpOMS\Math\Statistic\Average;
class ARIMA
{
private $data = [];
private $order = 0;
public function __construct(array $data, int $order = 12)

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\Business\Finance\Forecasting;

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package Framework
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -22,10 +21,9 @@ use phpOMS\Math\Statistic\Average;
*
* This can be used to simplify time series patterns for forecasts.
*
* @category Framework
* @package phpOMS\Math\Finance\Forecasting
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @see https://www.otexts.org/fpp/6/1
* @since 1.0.0
*/
@ -174,7 +172,7 @@ class ClassicalDecomposition
*/
public static function getStartOfDecomposition(int $dataSize, int $trendCycleComponents) : int
{
return ($dataSize - $trendCycleComponents) / 2;
return (int) (($dataSize - $trendCycleComponents) / 2);
}
/**

View File

@ -0,0 +1,33 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @package Framework
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\Business\Finance\Forecasting\ExponentialSmoothing;
use phpOMS\Stdlib\Base\Enum;
/**
* Smoothing enum.
*
* @package Framework
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class ErrorType extends Enum
{
/* public */ const ALL = 0;
/* public */ const NONE = 1;
/* public */ const ADDITIVE = 2;
/* public */ const MULTIPLICATIVE = 4;
}

View File

@ -4,12 +4,12 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package Framework
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @see https://www.otexts.org/fpp/7/7
*/
declare(strict_types = 1);
@ -38,585 +38,75 @@ class ExponentialSmoothing
$this->data = $data;
}
public function getRMSE() : float
public function getANN()
{
return $this->rmse;
}
public function getMSE() : float
public function getANA()
{
return $this->mse;
}
public function getMAE() : float
public function getANM()
{
return $this->mae;
}
public function getSSE() : float
public function getAAN()
{
return $this->sse;
}
public function getErrors() : array
public function getAAA()
{
return $this->errors;
}
public function getForecast(int $future, int $trendType = TrendType::NONE, int $seasonalType = SeasonalType::NONE, int $cycle = 12, float $damping = 1) : array
public function getAAM()
{
$this->rmse = PHP_INT_MAX;
if($trendType === TrendType::ALL || $seasonalType === SeasonalType::ALL) {
$trends = [$trendType];
if($trendType === TrendType::ALL) {
$trends = [TrendType::NONE, TrendType::ADDITIVE, TrendType::MULTIPLICATIVE];
}
$seasonals = [$seasonalType];
if($seasonalType === SeasonalType::ALL) {
$seasonals = [SeasonalType::NONE, SeasonalType::ADDITIVE, SeasonalType::MULTIPLICATIVE];
}
$forecast = [];
$bestError = PHP_INT_MAX;
foreach($trends as $trend) {
foreach($seasonals as $seasonal) {
$tempForecast = $this->getForecast($future, $trend, $seasonal, $cycle, $damping);
if ($this->rmse < $bestError) {
$bestError = $this->rmse;
$forecast = $tempForecast;
}
}
}
return $forecast;
} elseif($trendType === TrendType::NONE && $seasonalType === SeasonalType::NONE) {
return $this->getNoneNone($future);
} elseif($trendType === TrendType::NONE && $seasonalType === SeasonalType::ADDITIVE) {
return $this->getNoneAdditive($future, $cycle);
} elseif($trendType === TrendType::NONE && $seasonalType === SeasonalType::MULTIPLICATIVE) {
return $this->getNoneMultiplicative($future, $cycle);
} elseif($trendType === TrendType::ADDITIVE && $seasonalType === SeasonalType::NONE) {
return $this->getAdditiveNone($future, $damping);
} elseif($trendType === TrendType::ADDITIVE && $seasonalType === SeasonalType::ADDITIVE) {
return $this->getAdditiveAdditive($future, $cycle, $damping);
} elseif($trendType === TrendType::ADDITIVE && $seasonalType === SeasonalType::MULTIPLICATIVE) {
return $this->getAdditiveMultiplicative($future, $cycle, $damping);
} elseif($trendType === TrendType::MULTIPLICATIVE && $seasonalType === SeasonalType::NONE) {
return $this->getMultiplicativeNone($future, $damping);
} elseif($trendType === TrendType::MULTIPLICATIVE && $seasonalType === SeasonalType::ADDITIVE) {
return $this->getMultiplicativeAdditive($future, $cycle, $damping);
} elseif($trendType === TrendType::MULTIPLICATIVE && $seasonalType === SeasonalType::MULTIPLICATIVE) {
return $this->getMultiplicativeMultiplicative($future, $cycle, $damping);
}
throw new \Exception();
}
private function dampingSum(float $damping, int $length) : float
public function getAMN()
{
if(abs($damping - 1) < 0.001) {
return $length;
}
$sum = 0;
for($i = 0; $i < $length; $i++) {
$sum += pow($damping, $i);
}
return $sum;
}
public function getNoneNone(int $future) : array
public function getAMA()
{
$level = [$this->data[0]];
$dataLength = count($this->data) + $future;
$forecast = [];
$alpha = 0.00;
while ($alpha < 1) {
$error = [];
$tempForecast = [];
for($i = 1; $i < $dataLength; $i++) {
$level[$i] = $alpha * ($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) + (1 - $alpha) * $level[$i-1];
$tempForecast[$i] = $level[$i];
$error[] = $i < $dataLength - $future ? $this->data[$i] - $tempForecast[$i] : 0;
}
$tempRMSE = Error::getRootMeanSquaredError($error);
if ($tempRMSE < $this->rmse) {
$this->rmse = $tempRMSE;
$forecast = $tempForecast;
}
$alpha += 0.01;
}
$this->errors = $error;
$this->mse = Error::getMeanSquaredError($error);
$this->mae = Error::getMeanAbsoulteError($error);
$this->sse = Error::getSumSquaredError($error);
return $forecast;
}
public function getNoneAdditive(int $future, int $cycle) : array
public function getAMM()
{
$level = [$this->data[0]];
$dataLength = count($this->data) + $future;
$forecast = [];
$seasonal = [];
for($i = 1; $i < $cycle+1; $i++) {
$seasonal[$i] = $this->data[$i-1] - $level[0];
}
$alpha = 0.00;
while ($alpha < 1) {
$gamma = 0.00;
while($gamma < 1) {
$gamma_ = $gamma * (1 - $alpha);
$error = [];
$tempForecast = [];
for($i = 1; $i < $dataLength; $i++) {
$hm = (int) floor(($i-1) % $cycle) + 1;
$level[$i] = $alpha * (($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) - $seasonal[$i]) + (1 - $alpha) * $level[$i-1];
$seasonal[$i+$cycle] = $gamma_*(($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) - $level[$i-1]) + (1 - $gamma_) * $seasonal[$i];
$tempForecast[$i] = $level[$i] + $seasonal[$i+$hm];
$error[] = $i < $dataLength - $future ? $this->data[$i] - $tempForecast[$i] : 0;
}
$tempRMSE = Error::getRootMeanSquaredError($error);
if ($tempRMSE < $this->rmse) {
$this->rmse = $tempRMSE;
$forecast = $tempForecast;
}
$gamma += 0.01;
}
$alpha += 0.01;
}
$this->errors = $error;
$this->mse = Error::getMeanSquaredError($error);
$this->mae = Error::getMeanAbsoulteError($error);
$this->sse = Error::getSumSquaredError($error);
return $forecast;
}
public function getNoneMultiplicative(int $future, int $cycle) : array
public function getMNN()
{
$level = [$this->data[0]];
$dataLength = count($this->data) + $future;
$forecast = [];
$seasonal = [];
for($i = 1; $i < $cycle+1; $i++) {
$seasonal[$i] = $this->data[$i] / $level[0];
}
$alpha = 0.00;
while ($alpha < 1) {
$gamma = 0.00;
while($gamma < 1) {
$gamma_ = $gamma * (1 - $alpha);
$error = [];
$tempForecast = [];
for($i = 1; $i < $dataLength; $i++) {
$hm = (int) floor(($i-1) % $cycle) + 1;
$level[$i] = $alpha * (($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) / $seasonal[$i]) + (1 - $alpha) * $level[$i-1];
$seasonal[$i+$cycle] = $gamma_*(($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) / $level[$i-1]) + (1 - $gamma_) * $seasonal[$i];
$tempForecast[$i] = $level[$i] + $seasonal[$i+$hm];
$error[] = $i < $dataLength - $future ? $this->data[$i] - $tempForecast[$i] : 0;
}
$tempRMSE = Error::getRootMeanSquaredError($error);
if ($tempRMSE < $this->rmse) {
$this->rmse = $tempRMSE;
$forecast = $tempForecast;
}
$gamma += 0.01;
}
$alpha += 0.01;
}
$this->errors = $error;
$this->mse = Error::getMeanSquaredError($error);
$this->mae = Error::getMeanAbsoulteError($error);
$this->sse = Error::getSumSquaredError($error);
return $forecast;
}
public function getAdditiveNone(int $future, float $damping) : array
public function getMNA()
{
$level = [$this->data[0]];
$trend = [$this->data[1] - $this->data[0]];
$dataLength = count($this->data) + $future;
$forecast = [];
$alpha = 0.00;
while ($alpha < 1) {
$beta = 0.00;
while($beta < 1) {
$error = [];
$tempForecast = [];
for($i = 1; $i < $dataLength; $i++) {
$level[$i] = $alpha * ($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) + (1 - $alpha) * ($level[$i-1] + $damping * $trend[$i-1]);
$trend[$i] = $beta * ($level[$i] - $level[$i-1]) + (1 - $beta) * $damping * $trend[$i-1];
$tempForecast[$i] = $level[$i] + $this->dampingSum($damping, $i) * $trend[$i];
$error[] = $i < $dataLength - $future ? $this->data[$i] - $tempForecast[$i] : 0;
}
$tempRMSE = Error::getRootMeanSquaredError($error);
if ($tempRMSE < $this->rmse) {
$this->rmse = $tempRMSE;
$forecast = $tempForecast;
}
$beta += 0.01;
}
$alpha += 0.01;
}
$this->errors = $error;
$this->mse = Error::getMeanSquaredError($error);
$this->mae = Error::getMeanAbsoulteError($error);
$this->sse = Error::getSumSquaredError($error);
return $forecast;
}
public function getAdditiveAdditive(int $future, int $cycle, float $damping) : array
public function getMNM()
{
$level = [1 / $cycle * array_sum(array_slice($this->data, 0, $cycle))];
$trend = [1 / $cycle];
$dataLength = count($this->data) + $future;
$forecast = [];
$seasonal = [];
$sum = 0;
for($i = 1; $i < $cycle+1; $i++) {
$sum += ($this->data[$cycle] - $this->data[$i]) / $cycle;
}
$trend[0] *= $sum;
for($i = 1; $i < $cycle+1; $i++) {
$seasonal[$i] = $this->data[$i-1] - $level[0];
}
$alpha = 0.00;
while ($alpha < 1) {
$beta = 0.00;
while($beta < 1) {
$gamma = 0.00;
while($gamma < 1) {
$gamma_ = $gamma * (1 - $alpha);
$error = [];
$tempForecast = [];
for($i = 1; $i < $dataLength; $i++) {
$hm = (int) floor(($i-1) % $cycle) + 1;
$level[$i] = $alpha * (($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) - $seasonal[$i]) + (1 - $alpha) * ($level[$i-1] + $damping * $trend[$i-1]);
$trend[$i] = $beta * ($level[$i] - $level[$i-1]) + (1 - $beta) * $damping * $trend[$i-1];
$seasonal[$i+$cycle] = $gamma_*(($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) - $level[$i-1]) + (1 - $gamma_) * $seasonal[$i];
$tempForecast[$i] = $level[$i] + $this->dampingSum($damping, $i) * $trend[$i] + $seasonal[$i+$hm];
$error[] = $i < $dataLength - $future ? $this->data[$i] - $tempForecast[$i] : 0;
}
$tempRMSE = Error::getRootMeanSquaredError($error);
if ($tempRMSE < $this->rmse) {
$this->rmse = $tempRMSE;
$forecast = $tempForecast;
}
$gamma += 0.01;
}
$beta += 0.01;
}
$alpha += 0.01;
}
$this->errors = $error;
$this->mse = Error::getMeanSquaredError($error);
$this->mae = Error::getMeanAbsoulteError($error);
$this->sse = Error::getSumSquaredError($error);
return $forecast;
}
public function getAdditiveMultiplicative(int $future, int $cycle, float $damping) : array
public function getMAN()
{
$level = [1 / $cycle * array_sum(array_slice($this->data, 0, $cycle))];
$trend = [1 / $cycle];
$dataLength = count($this->data) + $future;
$forecast = [];
$seasonal = [];
$gamma_ = $gamma * (1 - $alpha);
$sum = 0;
for($i = 1; $i < $cycle+1; $i++) {
$sum += ($this->data[$cycle] - $this->data[$i]) / $cycle;
}
$trend[0] *= $sum;
for($i = 1; $i < $cycle+1; $i++) {
$seasonal[$i] = $this->data[$i] / $level[0];
}
$alpha = 0.00;
while ($alpha < 1) {
$beta = 0.00;
while($beta < 1) {
$gamma = 0.00;
while($gamma < 1) {
$gamma_ = $gamma * (1 - $alpha);
$error = [];
$tempForecast = [];
for($i = 1; $i < $dataLength; $i++) {
$hm = (int) floor(($i-1) % $cycle) + 1;
$level[$i] = $alpha * (($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) / $seasonal[$i]) + (1 - $alpha) * ($level[$i-1] + $damping * $trend[$i-1]);
$trend[$i] = $beta * ($level[$i] - $level[$i-1]) + (1 - $beta) * $damping * $trend[$i-1];
$seasonal[$i+$cycle] = $gamma_*($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) / ($level[$i-1] + $damping * $trend[$i-1]) + (1 - $gamma_) * $seasonal[$i];
$tempForecast[] = ($level[$i] + $this->dampingSum($damping, $i) * $trend[$i-1]) * $seasonal[$i+$hm];
$error[] = $i < $dataLength - $future ? $this->data[$i] - $tempForecast[$i] : 0;
}
$tempRMSE = Error::getRootMeanSquaredError($error);
if ($tempRMSE < $this->rmse) {
$this->rmse = $tempRMSE;
$forecast = $tempForecast;
}
$gamma += 0.01;
}
$beta += 0.01;
}
$alpha += 0.01;
}
$this->errors = $error;
$this->mse = Error::getMeanSquaredError($error);
$this->mae = Error::getMeanAbsoulteError($error);
$this->sse = Error::getSumSquaredError($error);
return $forecast;
}
public function getMultiplicativeNone(int $future, float $damping) : array
public function getMAA()
{
$level = [$this->data[0]];
$trend = [$this->data[1] / $this->data[0]];
$dataLength = count($this->data) + $future;
$forecast = [];
$alpha = 0.00;
while ($alpha < 1) {
$beta = 0.00;
while($beta < 1) {
$error = [];
$tempForecast = [];
for($i = 1; $i < $dataLength; $i++) {
$level[$i] = $alpha * ($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) + (1 - $alpha) * $level[$i-1] * pow($trend[$i-1], $damping);
$trend[$i] = $beta * ($level[$i] / $level[$i-1]) + (1 - $beta) * pow($trend[$i-1], $damping);
$tempForecast[$i] = $level[$i] * pow($trend[$i], $this->dampingSum($damping, $i));
$error[] = $i < $dataLength - $future ? $this->data[$i] - $tempForecast[$i] : 0;
}
$tempRMSE = Error::getRootMeanSquaredError($error);
if ($tempRMSE < $this->rmse) {
$this->rmse = $tempRMSE;
$forecast = $tempForecast;
}
$beta += 0.01;
}
$alpha += 0.01;
}
$this->errors = $error;
$this->mse = Error::getMeanSquaredError($error);
$this->mae = Error::getMeanAbsoulteError($error);
$this->sse = Error::getSumSquaredError($error);
return $forecast;
}
public function getMultiplicativeAdditive(int $future, int $cycle, float $damping) : array
public function getMAM()
{
$level = [$this->data[0]];
$trend = [1 / $cycle];
$dataLength = count($this->data) + $future;
$forecast = [];
$seasonal = [];
$sum = 0;
for($i = 1; $i < $cycle+1; $i++) {
$sum += ($this->data[$cycle] - $this->data[$i]) / $cycle;
}
$trend[0] *= $sum;
for($i = 1; $i < $cycle+1; $i++) {
$seasonal[$i] = $this->data[$i-1] - $level[0];
}
$alpha = 0.00;
while ($alpha < 1) {
$beta = 0.00;
while($beta < 1) {
$gamma = 0.00;
while($gamma < 1) {
$gamma_ = $gamma * (1 - $alpha);
$error = [];
$tempForecast = [];
for($i = 1; $i < $dataLength; $i++) {
$hm = (int) floor(($i-1) % $cycle) + 1;
$level[$i] = $alpha * (($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) - $seasonal[$i]) + (1 - $alpha) * $level[$i-1] * pow($trend[$i-1], $damping);
$trend[$i] = $beta * ($level[$i] / $level[$i-1]) + (1 - $beta) * pow($trend[$i-1], $damping);
$seasonal[$i+$cycle] = $gamma_*(($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) - $level[$i-1] * pow($trend[$i-1], $damping)) + (1 - $gamma_) * $seasonal[$i];
$tempForecast[$i] = $level[$i] * pow($trend[$i], $this->dampingSum($damping, $i)) + $seasonal[$i+$hm];
$error[] = $i < $dataLength - $future ? $this->data[$i] - $tempForecast[$i] : 0;
}
$tempRMSE = Error::getRootMeanSquaredError($error);
if ($tempRMSE < $this->rmse) {
$this->rmse = $tempRMSE;
$forecast = $tempForecast;
}
$gamma += 0.01;
}
$beta += 0.01;
}
$alpha += 0.01;
}
$this->errors = $error;
$this->mse = Error::getMeanSquaredError($error);
$this->mae = Error::getMeanAbsoulteError($error);
$this->sse = Error::getSumSquaredError($error);
return $forecast;
}
public function getMultiplicativeMultiplicative(int $future, int $cycle, float $damping) : array
public function getMMN()
{
$level = [$this->data[0]];
$trend = [1 / $cycle];
$dataLength = count($this->data) + $future;
$forecast = [];
$seasonal = [];
$sum = 0;
for($i = 1; $i < $cycle+1; $i++) {
$sum += ($this->data[$cycle] - $this->data[$i]) / $cycle;
}
$trend[0] *= $sum;
for($i = 1; $i < $cycle+1; $i++) {
$seasonal[$i] = $this->data[$i] / $level[0];
public function getMMA()
{
}
$alpha = 0.00;
while ($alpha < 1) {
$beta = 0.00;
while($beta < 1) {
$gamma = 0.00;
while($gamma < 1) {
$gamma_ = $gamma * (1 - $alpha);
$error = [];
$tempForecast = [];
for($i = 1; $i < $dataLength; $i++) {
$hm = (int) floor(($i-1) % $cycle) + 1;
$level[$i] = $alpha * (($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) / $seasonal[$i]) + (1 - $alpha) * $level[$i-1] * pow($trend[$i-1], $damping);
$trend[$i] = $beta * ($level[$i] / $level[$i-1]) + (1 - $beta) * pow($trend[$i-1], $damping);
$seasonal[$i+$cycle] = $gamma_*($i < $dataLength - $future ? $this->data[$i-1] : $tempForecast[$i-1]) / ($level[$i-1] * pow($trend[$i-1], $damping)) + (1 - $gamma_) * $seasonal[$i];
$tempForecast[$i] = $level[$i] * pow($trend[$i], $this->dampingSum($damping, $i)) * $seasonal[$i+$hm];
$error[] = $i < $dataLength - $future ? $this->data[$i] - $tempForecast[$i] : 0;
public function getMMM()
{
}
$tempRMSE = Error::getRootMeanSquaredError($error);
if ($tempRMSE < $this->rmse) {
$this->rmse = $tempRMSE;
$forecast = $tempForecast;
}
$gamma += 0.01;
}
$beta += 0.01;
}
$alpha += 0.01;
}
$this->errors = $error;
$this->mse = Error::getMeanSquaredError($error);
$this->mae = Error::getMeanAbsoulteError($error);
$this->sse = Error::getSumSquaredError($error);
return $forecast;
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package Framework
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\Stdlib\Base\Enum;
/**
* Smoothing enum.
*
* @category Framework
* @package phpOMS\Html
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class SeasonalType extends Enum

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package Framework
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\Stdlib\Base\Enum;
/**
* Smoothing enum.
*
* @category Framework
* @package phpOMS\Html
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class TrendType extends Enum

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\Business\Finance\Forecasting;

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\Business\Finance\Forecasting;
@ -18,4 +17,3 @@ class MA
{
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\Business\Finance\Forecasting;
@ -18,4 +17,3 @@ class NAR
{
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\Business\Finance\Forecasting;
@ -18,4 +17,3 @@ class NMA
{
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\Business\Finance\Forecasting;
@ -18,4 +17,3 @@ class SARIMA
{
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\Stdlib\Base\Enum;
/**
* Smoothing enum.
*
* @category Framework
* @package phpOMS\Html
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class SmoothingType extends Enum

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Business\Finance
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,11 +17,13 @@ namespace phpOMS\Business\Finance;
/**
* Finance class.
*
* @category Log
* @package Framework
* @package phpOMS\Business\Finance
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*
* @SuppressWarnings(PHPMD.CamelCaseParameterName)
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
*/
class Loan
{
@ -122,5 +123,4 @@ class Loan
{
return $loan / $collateral;
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Business\Finance
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,10 +17,9 @@ namespace phpOMS\Business\Finance;
/**
* Finance class.
*
* @category Log
* @package Framework
* @package phpOMS\Business\Finance
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Lorenzkurve

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Business\Finance
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,11 +17,13 @@ namespace phpOMS\Business\Finance;
/**
* Finance class.
*
* @category Log
* @package Framework
* @package phpOMS\Business\Finance
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*
* @SuppressWarnings(PHPMD.CamelCaseParameterName)
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
*/
class StockBonds
{
@ -368,7 +369,6 @@ class StockBonds
*/
public static function getZeroCouponBondEffectiveYield(float $F, float $PV, int $n) : float
{
return pow($F / $PV, 1 / $n) - 1;
return (float) pow($F / $PV, 1 / $n) - 1;
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Business\Marketing
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,16 +19,18 @@ namespace phpOMS\Business\Marketing;
*
* This class provided basic marketing metric calculations
*
* @category Framework
* @package phpOMS\Business
* @package phpOMS\Business\Marketing
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Metrics {
class Metrics
{
/**
* Calculate customer retention
*
* @latex r = \frac{ce - cn}{cs}
*
* @param int $ce Customer at the end of the period
* @param int $cn New customers during period
* @param int $cs Customers at the start of the period

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Business\Marketing
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -21,13 +20,13 @@ namespace phpOMS\Business\Marketing;
* The net promoter score is a basic evaluation of the happiness of customers.
* Instead of customers the NPS can also be transferred to non-customers.
*
* @category Framework
* @package phpOMS\Business
* @package phpOMS\Business\Marketing
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class NetPromoterScore {
class NetPromoterScore
{
/**
* Score values
*
@ -41,7 +40,8 @@ class NetPromoterScore {
*
* @since 1.0.0
*/
public function __construct() {
public function __construct()
{
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Business\Programming
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,13 +19,13 @@ namespace phpOMS\Business\Programming;
*
* This class provides basic programming metric calculations.
*
* @category Framework
* @package phpOMS\Business
* @package phpOMS\Business\Programming
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Metrics {
class Metrics
{
/**
* Calculate ABC metric score
*
@ -48,15 +47,16 @@ class Metrics {
/**
* Calculate the C.R.A.P score
*
* @latex r = \sqrt{a^{2} + b^{2} + c^{2}}
* @latex r = com^{2} \times (1 - cov)^{3} + com
*
* @param int $a Assignments
* @param int $b Branches
* @param int $c Conditionals
* @param int $complexity Complexity
* @param float $coverage Coverage
*
* @return int
*
* @since 1.0.0
*
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
*/
public static function CRAP(int $complexity, float $coverage) : int
{

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Business\Sales
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -21,13 +20,13 @@ namespace phpOMS\Business\Sales;
* This class can be used to calculate the market share based on a rank or vice versa
* the rank based on a marketshare in a Zipf distributed market.
*
* @category Framework
* @package phpOMS\Business
* @package phpOMS\Business\Sales
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class MarketShareEstimation {
class MarketShareEstimation
{
/**
* Calculate rank (r) based on market share (m)
*

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Config
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,10 +17,9 @@ namespace phpOMS\Config;
/**
* Options class.
*
* @category Framework
* @package phpOMS\Config
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
interface OptionsInterface
@ -73,5 +71,4 @@ interface OptionsInterface
* @since 1.0.0
*/
public function getOption($key);
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Config
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,8 +17,9 @@ namespace phpOMS\Config;
/**
* Options trait.
*
* @category Framework
* @package phpOMS\Config
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
trait OptionsTrait
@ -54,7 +54,7 @@ trait OptionsTrait
*/
public function setOption($key, $value, bool $overwrite = true) : bool
{
if ($overwrite || !array_key_exists($key, $this->options)) {
if ($overwrite || !isset($this->options[$key])) {
$this->options[$key] = $value;
return true;
@ -76,5 +76,4 @@ trait OptionsTrait
return false;
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Config
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -24,10 +23,9 @@ use phpOMS\DataStorage\Database\Query\Builder;
*
* Responsible for providing a database/cache bound settings manger
*
* @category Framework
* @package phpOMS\Config
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class SettingsAbstract implements OptionsInterface
@ -84,6 +82,7 @@ abstract class SettingsAbstract implements OptionsInterface
* @return mixed Option value
*
* @since 1.0.0
* @todo: don't db request if exists. check exists()
*/
public function get($columns)
{
@ -110,7 +109,7 @@ abstract class SettingsAbstract implements OptionsInterface
break;
}
return $options;
return count($options) > 1 ? $options : reset($options);
} catch (\PDOException $e) {
$exception = DatabaseExceptionFactory::createException($e);
$message = DatabaseExceptionFactory::createExceptionMessage($e);
@ -134,7 +133,16 @@ abstract class SettingsAbstract implements OptionsInterface
$this->setOptions($options);
if ($store) {
// save to db
foreach ($this->options as $key => $option) {
$query = new Builder($this->connection);
$sql = $query->update($this->connection->prefix . static::$table)
->set([static::$columns[1] => $option])
->where(static::$columns[0], '=', $key)
->toSql();
$sth = $this->connection->con->prepare($sql);
$sth->execute();
}
}
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,8 +17,7 @@ namespace phpOMS\Console;
/**
* CommandManager class.
*
* @category Framework
* @package phpOMS\Socket
* @package Framework
* @since 1.0.0
*
* @todo : Hey, this looks like a copy of an event manager!
@ -128,5 +126,4 @@ class CommandManager implements \Countable
{
return $this->count;
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Contract
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ namespace phpOMS\Contract;
*
* This stands always in combination with a jsonable instance.
*
* @category Framework
* @package phpOMS\Contract
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
interface ArrayableInterface
@ -37,5 +35,4 @@ interface ArrayableInterface
* @since 1.0.0
*/
public function toArray() : array;
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @package phpOMS\Contract
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -21,10 +20,9 @@ namespace phpOMS\Contract;
* This is primarily used for classes that provide formatted output or output,
* that get's rendered in third party applications.
*
* @category Framework
* @package phpOMS\Contract
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
interface RenderableInterface
@ -38,5 +36,4 @@ interface RenderableInterface
* @since 1.0.0
*/
public function render() : string;
}

View File

@ -4,28 +4,22 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\DataStorage\Cache;
use phpOMS\DataStorage\Cache\CacheInterface;
use phpOMS\DataStorage\Cache\FileCache;
/**
* Database connection factory.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class CacheFactory
@ -35,6 +29,7 @@ class CacheFactory
* Constructor.
*
* @since 1.0.0
* @codeCoverageIgnore
*/
private function __construct()
{

View File

@ -4,25 +4,24 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\DataStorage\Cache;
use phpOMS\Stdlib\Base\Exception\InvalidEnumValue;
/**
* Cache interface.
*
* @category Framework
* @package phpOMS\DataStorage\Cache
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
interface CacheInterface
@ -39,7 +38,7 @@ interface CacheInterface
*
* @since 1.0.0
*/
public function set($key, $value, int $expire = -1) /* : void */;
public function set($key, $value, int $expire = -1); /* : void */
/**
* Adding new data if it doesn't exist.
@ -109,7 +108,7 @@ interface CacheInterface
*
* @since 1.0.0
*/
public function setStatus(int $status) /* : void */;
public function setStatus(int $status); /* : void */
/**
* Updating existing value/key.
@ -141,5 +140,4 @@ interface CacheInterface
* @since 1.0.0
*/
public function getThreshold() : int;
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -17,8 +16,6 @@ namespace phpOMS\DataStorage\Cache;
use phpOMS\Config\OptionsInterface;
use phpOMS\Config\OptionsTrait;
use phpOMS\DataStorage\Cache\CacheFactory;
/**
* Cache class.
@ -26,10 +23,9 @@ use phpOMS\DataStorage\Cache\CacheFactory;
* Responsible for caching scalar data types and arrays.
* Caching HTML output and objects coming soon/is planned.
*
* @category Framework
* @package phpOMS\DataStorage\Cache
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class CachePool implements OptionsInterface
@ -39,12 +35,11 @@ class CachePool implements OptionsInterface
/**
* MemCache instance.
*
* @var \phpOMS\DataStorage\Cache\CacheInterface
* @var \phpOMS\DataStorage\Cache\CacheInterface[]
* @since 1.0.0
*/
private $pool = null;
/**
* Constructor.
*
@ -64,7 +59,7 @@ class CachePool implements OptionsInterface
*
* @since 1.0.0
*/
public function add(string $key = 'core', CacheInterface $cache) : bool
public function add(string $key, CacheInterface $cache) : bool
{
if (isset($this->pool[$key])) {
return false;

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -22,10 +21,9 @@ use phpOMS\Stdlib\Base\Enum;
*
* Possible caching status
*
* @category Framework
* @package phpOMS\DataStorage\Cache
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class CacheStatus extends Enum

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -22,10 +21,9 @@ use phpOMS\Stdlib\Base\Enum;
*
* Possible caching types
*
* @category Framework
* @package phpOMS\DataStorage\Cache
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class CacheType extends Enum

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -24,10 +23,9 @@ use phpOMS\System\File\Local\File;
*
* PHP Version 7.1
*
* @category Framework
* @package phpOMS\DataStorage\Cache
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class FileCache implements CacheInterface
@ -143,15 +141,13 @@ class FileCache implements CacheInterface
public function set($key, $value, int $expire = -1) /* : void */
{
if ($this->status !== CacheStatus::ACTIVE) {
return false;
return;
}
// todo: allow $key to contain / as char and create subdirectory if necessary. This is important for cleaner caching.
$path = File::sanitize($key, self::SANITIZE);
File::put($this->cachePath . '/' . trim($path, '/') . '.cache', $this->build($value, $expire));
return false;
}
/**
@ -163,8 +159,7 @@ class FileCache implements CacheInterface
return false;
}
$path = File::sanitize($key, self::SANITIZE);
$path = $this->cachePath . '/' . trim($path, '/') . '.cache';
$path = $this->getPath($key);
if (!File::exists($path)) {
File::put($path, $this->build($value, $expire));
@ -276,8 +271,7 @@ class FileCache implements CacheInterface
return null;
}
$name = File::sanitize($key, self::SANITIZE);
$path = $this->cachePath . '/' . trim($name, '/') . '.cache';
$path = $this->getPath($key);
if (!File::exists($path)) {
return null;
@ -291,7 +285,7 @@ class FileCache implements CacheInterface
}
$raw = File::get($path);
$type = $raw[0];
$type = (int) $raw[0];
$expireStart = strpos($raw, self::DELIM);
$expireEnd = strpos($raw, self::DELIM, $expireStart + 1);
@ -303,6 +297,11 @@ class FileCache implements CacheInterface
return null;
}
return $this->parseValue($type, $raw, $expireEnd);
}
private function parseValue(int $type, string $raw, int $expireEnd)
{
$value = null;
switch ($type) {
@ -343,8 +342,7 @@ class FileCache implements CacheInterface
return false;
}
$name = File::sanitize($key, self::SANITIZE);
$path = $this->cachePath . '/' . trim($name, '/') . '.cache';
$path = $this->getPath($key);
if ($expire < 0 && File::exists($path)) {
File::delete($path);
@ -353,7 +351,7 @@ class FileCache implements CacheInterface
}
if ($expire >= 0) {
$created = Directory::created($name)->getTimestamp();
$created = Directory::created(File::sanitize($key, self::SANITIZE))->getTimestamp();
$now = time();
$raw = file_get_contents($path);
$expireStart = strpos($raw, self::DELIM);
@ -385,8 +383,7 @@ class FileCache implements CacheInterface
foreach ($dir as $file) {
if ($file instanceof File) {
$created = $file->getCreatedAt()->getTimestamp();
if (
($expire >= 0 && $created + $expire < $now)
if (($expire >= 0 && $created + $expire < $now)
|| ($expire < 0 && $created + $this->getExpire($file->getContent()) < $now)
) {
File::delete($file->getPath());
@ -406,8 +403,7 @@ class FileCache implements CacheInterface
return false;
}
$name = File::sanitize($key, self::SANITIZE);
$path = $this->cachePath . '/' . trim($path, '/') . '.cache';
$path = $this->getPath($key);
if (File::exists($path)) {
File::put($path, $this->build($value, $expire));
@ -417,4 +413,10 @@ class FileCache implements CacheInterface
return false;
}
private function getPath($key) : string
{
$path = File::sanitize($key, self::SANITIZE);
return $this->cachePath . '/' . trim($path, '/') . '.cache';
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,10 +17,9 @@ namespace phpOMS\DataStorage\Cache;
/**
* Memcache class.
*
* @category Framework
* @package phpOMS\DataStorage\Cache
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class MemCache implements CacheInterface
@ -43,6 +41,8 @@ class MemCache implements CacheInterface
*/
private $threshold = 10;
private $status;
/**
* Constructor.
*
@ -50,7 +50,7 @@ class MemCache implements CacheInterface
*/
public function __construct()
{
$this->memc = new self();
$this->memc = null;
}
/**
@ -174,5 +174,4 @@ class MemCache implements CacheInterface
$this->memc = null;
}
}
}

View File

@ -0,0 +1,176 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\DataStorage\Cache;
/**
* Memcache class.
*
* @package Framework
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
class MemCached implements CacheInterface
{
/**
* Memcache instance.
*
* @var \Memcache
* @since 1.0.0
*/
private $memc = null;
/**
* Only cache if data is larger than threshold (0-100).
*
* @var int
* @since 1.0.0
*/
private $threshold = 10;
private $status;
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct()
{
$this->memc = null;
}
/**
* Adding server to server pool.
*
* @param mixed $data Server data array
*
* @return void
*
* @since 1.0.0
*/
public function addServer($data)
{
$this->memc->addServer($data['host'], $data['port'], $data['timeout']);
}
/**
* {@inheritdoc}
*/
public function set($key, $value, int $expire = -1) /* : void */
{
$this->memc->set($key, $value, false, $expire);
}
/**
* {@inheritdoc}
*/
public function add($key, $value, int $expire = -1) : bool
{
return $this->memc->add($key, $value, false, $expire);
}
/**
* {@inheritdoc}
*/
public function get($key, int $expire = -1)
{
return $this->memc->get($key);
}
/**
* {@inheritdoc}
*/
public function delete($key, int $expire = -1) : bool
{
$this->memc->delete($key);
}
/**
* {@inheritdoc}
*/
public function flush(int $expire = 0) : bool
{
$this->memc->flush();
return true;
}
/**
* {@inheritdoc}
*/
public function flushAll() : bool
{
$this->memc->flush();
return true;
}
/**
* {@inheritdoc}
*/
public function replace($key, $value, int $expire = -1) : bool
{
$this->memc->replace($key, $value, false, $expire);
}
/**
* {@inheritdoc}
*/
public function stats() : array
{
/** @noinspection PhpMethodOrClassCallIsNotCaseSensitiveInspection */
return $this->memc->getExtendedStats();
}
/**
* {@inheritdoc}
*/
public function getThreshold() : int
{
return $this->threshold;
}
/**
* {@inheritdoc}
*/
public function setStatus(int $status) /* : void */
{
$this->status = $status;
}
/**
* Destructor.
*
* @since 1.0.0
*/
public function __destruct()
{
$this->close();
}
/**
* Closing cache.
*
* @since 1.0.0
*/
public function close()
{
if ($this->memc !== null) {
$this->memc->close();
$this->memc = null;
}
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,10 +17,9 @@ namespace phpOMS\DataStorage\Cache;
/**
* Null cache class.
*
* @category Framework
* @package phpOMS\DataStorage\Cache
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class NullCache implements CacheInterface
@ -32,7 +30,6 @@ class NullCache implements CacheInterface
*/
public function set($key, $value, int $expire = -1) /* : void */
{
// TODO: Implement set() method.
}
/**
@ -40,7 +37,7 @@ class NullCache implements CacheInterface
*/
public function add($key, $value, int $expire = -1) : bool
{
// TODO: Implement add() method.
return true;
}
/**
@ -48,7 +45,7 @@ class NullCache implements CacheInterface
*/
public function get($key, int $expire = -1)
{
// TODO: Implement get() method.
return null;
}
/**
@ -56,7 +53,7 @@ class NullCache implements CacheInterface
*/
public function delete($key, int $expire = -1) : bool
{
// TODO: Implement delete() method.
return true;
}
/**
@ -64,8 +61,6 @@ class NullCache implements CacheInterface
*/
public function flush(int $expire = 0) : bool
{
// TODO: Implement flush() method.
return true;
}
@ -74,8 +69,6 @@ class NullCache implements CacheInterface
*/
public function flushAll() : bool
{
// TODO: Implement flush() method.
return true;
}
@ -84,7 +77,7 @@ class NullCache implements CacheInterface
*/
public function replace($key, $value, int $expire = -1) : bool
{
// TODO: Implement replace() method.
return true;
}
/**
@ -92,7 +85,7 @@ class NullCache implements CacheInterface
*/
public function stats() : array
{
// TODO: Implement stats() method.
return [];
}
/**
@ -100,7 +93,7 @@ class NullCache implements CacheInterface
*/
public function getThreshold() : int
{
// TODO: Implement getThreshold() method.
return 0;
}
/**
@ -108,6 +101,5 @@ class NullCache implements CacheInterface
*/
public function setStatus(int $status) /* : void */
{
// TODO: Implement setStatus() method.
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ namespace phpOMS\DataStorage\Cache;
*
* PHP Version 5.6
*
* @category Framework
* @package phpOMS\DataStorage\Cache
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class RedisCache implements CacheInterface

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ namespace phpOMS\DataStorage\Cache;
*
* PHP Version 5.6
*
* @category Framework
* @package phpOMS\DataStorage\Cache
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class WinCache implements CacheInterface

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\DataStorage\LockException;
/**
* CookieJar class
*
* @category Framework
* @package phpOMS\Utils
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class CookieJar
@ -47,6 +45,8 @@ class CookieJar
* Constructor.
*
* @since 1.0.0
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function __construct()
{

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -22,10 +21,9 @@ use phpOMS\DataStorage\Database\Query\Builder;
*
* DB, Cache, Session
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
interface DataMapperInterface
@ -47,11 +45,11 @@ interface DataMapperInterface
*
* @param mixed $obj Object reference (gets filled with insert id)
*
* @return int Status
* @return mixed
*
* @since 1.0.0
*/
public static function update($obj) : int;
public static function update($obj);
/**
* Delete data.
@ -129,5 +127,4 @@ interface DataMapperInterface
* @since 1.0.0
*/
public static function get($primaryKey);
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
/**
* Database query builder.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class BuilderAbstract
@ -60,6 +58,14 @@ abstract class BuilderAbstract
*/
protected $prefix = '';
/**
* Raw.
*
* @var string
* @since 1.0.0
*/
public $raw = '';
/**
* Set prefix.
*

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -25,10 +24,9 @@ use phpOMS\DataStorage\Database\Schema\Grammar\Grammar as SchemaGrammar;
* Handles the database connection.
* Implementing wrapper functions for multiple databases is planned (far away).
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class ConnectionAbstract implements ConnectionInterface
@ -65,15 +63,15 @@ abstract class ConnectionAbstract implements ConnectionInterface
/**
* Database type.
*
* @var \phpOMS\DataStorage\Database\DatabaseType
* @var string
* @since 1.0.0
*/
protected $type = null;
protected $type = 'undefined';
/**
* Database status.
*
* @var DatabaseStatus
* @var int
* @since 1.0.0
*/
protected $status = DatabaseStatus::CLOSED;
@ -202,5 +200,4 @@ abstract class ConnectionAbstract implements ConnectionInterface
$this->con = null;
$this->status = DatabaseStatus::CLOSED;
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\DataStorage\Database\DatabaseType;
/**
* Database connection factory.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class ConnectionFactory
@ -57,10 +55,8 @@ class ConnectionFactory
switch ($dbdata['db']) {
case DatabaseType::MYSQL:
return new MysqlConnection($dbdata);
break;
case DatabaseType::SQLSRV:
return new SqlServerConnection($dbdata);
break;
default:
throw new \InvalidArgumentException('Database "' . $dbdata['db'] . '" is not supported.');
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -21,10 +20,9 @@ use phpOMS\DataStorage\Database\Schema\Grammar\Grammar as SchemaGrammar;
/**
* Database connection interface.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
interface ConnectionInterface
@ -41,7 +39,7 @@ interface ConnectionInterface
*
* @since 1.0.0
*/
public function connect(array $dbdata) /* : void */;
public function connect(array $dbdata); /* : void */
/**
* Get the database type.
@ -68,7 +66,7 @@ interface ConnectionInterface
*
* @since 1.0.0
*/
public function close() /* : void */;
public function close(); /* : void */
/**
* Return grammar for this connection.
@ -87,5 +85,4 @@ interface ConnectionInterface
* @since 1.0.0
*/
public function getSchemaGrammar() : SchemaGrammar;
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -27,10 +26,9 @@ use phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException;
* Handles the database connection.
* Implementing wrapper functions for multiple databases is planned (far away).
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class MysqlConnection extends ConnectionAbstract
@ -60,32 +58,11 @@ class MysqlConnection extends ConnectionAbstract
{
$this->dbdata = isset($dbdata) ? $dbdata : $this->dbdata;
if(!isset($this->dbdata['db'])) {
throw new InvalidConnectionConfigException('db');
}
if(!isset($this->dbdata['host'])) {
throw new InvalidConnectionConfigException('host');
}
if(!isset($this->dbdata['port'])) {
throw new InvalidConnectionConfigException('port');
}
if(!isset($this->dbdata['database'])) {
throw new InvalidConnectionConfigException('database');
}
if(!isset($this->dbdata['login'])) {
throw new InvalidConnectionConfigException('login');
}
if(!isset($this->dbdata['password'])) {
throw new InvalidConnectionConfigException('password');
if (!isset($this->dbdata['db'], $this->dbdata['host'], $this->dbdata['port'], $this->dbdata['database'], $this->dbdata['login'], $this->dbdata['password'])) {
throw new InvalidConnectionConfigException(json_encode($this->dbdata));
}
$this->close();
$this->prefix = $dbdata['prefix'] ?? '';
try {
@ -101,5 +78,4 @@ class MysqlConnection extends ConnectionAbstract
$this->dbdata['password'] = '****';
}
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -25,10 +24,9 @@ use phpOMS\DataStorage\Database\Query\Grammar\SqliteGrammar;
* Handles the database connection.
* Implementing wrapper functions for multiple databases is planned (far away).
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class SqliteConnection extends ConnectionAbstract
@ -71,5 +69,4 @@ class SqliteConnection extends ConnectionAbstract
$this->con = null;
}
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -26,10 +25,9 @@ use phpOMS\DataStorage\Database\Schema\Grammar\MysqlGrammar as MysqlSchemaGramma
* Handles the database connection.
* Implementing wrapper functions for multiple databases is planned (far away).
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class SqlServerConnection extends ConnectionAbstract

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -26,10 +25,9 @@ use phpOMS\DataStorage\Database\Exception\InvalidMapperException;
*
* DB, Cache, Session
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class DataMapperAbstract implements DataMapperInterface
@ -72,7 +70,7 @@ class DataMapperAbstract implements DataMapperInterface
* @var string
* @since 1.0.0
*/
protected static $language_field = '';
protected static $languageField = '';
/**
* Columns.
@ -149,7 +147,7 @@ class DataMapperAbstract implements DataMapperInterface
/**
* Highest mapper to know when to clear initialized objects
*
* @var DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static $parentMapper = null;
@ -303,7 +301,7 @@ class DataMapperAbstract implements DataMapperInterface
// clear parent and objects
if (static::class === self::$parentMapper) {
self::$initObjects = [];
//self::$initObjects = []; // todo: now all objects are cached for the whole request
self::$parentMapper = null;
}
}
@ -346,13 +344,11 @@ class DataMapperAbstract implements DataMapperInterface
{
self::extend(__CLASS__);
if($obj === null ||
(is_object($obj) && strpos($className = get_class($obj), '\Null') !== false)
) {
if (!isset($obj) || self::isNullObject($obj)) {
return null;
}
$reflectionClass = new \ReflectionClass($className);
$reflectionClass = new \ReflectionClass($obj);
$objId = self::createModel($obj, $reflectionClass);
self::setObjectId($reflectionClass, $obj, $objId);
@ -505,7 +501,7 @@ class DataMapperAbstract implements DataMapperInterface
*/
private static function getObjectId($obj, \ReflectionClass $reflectionClass = null)
{
$reflectionClass = $reflectionClass ?? new \ReflectionClass(get_class($obj));
$reflectionClass = $reflectionClass ?? new \ReflectionClass($obj);
$reflectionProperty = $reflectionClass->getProperty(static::$columns[static::$primaryField]['internal']);
if (!($isPublic = $reflectionProperty->isPublic())) {
@ -594,7 +590,7 @@ class DataMapperAbstract implements DataMapperInterface
}
if (!isset($relReflectionClass)) {
$relReflectionClass = new \ReflectionClass(get_class($value));
$relReflectionClass = new \ReflectionClass($value);
}
$primaryKey = $mapper::getObjectId($value, $relReflectionClass);
@ -689,10 +685,6 @@ class DataMapperAbstract implements DataMapperInterface
private static function createHasOne(\ReflectionClass $reflectionClass, $obj)
{
throw new \Exception();
foreach (static::$hasOne as $propertyName => $rel) {
}
}
/**
@ -825,8 +817,7 @@ class DataMapperAbstract implements DataMapperInterface
private static function createRelationTable(string $propertyName, array $objsIds, $objId)
{
/** @var string $table */
if (
!empty($objsIds)
if (!empty($objsIds)
&& static::$hasMany[$propertyName]['table'] !== static::$table
&& static::$hasMany[$propertyName]['table'] !== static::$hasMany[$propertyName]['mapper']::$table
) {
@ -929,7 +920,7 @@ class DataMapperAbstract implements DataMapperInterface
}
if (!isset($relReflectionClass)) {
$relReflectionClass = new \ReflectionClass(get_class($value));
$relReflectionClass = new \ReflectionClass($value);
}
$primaryKey = $mapper::getObjectId($value, $relReflectionClass);
@ -972,14 +963,11 @@ class DataMapperAbstract implements DataMapperInterface
*
* Deletes old entries and creates new ones
*
* @param string $propertyName Property name to initialize
* @param array $objsIds Object ids to insert
* @param mixed $objId Model to reference
*
* @return mixed
*
* @throws \Exception
*
* @since 1.0.0
*/
private static function updateRelationTable(array $objsIds, $objId)
@ -1014,8 +1002,7 @@ class DataMapperAbstract implements DataMapperInterface
private static function deleteRelationTable(string $propertyName, array $objsIds, $objId)
{
/** @var string $table */
if (
!empty($objsIds)
if (!empty($objsIds)
&& static::$hasMany[$propertyName]['table'] !== static::$table
&& static::$hasMany[$propertyName]['table'] !== static::$hasMany[$propertyName]['mapper']::$table
) {
@ -1152,18 +1139,25 @@ class DataMapperAbstract implements DataMapperInterface
* @param mixed $obj Object reference (gets filled with insert id)
* @param int $relations Create all relations as well
*
* @return int
* @return mixed
*
* @since 1.0.0
*/
public static function update($obj, int $relations = RelationType::ALL) : int
public static function update($obj, int $relations = RelationType::ALL)
{
self::extend(__CLASS__);
$reflectionClass = new \ReflectionClass(get_class($obj));
if (!isset($obj) || self::isNullObject($obj)) {
return null;
}
$reflectionClass = new \ReflectionClass($obj);
$objId = self::getObjectId($obj, $reflectionClass);
$update = true;
// todo: maybe don't remove obj and just update cache... ? since it might have to be loaded again
self::removeInitialized(static::class, $objId);
if (empty($objId)) {
$update = false;
self::create($obj, $relations);
@ -1227,7 +1221,7 @@ class DataMapperAbstract implements DataMapperInterface
}
if (!isset($relReflectionClass)) {
$relReflectionClass = new \ReflectionClass(get_class($value));
$relReflectionClass = new \ReflectionClass($value);
}
$primaryKey = $mapper::getObjectId($value, $relReflectionClass);
@ -1370,13 +1364,15 @@ class DataMapperAbstract implements DataMapperInterface
{
self::extend(__CLASS__);
$reflectionClass = new \ReflectionClass(get_class($obj));
$reflectionClass = new \ReflectionClass($obj);
$objId = self::getObjectId($obj, $reflectionClass);
if (empty($objId)) {
return null;
}
self::removeInitialized(static::class, $objId);
if ($relations !== RelationType::NONE) {
self::deleteHasMany($reflectionClass, $obj, $objId, $relations);
}
@ -1468,15 +1464,16 @@ class DataMapperAbstract implements DataMapperInterface
*
* @param array[] $result Result set
* @param mixed $obj Object to add the relations to
* @param int $depth Relation depth
*
* @return void
*
* @since 1.0.0
*/
public static function populateManyToMany(array $result, &$obj) /* : void */
public static function populateManyToMany(array $result, &$obj, int $depth = null) /* : void */
{
// todo: maybe pass reflectionClass as optional parameter for performance increase
$reflectionClass = new \ReflectionClass(get_class($obj));
$reflectionClass = new \ReflectionClass($obj);
foreach ($result as $member => $values) {
if (!empty($values) && $reflectionClass->hasProperty($member)) {
@ -1484,17 +1481,12 @@ class DataMapperAbstract implements DataMapperInterface
$mapper = static::$hasMany[$member]['mapper'];
$reflectionProperty = $reflectionClass->getProperty($member);
$values = array_diff($values, array_keys(self::$initObjects[$mapper] ?? []));
if(empty($values)) {
continue;
}
if (!($accessible = $reflectionProperty->isPublic())) {
$reflectionProperty->setAccessible(true);
}
$objects = $mapper::get($values);
$reflectionProperty->setValue($obj, !is_array($objects) ? [$objects] : $objects);
$objects = $mapper::get($values, RelationType::ALL, null, $depth);
$reflectionProperty->setValue($obj, !is_array($objects) ? [$objects->getId() => $objects] : $objects);
if (!$accessible) {
$reflectionProperty->setAccessible(false);
@ -1508,24 +1500,20 @@ class DataMapperAbstract implements DataMapperInterface
*
* @param array[] $result Result set
* @param array $obj Object to add the relations to
* @param int $depth Relation depth
*
* @return void
*
* @since 1.0.0
*/
public static function populateManyToManyArray(array $result, array &$obj) /* : void */
public static function populateManyToManyArray(array $result, array &$obj, int $depth = null) /* : void */
{
foreach ($result as $member => $values) {
if (!empty($values)) {
/** @var string $mapper */
$mapper = static::$hasMany[$member]['mapper'];
$values = array_diff($values, array_keys(self::$initObjects[$mapper] ?? []));
if(empty($values)) {
continue;
}
$objects = $mapper::getArray($values);
$objects = $mapper::getArray($values, RelationType::ALL, $depth);
$obj[$member] = $objects;
}
}
@ -1535,6 +1523,7 @@ class DataMapperAbstract implements DataMapperInterface
* Populate data.
*
* @param mixed $obj Object to add the relations to
* @param int $depth Relation depth
*
* @return void
*
@ -1542,9 +1531,9 @@ class DataMapperAbstract implements DataMapperInterface
*
* @since 1.0.0
*/
public static function populateHasOne(&$obj) /* : void */
public static function populateHasOne(&$obj, int $depth = null) /* : void */
{
$reflectionClass = new \ReflectionClass(get_class($obj));
$reflectionClass = new \ReflectionClass($obj);
foreach (static::$hasOne as $member => $one) {
// todo: is that if necessary? performance is suffering for sure!
@ -1557,13 +1546,15 @@ class DataMapperAbstract implements DataMapperInterface
/** @var string $mapper */
$mapper = static::$hasOne[$member]['mapper'];
$id = $reflectionProperty->getValue($obj);
if(self::isInitialized($mapper, ($id = $reflectionProperty->getValue($obj)))) {
$value = self::$initObjects[$mapper][$id];
} else {
$value = $mapper::get($reflectionProperty->getValue($obj));
if (self::isNullObject($id)) {
continue;
}
$id = is_object($id) ? self::getObjectId($id) : $id;
$value = self::getInitialized($mapper, $id) ?? $mapper::get($id, RelationType::ALL, null, $depth);
$reflectionProperty->setValue($obj, $value);
if (!$accessible) {
@ -1577,6 +1568,7 @@ class DataMapperAbstract implements DataMapperInterface
* Populate data.
*
* @param array $obj Object to add the relations to
* @param int $depth Relation depth
*
* @return void
*
@ -1584,19 +1576,12 @@ class DataMapperAbstract implements DataMapperInterface
*
* @since 1.0.0
*/
public static function populateHasOneArray(array &$obj) /* : void */
public static function populateHasOneArray(array &$obj, int $depth = null) /* : void */
{
foreach (static::$hasOne as $member => $one) {
/** @var string $mapper */
$mapper = static::$hasOne[$member]['mapper'];
if(self::isInitialized($mapper, $obj['member'])) {
$value = self::$initObjects[$mapper][$obj['member']];
} else {
$value = $mapper::getArray($obj[$member]);
}
$obj[$member] = $value;
$obj[$member] = self::getInitialized($mapper, $obj['member']) ?? $mapper::getArray($obj[$member], RelationType::ALL, $depth);
}
}
@ -1604,6 +1589,7 @@ class DataMapperAbstract implements DataMapperInterface
* Populate data.
*
* @param mixed $obj Object to add the relations to
* @param int $depth Relation depth
*
* @return void
*
@ -1611,9 +1597,9 @@ class DataMapperAbstract implements DataMapperInterface
*
* @since 1.0.0
*/
public static function populateOwnsOne(&$obj) /* : void */
public static function populateOwnsOne(&$obj, int $depth = null) /* : void */
{
$reflectionClass = new \ReflectionClass(get_class($obj));
$reflectionClass = new \ReflectionClass($obj);
foreach (static::$ownsOne as $member => $one) {
// todo: is that if necessary? performance is suffering for sure!
@ -1626,13 +1612,15 @@ class DataMapperAbstract implements DataMapperInterface
/** @var string $mapper */
$mapper = static::$ownsOne[$member]['mapper'];
$id = $reflectionProperty->getValue($obj);
if(self::isInitialized($mapper, ($id = $reflectionProperty->getValue($obj)))) {
$value = self::$initObjects[$mapper][$id];
} else {
$value = $mapper::get($reflectionProperty->getValue($obj));
if (self::isNullObject($id)) {
continue;
}
$id = is_object($id) ? self::getObjectId($id) : $id;
$value = self::getInitialized($mapper, $id) ?? $mapper::get($id, RelationType::ALL, null, $depth);
$reflectionProperty->setValue($obj, $value);
if (!$accessible) {
@ -1646,6 +1634,7 @@ class DataMapperAbstract implements DataMapperInterface
* Populate data.
*
* @param array $obj Object to add the relations to
* @param int $depth Relation depth
*
* @return void
*
@ -1653,19 +1642,12 @@ class DataMapperAbstract implements DataMapperInterface
*
* @since 1.0.0
*/
public static function populateOwnsOneArray(array &$obj) /* : void */
public static function populateOwnsOneArray(array &$obj, int $depth = null) /* : void */
{
foreach (static::$ownsOne as $member => $one) {
/** @var string $mapper */
$mapper = static::$ownsOne[$member]['mapper'];
if(self::isInitialized($mapper, $obj[$member])) {
$value = self::$initObjects[$mapper][$obj[$member]];
} else {
$value = $mapper::getArray($obj[$member]);
}
$obj[$member] = $value;
$obj[$member] = self::getInitialized($mapper, $obj[$member]) ?? $mapper::getArray($obj[$member], RelationType::ALL, $depth);
}
}
@ -1673,6 +1655,7 @@ class DataMapperAbstract implements DataMapperInterface
* Populate data.
*
* @param mixed $obj Object to add the relations to
* @param int $depth Relation depth
*
* @return void
*
@ -1680,9 +1663,9 @@ class DataMapperAbstract implements DataMapperInterface
*
* @since 1.0.0
*/
public static function populateBelongsTo(&$obj) /* : void */
public static function populateBelongsTo(&$obj, int $depth = null) /* : void */
{
$reflectionClass = new \ReflectionClass(get_class($obj));
$reflectionClass = new \ReflectionClass($obj);
foreach (static::$belongsTo as $member => $one) {
// todo: is that if necessary? performance is suffering for sure!
@ -1695,13 +1678,15 @@ class DataMapperAbstract implements DataMapperInterface
/** @var string $mapper */
$mapper = static::$belongsTo[$member]['mapper'];
$id = $reflectionProperty->getValue($obj);
if(self::isInitialized($mapper, ($id = $reflectionProperty->getValue($obj)))) {
$value = self::$initObjects[$mapper][$id];
} else {
$value = $mapper::get($reflectionProperty->getValue($obj));
if (self::isNullObject($id)) {
continue;
}
$id = is_object($id) ? self::getObjectId($id) : $id;
$value = self::getInitialized($mapper, $id) ?? $mapper::get($id, RelationType::ALL, null, $depth);
$reflectionProperty->setValue($obj, $value);
if (!$accessible) {
@ -1715,6 +1700,7 @@ class DataMapperAbstract implements DataMapperInterface
* Populate data.
*
* @param array $obj Object to add the relations to
* @param int $depth Relation depth
*
* @return void
*
@ -1722,19 +1708,12 @@ class DataMapperAbstract implements DataMapperInterface
*
* @since 1.0.0
*/
public static function populateBelongsToArray(array &$obj) /* : void */
public static function populateBelongsToArray(array &$obj, int $depth = null) /* : void */
{
foreach (static::$belongsTo as $member => $one) {
/** @var string $mapper */
$mapper = static::$belongsTo[$member]['mapper'];
if(self::isInitialized($mapper, $obj[$member])) {
$value = self::$initObjects[$mapper][$obj[$member]];
} else {
$value = $mapper::get($obj[$member]);
}
$obj[$member] = $value;
$obj[$member] = self::getInitialized($mapper, $obj[$member]) ?? $mapper::get($obj[$member], RelationType::ALL, null, $depth);
}
}
@ -1752,10 +1731,13 @@ class DataMapperAbstract implements DataMapperInterface
*/
public static function populateAbstract(array $result, $obj)
{
$reflectionClass = new \ReflectionClass(get_class($obj));
$reflectionClass = new \ReflectionClass($obj);
foreach ($result as $column => $value) {
if (isset(static::$columns[$column]['internal']) /* && $reflectionClass->hasProperty(static::$columns[$column]['internal']) */) {
if (!isset(static::$columns[$column]['internal']) /* && $reflectionClass->hasProperty(static::$columns[$column]['internal']) */) {
continue;
}
$reflectionProperty = $reflectionClass->getProperty(static::$columns[$column]['internal']);
if (!($accessible = $reflectionProperty->isPublic())) {
@ -1783,7 +1765,6 @@ class DataMapperAbstract implements DataMapperInterface
$reflectionProperty->setAccessible(false);
}
}
}
return $obj;
}
@ -1829,13 +1810,20 @@ class DataMapperAbstract implements DataMapperInterface
* @param mixed $primaryKey Key
* @param int $relations Load relations
* @param mixed $fill Object to fill
* @param int $depth Relation depth
*
* @return mixed
*
* @todo: implement language
*
* @since 1.0.0
*/
public static function get($primaryKey, int $relations = RelationType::ALL, $fill = null)
public static function get($primaryKey, int $relations = RelationType::ALL, $fill = null, int $depth = null)
{
if (isset($depth) && $depth < 1) {
return $primaryKey;
}
if (!isset(self::$parentMapper)) {
self::setUpParentMapper();
}
@ -1850,6 +1838,7 @@ class DataMapperAbstract implements DataMapperInterface
foreach ($primaryKey as $key => $value) {
if (self::isInitialized(static::class, $value)) {
$obj[$value] = self::$initObjects[static::class][$value];
continue;
}
@ -1864,10 +1853,10 @@ class DataMapperAbstract implements DataMapperInterface
$obj[$value]->initialize();
}
self::addInitialized(static::class, $value);
self::addInitialized(static::class, $value, $obj[$value]);
}
self::fillRelations($obj, $relations);
self::fillRelations($obj, $relations, isset($depth) ? --$depth : null);
self::clear();
$countResulsts = count($obj);
@ -1881,6 +1870,13 @@ class DataMapperAbstract implements DataMapperInterface
return $obj;
}
/**
* Creates the current null object
*
* @return mixed
*
* @since 1.0.0
*/
private static function getNullModelObj()
{
$class = static::class;
@ -1898,13 +1894,18 @@ class DataMapperAbstract implements DataMapperInterface
*
* @param mixed $primaryKey Key
* @param int $relations Load relations
* @param int $depth Relation depth
*
* @return mixed
*
* @since 1.0.0
*/
public static function getArray($primaryKey, int $relations = RelationType::ALL) : array
public static function getArray($primaryKey, int $relations = RelationType::ALL, int $depth = null) : array
{
if (isset($depth) && $depth < 1) {
return $primaryKey;
}
if (!isset(self::$parentMapper)) {
self::setUpParentMapper();
}
@ -1916,15 +1917,16 @@ class DataMapperAbstract implements DataMapperInterface
foreach ($primaryKey as $key => $value) {
if (self::isInitialized(static::class, $value)) {
$obj[$value] = self::$initObjects[static::class][$value];
continue;
}
$obj[$value] = self::populateAbstractArray(self::getRaw($value));
self::addInitialized(static::class, $value);
self::addInitialized(static::class, $value, $obj[$value]);
}
self::fillRelationsArray($obj, $relations);
self::fillRelationsArray($obj, $relations, isset($depth) ? --$depth : null);
self::clear();
return count($obj) === 1 ? reset($obj) : $obj;
@ -1937,13 +1939,18 @@ class DataMapperAbstract implements DataMapperInterface
* @param string $ref The field that defines the for
* @param int $relations Load relations
* @param mixed $fill Object to fill
* @param int $depth Relation depth
*
* @return mixed
*
* @since 1.0.0
*/
public static function getFor($refKey, string $ref, int $relations = RelationType::ALL, $fill = null)
public static function getFor($refKey, string $ref, int $relations = RelationType::ALL, $fill = null, int $depth = null)
{
if (isset($depth) && $depth < 1) {
return $refKey;
}
if (!isset(self::$parentMapper)) {
self::setUpParentMapper();
}
@ -1962,7 +1969,7 @@ class DataMapperAbstract implements DataMapperInterface
$toLoad = self::getPrimaryKeysBy($value, self::getColumnByMember($ref));
}
$obj[$value] = self::get($toLoad, $relations, $fill);
$obj[$value] = self::get($toLoad, $relations, $fill, isset($depth) ? --$depth : null);
}
$countResulsts = count($obj);
@ -2018,20 +2025,25 @@ class DataMapperAbstract implements DataMapperInterface
* Get object.
*
* @param int $relations Load relations
* @param int $depth Relation depth
* @param string $lang Language
*
* @return array
*
* @since 1.0.0
*/
public static function getAll(int $relations = RelationType::ALL, string $lang = '') : array
public static function getAll(int $relations = RelationType::ALL, int $depth = null, string $lang = '') : array
{
if (isset($depth) && $depth < 1) {
return null;
}
if (!isset(self::$parentMapper)) {
self::setUpParentMapper();
}
$obj = self::populateIterable(self::getAllRaw($lang));
self::fillRelations($obj, $relations);
self::fillRelations($obj, $relations, isset($depth) ? --$depth : null);
self::clear();
return $obj;
@ -2041,20 +2053,25 @@ class DataMapperAbstract implements DataMapperInterface
* Get object.
*
* @param int $relations Load relations
* @param int $depth Relation depth
* @param string $lang Language
*
* @return array
*
* @since 1.0.0
*/
public static function getAllArray(int $relations = RelationType::ALL, string $lang = '') : array
public static function getAllArray(int $relations = RelationType::ALL, int $depth = null, string $lang = '') : array
{
if (isset($depth) && $depth < 1) {
return null;
}
if (!isset(self::$parentMapper)) {
self::setUpParentMapper();
}
$obj = self::populateIterableArray(self::getAllRaw($lang));
self::fillRelationsArray($obj, $relations);
self::fillRelationsArray($obj, $relations, isset($depth) ? --$depth : null);
self::clear();
return $obj;
@ -2085,14 +2102,19 @@ class DataMapperAbstract implements DataMapperInterface
* @param int $limit Newest limit
* @param Builder $query Pre-defined query
* @param int $relations Load relations
* @param int $depth Relation depth
* @param string $lang Language
*
* @return mixed
*
* @since 1.0.0
*/
public static function getNewest(int $limit = 1, Builder $query = null, int $relations = RelationType::ALL, string $lang = '') : array
public static function getNewest(int $limit = 1, Builder $query = null, int $relations = RelationType::ALL, int $depth = null, string $lang = '') : array
{
if (isset($depth) && $depth < 1) {
return null;
}
self::extend(__CLASS__);
$query = $query ?? new Builder(self::$db);
@ -2105,8 +2127,8 @@ class DataMapperAbstract implements DataMapperInterface
$query->orderBy(static::$table . '.' . static::$columns[static::$primaryField]['name'], 'DESC');
}
if (!empty(self::$language_field) && !empty($lang)) {
$query->where(static::$table . '.' . static::$language_field, '=', $lang, 'AND');
if (!empty(self::$languageField) && !empty($lang)) {
$query->where(static::$table . '.' . static::$languageField, '=', $lang, 'AND');
}
$sth = self::$db->con->prepare($query->toSql());
@ -2115,7 +2137,7 @@ class DataMapperAbstract implements DataMapperInterface
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
$obj = self::populateIterable(is_bool($results) ? [] : $results);
self::fillRelations($obj, $relations);
self::fillRelations($obj, $relations, isset($depth) ? --$depth : null);
self::clear();
return $obj;
@ -2127,13 +2149,18 @@ class DataMapperAbstract implements DataMapperInterface
*
* @param Builder $query Query
* @param int $relations Relations
* @param int $depth Relation depth
*
* @return array
*
* @since 1.0.0
*/
public static function getAllByQuery(Builder $query, int $relations = RelationType::ALL) : array
public static function getAllByQuery(Builder $query, int $relations = RelationType::ALL, int $depth = null) : array
{
if (isset($depth) && $depth < 1) {
return null;
}
$sth = self::$db->con->prepare($query->toSql());
$sth->execute();
@ -2141,7 +2168,7 @@ class DataMapperAbstract implements DataMapperInterface
$results = is_bool($results) ? [] : $results;
$obj = self::populateIterable($results);
self::fillRelations($obj, $relations);
self::fillRelations($obj, $relations, isset($depth) ? --$depth : null);
self::clear();
return $obj;
@ -2152,13 +2179,18 @@ class DataMapperAbstract implements DataMapperInterface
*
* @param int $amount Amount of random models
* @param int $relations Relations type
* @param int $depth Relation depth
*
* @return mixed
*
* @since 1.0.0
*/
public static function getRandom(int $amount = 1, int $relations = RelationType::ALL)
public static function getRandom(int $amount = 1, int $relations = RelationType::ALL, int $depth = null)
{
if (isset($depth) && $depth < 1) {
return null;
}
$query = new Builder(self::$db);
$query->prefix(self::$db->getPrefix())
->random(static::$primaryField)
@ -2168,7 +2200,7 @@ class DataMapperAbstract implements DataMapperInterface
$sth = self::$db->con->prepare($query->toSql());
$sth->execute();
return self::get($sth->fetchAll(), $relations);
return self::get($sth->fetchAll(), $relations, null, $depth);
}
/**
@ -2176,36 +2208,47 @@ class DataMapperAbstract implements DataMapperInterface
*
* @param mixed $obj Objects to fill
* @param int $relations Relations type
* @param int $depth Relation depth
*
* @return void
*
* @since 1.0.0
*/
public static function fillRelations(array &$obj, int $relations = RelationType::ALL) /* : void */
public static function fillRelations(array &$obj, int $relations = RelationType::ALL, int $depth = null) /* : void */
{
if (isset($depth) && $depth < 1) {
return;
}
if ($relations === RelationType::NONE) {
return;
}
$hasMany = !empty(static::$hasMany);
$hasOne = !empty(static::$hasOne);
$ownsOne = !empty(static::$ownsOne);
$belongsTo = !empty(static::$belongsTo);
if ($relations !== RelationType::NONE && ($hasMany || $hasOne || $ownsOne || $belongsTo)) {
if (!($hasMany || $hasOne || $ownsOne || $belongsTo)) {
return;
}
foreach ($obj as $key => $value) {
/* loading relations from relations table and populating them and then adding them to the object */
if ($hasMany) {
self::populateManyToMany(self::getHasManyRaw($key, $relations), $obj[$key]);
self::populateManyToMany(self::getHasManyRaw($key, $relations), $obj[$key], $depth);
}
if ($hasOne) {
self::populateHasOne($obj[$key]);
self::populateHasOne($obj[$key], $depth);
}
if ($ownsOne) {
self::populateOwnsOne($obj[$key]);
self::populateOwnsOne($obj[$key], $depth);
}
if ($belongsTo) {
self::populateBelongsTo($obj[$key]);
}
self::populateBelongsTo($obj[$key], $depth);
}
}
}
@ -2215,36 +2258,47 @@ class DataMapperAbstract implements DataMapperInterface
*
* @param mixed $obj Objects to fill
* @param int $relations Relations type
* @param int $depth Relation depth
*
* @return void
*
* @since 1.0.0
*/
public static function fillRelationsArray(array &$obj, int $relations = RelationType::ALL) /* : void */
public static function fillRelationsArray(array &$obj, int $relations = RelationType::ALL, int $depth = null) /* : void */
{
if (isset($depth) && $depth < 1) {
return null;
}
if ($relations !== RelationType::NONE) {
return;
}
$hasMany = !empty(static::$hasMany);
$hasOne = !empty(static::$hasOne);
$ownsOne = !empty(static::$ownsOne);
$belongsTo = !empty(static::$belongsTo);
if ($relations !== RelationType::NONE && ($hasMany || $hasOne || $ownsOne || $belongsTo)) {
if (!($hasMany || $hasOne || $ownsOne || $belongsTo)) {
return;
}
foreach ($obj as $key => $value) {
/* loading relations from relations table and populating them and then adding them to the object */
if ($hasMany) {
self::populateManyToManyArray(self::getHasManyRaw($key, $relations), $obj[$key]);
self::populateManyToManyArray(self::getHasManyRaw($key, $relations), $obj[$key], $depth);
}
if ($hasOne) {
self::populateHasOneArray($obj[$key]);
self::populateHasOneArray($obj[$key], $depth);
}
if ($ownsOne) {
self::populateOwnsOneArray($obj[$key]);
self::populateOwnsOneArray($obj[$key], $depth);
}
if ($belongsTo) {
self::populateBelongsToArray($obj[$key]);
}
self::populateBelongsToArray($obj[$key], $depth);
}
}
}
@ -2336,8 +2390,8 @@ class DataMapperAbstract implements DataMapperInterface
{
$query = self::getQuery();
if (!empty(self::$language_field) && !empty($lang)) {
$query->where(static::$table . '.' . static::$language_field, '=', $lang, 'AND');
if (!empty(self::$languageField) && !empty($lang)) {
$query->where(static::$table . '.' . static::$languageField, '=', $lang, 'AND');
}
$sth = self::$db->con->prepare($query->toSql());
@ -2446,8 +2500,8 @@ class DataMapperAbstract implements DataMapperInterface
public static function getByRequest(RequestAbstract $request)
{
if (!is_null($request->getData('id'))) {
$result = static::get($request->getData('id'));
} elseif (!is_null($filter = $request->getData('filter'))) {
$result = static::get((int) $request->getData('id'));
} elseif (!is_null($filter = ((string) $request->getData('filter')))) {
$filter = strtolower($filter);
if ($filter === 'all') {
@ -2457,8 +2511,8 @@ class DataMapperAbstract implements DataMapperInterface
$result = static::get(json_decode($list, true));
} else {
$limit = (int) ($request->getData('limit') ?? 1);
$from = !is_null($request->getData('from')) ? new \DateTime($request->getData('from')) : null;
$to = !is_null($request->getData('to')) ? new \DateTime($request->getData('to')) : null;
$from = !is_null($request->getData('from')) ? new \DateTime((string) $request->getData('from')) : null;
$to = !is_null($request->getData('to')) ? new \DateTime((string) $request->getData('to')) : null;
$query = static::getQuery();
$query->limit($limit);
@ -2507,11 +2561,43 @@ class DataMapperAbstract implements DataMapperInterface
*
* @since 1.0.0
*/
private static function isInitialized($mapper, $id) : bool
private static function isInitialized(string $mapper, $id) : bool
{
return isset(self::$initObjects[$mapper]) && isset(self::$initObjects[$mapper][$id]);
}
/**
* Get initialized object
*
* @param string $mapper Mapper name
* @param mixed $id Object id
*
* @return mixed
*
* @since 1.0.0
*/
private static function getInitialized(string $mapper, $id)
{
return self::$initObjects[$mapper][$id] ?? null;
}
/**
* Remove initialized object
*
* @param string $mapper Mapper name
* @param mixed $id Object id
*
* @return mixed
*
* @since 1.0.0
*/
private static function removeInitialized(string $mapper, $id)
{
if (self::isInitialized($mapper, $id)) {
unset(self::$initObjects[$mapper][$id]);
}
}
/**
* Define the highest mapper of this request
*
@ -2570,4 +2656,18 @@ class DataMapperAbstract implements DataMapperInterface
throw new \Exception('Invalid member name');
}
/**
* Test if object is null object
*
* @param object $obj Object to check
*
* @return bool
*
* @since 1.0.0
*/
private static function isNullObject($obj) : bool
{
return is_object($obj) && strpos(get_class($obj), '\Null') !== false;
}
}

View File

@ -1,437 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
declare(strict_types=1);
namespace phpOMS\DataStorage\Database;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Database\Query\Builder;
/**
* Datamapper for databases.
*
* DB, Cache, Session
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class DataMapperBaseAbstract
{
/**
* Database connection.
*
* @var ConnectionAbstract
* @since 1.0.0
*/
protected static $db = null;
/**
* Overwriting extended values.
*
* @var bool
* @since 1.0.0
*/
protected static $overwrite = true;
/**
* Primary field name.
*
* @var string
* @since 1.0.0
*/
protected static $primaryField = '';
/**
* Primary field name.
*
* @var string
* @since 1.0.0
*/
protected static $createdAt = '';
/**
* Language
*
* @var string
* @since 1.0.0
*/
protected static $language_field = '';
/**
* Columns.
*
* @var array
* @since 1.0.0
*/
protected static $columns = [];
/**
* Relations.
*
* Relation is defined in a relation table
*
* @var string[]
* @since 1.0.0
*/
protected static $hasMany = [];
/**
* Relations.
*
* Relation is defined in the model
*
* @var string[]
* @since 1.0.0
*/
protected static $hasOne = [];
/**
* Relations.
*
* Relation is defined in current mapper
*
* @var string[]
* @since 1.0.0
*/
protected static $ownsOne = [];
/**
* Relations.
*
* Relation is defined in current mapper
*
* @var string[]
* @since 1.0.0
*/
protected static $belongsTo = [];
/**
* Table.
*
* @var string
* @since 1.0.0
*/
protected static $table = '';
/**
* Fields to load.
*
* @var array[]
* @since 1.0.0
*/
protected static $fields = [];
/**
* Initialized objects for cross reference to reduce initialization costs
*
* @var array[]
* @since 1.0.0
*/
protected static $initObjects = [];
/**
* Highest mapper to know when to clear initialized objects
*
* @var DataMapperAbstract
* @since 1.0.0
*/
protected static $parentMapper = null;
/**
* Extended value collection.
*
* @var array
* @since 1.0.0
*/
protected static $collection = [
'primaryField' => [],
'createdAt' => [],
'columns' => [],
'hasMany' => [],
'hasOne' => [],
'ownsOne' => [],
'table' => [],
];
/**
* Constructor.
*
* @since 1.0.0
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* Clone.
*
* @return void
*
* @since 1.0.0
* @codeCoverageIgnore
*/
private function __clone()
{
}
/**
* Set database connection.
*
* @param ConnectionAbstract $con Database connection
*
* @return void
*
* @since 1.0.0
*/
public static function setConnection(ConnectionAbstract $con) /* : void */
{
self::$db = $con;
}
/**
* Get primary field.
*
* @return string
*
* @since 1.0.0
*/
public static function getPrimaryField() : string
{
return static::$primaryField;
}
/**
* Get main table.
*
* @return string
*
* @since 1.0.0
*/
public static function getTable() : string
{
return static::$table;
}
/**
* Collect values from extension.
*
* @param mixed $class Current extended mapper
*
* @return void
*
* @since 1.0.0
*/
private static function extend($class) /* : void */
{
/* todo: have to implement this in the queries, so far not used */
self::$collection['primaryField'][] = $class::$primaryField;
self::$collection['createdAt'][] = $class::$createdAt;
self::$collection['columns'][] = $class::$columns;
self::$collection['hasMany'][] = $class::$hasMany;
self::$collection['hasOne'][] = $class::$hasOne;
self::$collection['ownsOne'][] = $class::$ownsOne;
self::$collection['table'][] = $class::$table;
if (($parent = get_parent_class($class)) !== false && !$class::$overwrite) {
self::extend($parent);
}
}
/**
* Resets all loaded mapper variables.
*
* This is used after one action is performed otherwise other models would use wrong settings.
*
* @return void
*
* @since 1.0.0
*/
public static function clear() /* : void */
{
self::$overwrite = true;
self::$primaryField = '';
self::$createdAt = '';
self::$columns = [];
self::$hasMany = [];
self::$hasOne = [];
self::$ownsOne = [];
self::$table = '';
self::$fields = [];
self::$collection = [
'primaryField' => [],
'createdAt' => [],
'columns' => [],
'hasOne' => [],
'ownsMany' => [],
'ownsOne' => [],
'table' => [],
];
// clear parent and objects
if(static::class === self::$parentMapper) {
self::$initObjects = [];
self::$parentMapper = null;
}
}
/**
* Get created at column
*
* @return string
*
* @since 1.0.0
*/
public static function getCreatedAt() : string
{
return static::$createdAt;
}
/**
* Get id of object
*
* @param Object $obj Model to create
* @param \ReflectionClass $reflectionClass Reflection class
*
* @return mixed
*
* @since 1.0.0
*/
private static function getObjectId($obj, \ReflectionClass $reflectionClass = null)
{
$reflectionClass = $reflectionClass ?? new \ReflectionClass(get_class($obj));
$reflectionProperty = $reflectionClass->getProperty(static::$columns[static::$primaryField]['internal']);
if (!($isPublic = $reflectionProperty->isPublic())) {
$reflectionProperty->setAccessible(true);
}
$objectId = $reflectionProperty->getValue($obj);
if (!$isPublic) {
$reflectionProperty->setAccessible(false);
}
return $objectId;
}
/**
* Set id to model
*
* @param \ReflectionClass $reflectionClass Reflection class
* @param Object $obj Object to create
* @param mixed $objId Id to set
*
* @return void
*
* @since 1.0.0
*/
private static function setObjectId(\ReflectionClass $reflectionClass, $obj, $objId) /* : void */
{
$reflectionProperty = $reflectionClass->getProperty(static::$columns[static::$primaryField]['internal']);
if (!($isPublic = $reflectionProperty->isPublic())) {
$reflectionProperty->setAccessible(true);
}
settype($objId, static::$columns[static::$primaryField]['type']);
$reflectionProperty->setValue($obj, $objId);
if (!$isPublic) {
$reflectionProperty->setAccessible(false);
}
}
/**
* Parse value
*
* @param string $type Value type
* @param mixed $value Value to parse
*
* @return mixed
*
* @since 1.0.0
*/
private static function parseValue(string $type, $value)
{
if (is_null($value)) {
return null;
} elseif ($type === 'DateTime') {
return $value->format('Y-m-d H:i:s');
} elseif ($type === 'Json' || $type === 'jsonSerializable') {
return json_encode($value);
} elseif ($type === 'Serializable') {
return $value->serialize();
} elseif ($value instanceof \JsonSerializable) {
return json_encode($value->jsonSerialize());
} elseif (is_object($value) && method_exists($value, 'getId')) {
return $value->getId();
} elseif ($type === 'int') {
return (int) $value;
} elseif ($type === 'string') {
return (string) $value;
} elseif ($type === 'float') {
return (float) $value;
} elseif ($type === 'bool') {
return (bool) $value;
}
return $value;
}
/**
* Get mapper specific builder
*
* @param Builder $query Query to fill
*
* @return Builder
*
* @since 1.0.0
*/
public static function getQuery(Builder $query = null) : Builder
{
$query = $query ?? new Builder(self::$db);
$query->prefix(self::$db->getPrefix())
->select('*')
->from(static::$table);
return $query;
}
/**
* Define the highest mapper of this request
*
* @return void
*
* @since 1.0.0
*/
private static function setUpParentMapper() /* : void */
{
self::$parentMapper = static::class;
}
private static function getColumnByMember(string $name) : string
{
foreach(static::$columns as $cName => $column) {
if($column['internal'] === $name) {
return $cName;
}
}
throw \Exception();
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\DataStorage\Database\Schema\Exception\TableException;
/**
* Database exception factory.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class DatabaseExceptionFactory

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -21,10 +20,9 @@ use phpOMS\DataStorage\Database\Connection\ConnectionFactory;
/**
* Database pool handler.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class DatabasePool
@ -57,7 +55,7 @@ class DatabasePool
*
* @since 1.0.0
*/
public function add(string $key = 'core', ConnectionAbstract $db) : bool
public function add(string $key, ConnectionAbstract $db) : bool
{
if (isset($this->pool[$key])) {
return false;
@ -130,5 +128,4 @@ class DatabasePool
return true;
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -22,10 +21,9 @@ use phpOMS\Stdlib\Base\Enum;
*
* Possible database connection status
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class DatabaseStatus extends Enum

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -22,10 +21,9 @@ use phpOMS\Stdlib\Base\Enum;
*
* Database types that are supported by the application
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class DatabaseType extends Enum

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,10 +17,9 @@ namespace phpOMS\DataStorage\Database\Exception;
/**
* Permission exception class.
*
* @category Framework
* @package phpOMS\System\File
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class InvalidConnectionConfigException extends \InvalidArgumentException
@ -31,7 +29,7 @@ class InvalidConnectionConfigException extends \InvalidArgumentException
*
* @param string $message Exception message
* @param int $code Exception code
* @param \Exception Previous exception
* @param \Exception $previous Previous exception
*
* @since 1.0.0
*/

View File

@ -0,0 +1,40 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\DataStorage\Database\Exception;
/**
* Permission exception class.
*
* @package Framework
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
class InvalidDatabaseTypeException extends \InvalidArgumentException
{
/**
* Constructor.
*
* @param string $message Exception message
* @param int $code Exception code
* @param \Exception $previous Previous exception
*
* @since 1.0.0
*/
public function __construct(string $message = '', int $code = 0, \Exception $previous = null)
{
parent::__construct('Invalid database type "' . $message . '".', $code, $previous);
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,10 +17,9 @@ namespace phpOMS\DataStorage\Database\Exception;
/**
* Permission exception class.
*
* @category Framework
* @package phpOMS\System\File
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class InvalidMapperException extends \RuntimeException
@ -31,7 +29,7 @@ class InvalidMapperException extends \RuntimeException
*
* @param string $message Exception message
* @param int $code Exception code
* @param \Exception Previous exception
* @param \Exception $previous Previous exception
*
* @since 1.0.0
*/

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,10 +17,9 @@ namespace phpOMS\DataStorage\Database;
/**
* Grammar.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class GrammarAbstract
@ -238,24 +236,19 @@ abstract class GrammarAbstract
$identifier = $this->systemIdentifier;
foreach ($this->specialKeywords as $keyword) {
if($keyword === '' || strrpos($system, $keyword, -strlen($system)) !== false) {
if (strrpos($system, $keyword, -strlen($system)) !== false) {
$prefix = '';
$identifier = '';
}
}
// todo: move remaining * test also here not just if .* but also if * (should be done in else?)
if (count($split = explode('.', $system)) == 2) {
if ($split[1] === '*') {
$system = $split[1];
} else {
$system = $this->compileSystem($split[1]);
}
if (count($split = explode('.', $system)) === 2) {
$system = $split[1] === '*' ? $split[1] : $this->compileSystem($split[1]);
return $this->compileSystem($prefix . $split[0]) . '.' . $system;
} else {
}
return $identifier . $prefix . $system . $identifier;
}
}
}

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -21,10 +20,9 @@ use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
/**
* Database query builder.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Builder extends BuilderAbstract
@ -64,7 +62,7 @@ class Builder extends BuilderAbstract
/**
* Into.
*
* @var array
* @var string|\Closure
* @since 1.0.0
*/
public $into = null;
@ -152,7 +150,7 @@ class Builder extends BuilderAbstract
/**
* Offset.
*
* @var array
* @var int
* @since 1.0.0
*/
public $offset = null;
@ -181,14 +179,6 @@ class Builder extends BuilderAbstract
*/
public $lock = false;
/**
* Raw query.
*
* @var bool
* @since 1.0.0
*/
public $raw = '';
/**
* Comparison OPERATORS.
*
@ -229,6 +219,7 @@ class Builder extends BuilderAbstract
* Constructor.
*
* @param ConnectionAbstract $connection Database connection
* @param bool $readOnly Query is read only
*
* @since 1.0.0
*/
@ -352,11 +343,38 @@ class Builder extends BuilderAbstract
*
* @return Builder
*
* @throws \Exception
*
* @since 1.0.0
*/
public function raw(string $raw) : Builder
{
if($this->isReadOnly) {
if (!$this->isValidReadOnly($raw)) {
throw new \Exception();
}
$this->type = QueryType::RAW;
$this->raw = rtrim($raw, ';');
return $this;
}
/**
* Tests if a string contains a non read only component in case the builder is read only.
* If the builder is not read only it will always return true
*
* @param string $raw Raw query
*
* @return bool
*
* @since 1.0.0
*/
private function isValidReadOnly($raw) : bool
{
if (!$this->isReadOnly) {
return true;
}
$test = strtolower($raw);
if (strpos($test, 'insert') !== false
@ -364,15 +382,12 @@ class Builder extends BuilderAbstract
|| strpos($test, 'drop') !== false
|| strpos($test, 'delete') !== false
|| strpos($test, 'create') !== false
|| strpos($test, 'alter') !== false) {
throw new \Exception();
}
|| strpos($test, 'alter') !== false
) {
return false;
}
$this->type = QueryType::RAW;
$this->raw = rtrim($raw, ';');
return $this;
return true;
}
/**
@ -398,7 +413,7 @@ class Builder extends BuilderAbstract
*
* @since 1.0.0
*/
public function distinct(...$columns) : Builder
public function distinct() : Builder
{
$this->distinct = true;
@ -408,7 +423,7 @@ class Builder extends BuilderAbstract
/**
* From.
*
* @param array $tables Tables
* @param string|array $tables Tables
*
* @return Builder
*
@ -446,7 +461,7 @@ class Builder extends BuilderAbstract
/**
* Where.
*
* @param string|array|\Closure $columns Columns
* @param Where|string|\Closure|array $columns Columns
* @param string|array $operator Operator
* @param mixed $values Values
* @param string|array $boolean Boolean condition
@ -459,12 +474,17 @@ class Builder extends BuilderAbstract
*/
public function where($columns, $operator = null, $values = null, $boolean = 'and') : Builder
{
// TODO: handle $value is null -> operator NULL
if (isset($operator) && !is_array($operator) && !in_array(strtolower($operator), self::OPERATORS)) {
throw new \InvalidArgumentException('Unknown operator.');
}
if (is_array($columns)) {
if (is_string($columns)) {
$columns = [$columns];
$operator = [$operator];
$values = [$values];
$boolean = [$boolean];
}
$i = 0;
foreach ($columns as $key => $column) {
if (isset($operator[$i]) && !in_array(strtolower($operator[$i]), self::OPERATORS)) {
@ -480,16 +500,6 @@ class Builder extends BuilderAbstract
$i++;
}
} elseif (is_string($columns)) {
if (isset($operator) && !in_array(strtolower($operator), self::OPERATORS)) {
throw new \InvalidArgumentException('Unknown operator.');
}
$this->wheres[self::getPublicColumnName($columns)][] = ['column' => $columns, 'operator' => $operator, 'value' => $values,
'boolean' => $boolean,];
} else {
throw new \InvalidArgumentException();
}
return $this;
}
@ -533,7 +543,9 @@ class Builder extends BuilderAbstract
/**
* Where and sub condition.
*
* @param Where $where Where sub condition
* @param Where|string|\Closure|array $where Where sub condition
* @param mixed $operator Operator
* @param mixed $values Values
*
* @return Builder
*
@ -547,7 +559,9 @@ class Builder extends BuilderAbstract
/**
* Where or sub condition.
*
* @param Where $where Where sub condition
* @param Where|string|\Closure|array $where Where sub condition
* @param mixed $operator Operator
* @param mixed $values Values
*
* @return Builder
*
@ -561,7 +575,7 @@ class Builder extends BuilderAbstract
/**
* Where in.
*
* @param string|array|\Closure $column Column
* @param Where|string|\Closure|array $column Column
* @param mixed $values Values
* @param string $boolean Boolean condition
*
@ -579,7 +593,7 @@ class Builder extends BuilderAbstract
/**
* Where null.
*
* @param string|array|\Closure $column Column
* @param Where|string|\Closure|array $column Column
* @param string $boolean Boolean condition
*
* @return Builder
@ -596,7 +610,7 @@ class Builder extends BuilderAbstract
/**
* Where not null.
*
* @param string|array|\Closure $column Column
* @param Where|string|\Closure|array $column Column
* @param string $boolean Boolean condition
*
* @return Builder
@ -696,7 +710,7 @@ class Builder extends BuilderAbstract
/**
* Offset.
*
* @param int|\Closure $offset Offset
* @param int $offset Offset
*
* @return Builder
*
@ -712,7 +726,7 @@ class Builder extends BuilderAbstract
/**
* Limit.
*
* @param int|\Closure $limit Limit
* @param int $limit Limit
*
* @return Builder
*
@ -726,7 +740,7 @@ class Builder extends BuilderAbstract
}
/**
* Limit.
* Union.
*
* @param string|\phpOMS\DataStorage\Database\Query\Builder $query Query
*
@ -785,9 +799,13 @@ class Builder extends BuilderAbstract
/**
* Count results.
*
* @param string $table Table to count the result set
*
* @return Builder
*
* @since 1.0.0
*/
public function count(string $table = '*')
public function count(string $table = '*') : Builder
{
// todo: don't do this as string, create new object new Count(); this can get handled by the grammar parser WAY better
return $this->select('COUNT(' . $table . ')');
@ -836,6 +854,8 @@ class Builder extends BuilderAbstract
*
* @return Builder
*
* @throws \Exception
*
* @since 1.0.0
*/
public function insert(...$columns) : Builder
@ -941,10 +961,12 @@ class Builder extends BuilderAbstract
/**
* Update columns.
*
* @param array $columns Column names to update
* @param array $tables Column names to update
*
* @return Builder
*
* @throws \Exception
*
* @since 1.0.0
*/
public function update(...$tables) : Builder
@ -1113,16 +1135,20 @@ class Builder extends BuilderAbstract
/**
* Get bind parameter type.
*
* @param mixed $value Value to bind
*
* @return mixed
*
* @throws \Exception
*
* @since 1.0.0
*/
public static function getBindParamType($value)
{
if (is_int($value)) {
return PDO::PARAM_INT;
return \PDO::PARAM_INT;
} elseif (is_string($value) || is_float($value)) {
return PDO::PARAM_STR;
return \PDO::PARAM_STR;
}
throw new \Exception();
@ -1137,7 +1163,7 @@ class Builder extends BuilderAbstract
} elseif ($column instanceof \Closure) {
return $column();
} elseif ($column instanceof \Serializable) {
return $column;
return $column->serialize();
}
throw new \Exception();

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,10 +17,9 @@ namespace phpOMS\DataStorage\Database\Query;
/**
* Database query builder.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Column
@ -63,5 +61,4 @@ class Column
{
$this->column = $column;
}
}

View File

@ -4,18 +4,16 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\DataStorage\Database\Query;
class Expression
{

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -25,10 +24,9 @@ use phpOMS\DataStorage\Database\Query\Where;
/**
* Database query grammar.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Grammar extends GrammarAbstract
@ -115,29 +113,12 @@ class Grammar extends GrammarAbstract
{
$sql = [];
switch ($query->getType()) {
case QueryType::SELECT:
$components = $this->selectComponents;
break;
case QueryType::INSERT:
$components = $this->insertComponents;
break;
case QueryType::UPDATE:
$components = $this->updateComponents;
break;
case QueryType::DELETE:
$components = $this->deleteComponents;
break;
case QueryType::RANDOM:
$components = $this->selectComponents;
break;
case QueryType::RAW:
if ($query->getType() === QueryType::RAW) {
return [$query->raw];
break;
default:
throw new \InvalidArgumentException('Unknown query type.');
}
$components = $this->getComponents($query->getType());
/* Loop all possible query components and if they exist compile them. */
foreach ($components as $component) {
if (isset($query->{$component}) && !empty($query->{$component})) {
@ -148,6 +129,35 @@ class Grammar extends GrammarAbstract
return $sql;
}
/**
* Get query components based on query type.
*
* @param int $type Query type
*
* @return array Array of components to build query
*
* @throws \InvalidArgumentException Throws this exception if the query type is undefined
*
* @since 1.0.0
*/
private function getComponents(int $type) : array
{
switch ($type) {
case QueryType::SELECT:
return $components = $this->selectComponents;
case QueryType::INSERT:
return $components = $this->insertComponents;
case QueryType::UPDATE:
return $components = $this->updateComponents;
case QueryType::DELETE:
return $components = $this->deleteComponents;
case QueryType::RANDOM:
return $components = $this->selectComponents;
default:
throw new \InvalidArgumentException('Unknown query type.');
}
}
/**
* Compile select.
*
@ -173,7 +183,7 @@ class Grammar extends GrammarAbstract
* Compile select.
*
* @param Builder $query Builder
* @param array $columns Columns
* @param array $table Table
*
* @return string
*

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);

View File

@ -4,25 +4,24 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\DataStorage\Database\Query\Grammar;
use phpOMS\DataStorage\Database\Query\Builder;
/**
* Grammar class.
*
* @category Framework
* @package phpOMS\DataStorage\Database\Query\Grammar
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class MicrosoftGrammar extends Grammar

View File

@ -4,25 +4,24 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\DataStorage\Database\Query\Grammar;
use phpOMS\DataStorage\Database\Query\Builder;
/**
* Grammar class.
*
* @category Framework
* @package phpOMS\DataStorage\Database\Query\Grammar
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class MysqlGrammar extends Grammar

View File

@ -4,25 +4,24 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\DataStorage\Database\Query\Grammar;
use phpOMS\DataStorage\Database\Query\Builder;
/**
* Grammar class.
*
* @category Framework
* @package phpOMS\DataStorage\Database\Query\Grammar
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class OracleGrammar extends Grammar

View File

@ -4,25 +4,24 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\DataStorage\Database\Query\Grammar;
use phpOMS\DataStorage\Database\Query\Builder;
/**
* Grammar class.
*
* @category Framework
* @package phpOMS\DataStorage\Database\Query\Grammar
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class PostgresGrammar extends Grammar

View File

@ -4,25 +4,24 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
namespace phpOMS\DataStorage\Database\Query\Grammar;
use phpOMS\DataStorage\Database\Query\Builder;
/**
* Grammar class.
*
* @category Framework
* @package phpOMS\DataStorage\Database\Query\Grammar
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class SqliteGrammar extends Grammar

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\Stdlib\Base\Enum;
/**
* Query type enum.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class JoinType extends Enum

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -20,10 +19,9 @@ use phpOMS\Stdlib\Base\Enum;
/**
* Query type enum.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class QueryType extends Enum

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,10 +17,9 @@ namespace phpOMS\DataStorage\Database\Query;
/**
* Database query builder.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Where

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -22,10 +21,9 @@ use phpOMS\Stdlib\Base\Enum;
*
* Database types that are supported by the application
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
abstract class RelationType extends Enum

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -17,15 +16,13 @@ namespace phpOMS\DataStorage\Database\Schema;
use phpOMS\DataStorage\Database\BuilderAbstract;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Database\Query;
/**
* Database query builder.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Builder extends BuilderAbstract

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -18,10 +17,9 @@ namespace phpOMS\DataStorage\Database\Schema\Exception;
/**
* Path exception class.
*
* @category System
* @package Framework
* @package System
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class TableException extends \PDOException
@ -31,7 +29,7 @@ class TableException extends \PDOException
*
* @param string $message Exception message
* @param int $code Exception code
* @param \Exception Previous exception
* @param \Exception $previous Previous exception
*
* @since 1.0.0
*/

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);
@ -22,10 +21,9 @@ use phpOMS\DataStorage\Database\Schema\QueryType;
/**
* Database query grammar.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @package Framework
* @license OMS License 1.0
* @link http://orange-management.com
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Grammar extends GrammarAbstract

View File

@ -4,12 +4,11 @@
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
* @link http://website.orange-management.de
*/
declare(strict_types = 1);

Some files were not shown because too many files have changed in this diff Show More