From f4ae14198175ea5410bda67666b85f9f0b69be2d Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 15 Feb 2020 02:00:46 +0100 Subject: [PATCH] fixes Orange-Management/Modules#135 --- ApplicationAbstract.php | 8 +- Config/SettingsAbstract.php | 243 ------------------------- Config/SettingsInterface.php | 85 +++++++++ DataStorage/Database/Query/Builder.php | 10 +- 4 files changed, 95 insertions(+), 251 deletions(-) delete mode 100644 Config/SettingsAbstract.php create mode 100644 Config/SettingsInterface.php diff --git a/ApplicationAbstract.php b/ApplicationAbstract.php index 9e08fae54..beaf9aa23 100644 --- a/ApplicationAbstract.php +++ b/ApplicationAbstract.php @@ -15,7 +15,7 @@ declare(strict_types=1); namespace phpOMS; use phpOMS\Account\AccountManager; -use phpOMS\Config\SettingsAbstract; +use phpOMS\Config\SettingsInterface; use phpOMS\DataStorage\Cache\CachePool; use phpOMS\DataStorage\Cookie\CookieJar; use phpOMS\DataStorage\Database\DatabasePool; @@ -46,7 +46,7 @@ use phpOMS\Router\RouterInterface; * @property \phpOMS\Module\ModuleManager $moduleManager * @property \phpOMS\Dispatcher\Dispatcher $dispatcher * @property \phpOMS\DataStorage\Cache\CachePool $cachePool - * @property \phpOMS\Config\SettingsAbstract $appSettings + * @property \phpOMS\Config\SettingsInterface $appSettings * @property \phpOMS\Event\EventManager $eventManager * @property \phpOMS\Account\AccountManager $accountManager * @property \phpOMS\Log\FileLogger $logger @@ -93,10 +93,10 @@ class ApplicationAbstract /** * Application settings object. * - * @var SettingsAbstract + * @var SettingsInterface * @since 1.0.0 */ - protected SettingsAbstract $appSettings; + protected SettingsInterface $appSettings; /** * Account manager instance. diff --git a/Config/SettingsAbstract.php b/Config/SettingsAbstract.php deleted file mode 100644 index 41c2c4436..000000000 --- a/Config/SettingsAbstract.php +++ /dev/null @@ -1,243 +0,0 @@ -exists($key)) { - $options[$key] = $this->getOption($key); - - return \count($options) > 1 ? $options : \reset($options); - } - } else { - if (!\is_array($columns)) { - $keys = [$columns]; - } else { - $keys = []; - foreach ($columns as $key) { - $keys[] = \is_string($key) ? (int) \preg_replace('/[^0-9.]/', '', $key) : $key; - } - } - - foreach ($keys as $key) { - if ($this->exists($key)) { - $options[$key] = $this->getOption($key); - unset($keys[$key]); - } - } - - if (empty($keys)) { - return \count($options) > 1 ? $options : \reset($options); - } - } - - try { - $dbOptions = []; - $query = new Builder($this->connection); - $query->select(...static::$columns) - ->from($this->connection->prefix . static::$table); - - if (!empty($columns)) { - $query->where(static::$columns[0], 'in', $keys); - } else { - if ($name !== null) { - $query->where(static::$columns['name'], '=', $name); - } - - if ($module !== null) { - $query->andWhere(static::$columns['module'], '=', $module); - } - - if ($group !== null) { - $query->andWhere(static::$columns['group'], '=', $group); - } - - if ($account !== null) { - $query->andWhere(static::$columns['account'], '=', $account); - } - } - - $sql = $query->toSql(); - - $sth = $this->connection->con->prepare($sql); - $sth->execute(); - - $dbOptions = $sth->fetchAll(\PDO::FETCH_KEY_PAIR); - $options += $dbOptions === false ? [] : $dbOptions; - - if ($dbOptions === false) { - return \count($options) > 1 ? $options : \reset($options); // @codeCoverageIgnore - } - - $this->setOptions($dbOptions); - } catch (\Throwable $e) { - throw $e; - } - - return \count($options) > 1 ? $options : \reset($options); - } - - /** - * Set option by key. - * - * @param string[] $options Column values for filtering - * @param bool $store Save this Setting immediately to database - * - * @return void - * - * @since 1.0.0 - */ - public function set(array $options, bool $store = false) : void - { - $this->setOptions($options); - - if ($store) { - $this->connection->con->beginTransaction(); - - foreach ($options as $key => $option) { - if (\is_string($key)) { - $key = (int) \preg_replace('/[^0-9.]/', '', $key); - } - - $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(); - } - - $this->connection->con->commit(); - } - } - - /** - * Save options. - * - * @return void - * - * @since 1.0.0 - */ - public function save() : void - { - $this->connection->con->beginTransaction(); - - foreach ($this->options as $key => $option) { - if (\is_string($key)) { - $key = (int) \preg_replace('/[^0-9.]/', '', $key); - } - - $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(); - } - - $this->connection->con->commit(); - } -} diff --git a/Config/SettingsInterface.php b/Config/SettingsInterface.php new file mode 100644 index 000000000..60d74802d --- /dev/null +++ b/Config/SettingsInterface.php @@ -0,0 +1,85 @@ + $options Column values for filtering + * @param bool $store Save this Setting immediately to database + * + * @return void + * + * @since 1.0.0 + */ + public function set(array $options, bool $store = false) : void; + + /** + * Save options. + * + * @param array $options Options to save + * + * @return void + * + * @since 1.0.0 + */ + public function save(array $options = []) : void; + + /** + * Create option. + * + * @param array $options Options to save + * + * @return void + * + * @since 1.0.0 + */ + public function create(array $options = []) : void; +} diff --git a/DataStorage/Database/Query/Builder.php b/DataStorage/Database/Query/Builder.php index 9d5dce806..63c9ef28f 100644 --- a/DataStorage/Database/Query/Builder.php +++ b/DataStorage/Database/Query/Builder.php @@ -96,7 +96,7 @@ class Builder extends BuilderAbstract /** * Into columns. * - * @var array + * @var array * @since 1.0.0 */ public array $values = []; @@ -888,7 +888,7 @@ class Builder extends BuilderAbstract $this->type = QueryType::INSERT; - foreach ($columns as $key => $column) { + foreach ($columns as $column) { $this->inserts[] = $column; } @@ -951,10 +951,12 @@ class Builder extends BuilderAbstract public function value($value) : self { \end($this->values); - $key = \key($this->values); + + $key = \key($this->values); + $key ??= 0; if (\is_array($value)) { - $this->values[$key] = $value; + $this->values[$key + 1] = $value; } else { $this->values[$key][] = $value; }