diff --git a/Config/OptionsTrait.php b/Config/OptionsTrait.php index d3f34def2..a833ca5b0 100644 --- a/Config/OptionsTrait.php +++ b/Config/OptionsTrait.php @@ -51,6 +51,22 @@ trait OptionsTrait return $this->options[$key] ?? null; } + /** + * {@inheritdoc} + */ + public function getOptions(array $key) + { + $options = []; + + foreach ($key as $value) { + if (isset($this->options[$value])) { + $options[$value] = $this->options[$value]; + } + } + + return $options; + } + /** * {@inheritdoc} */ diff --git a/Config/SettingsAbstract.php b/Config/SettingsAbstract.php index 745ead076..f45101414 100644 --- a/Config/SettingsAbstract.php +++ b/Config/SettingsAbstract.php @@ -85,19 +85,27 @@ abstract class SettingsAbstract implements OptionsInterface */ public function get($columns) { - try { - if (!\is_array($columns)) { - $keys = [$columns]; - } else { - $keys = []; - foreach ($columns as $key) { - $keys[] = \is_string($key) ? (int) \preg_replace('/[^0-9.]/', '', $key) : $key; - } + $options = []; + if (!\is_array($columns)) { + $keys = [$columns]; + } else { + $keys = []; + foreach ($columns as $key) { + $keys[] = \is_string($key) ? (int) \preg_replace('/[^0-9.]/', '', $key) : $key; } + } - $options = []; - $query = new Builder($this->connection); - $sql = $query->select(...static::$columns) + foreach ($keys as $key) { + if ($this->exists($key)) { + $options[$key] = $this->getOption($key); + unset($keys[$key]); + } + } + + try { + $dbOptions = []; + $query = new Builder($this->connection); + $sql = $query->select(...static::$columns) ->from($this->connection->prefix . static::$table) ->where(static::$columns[0], 'in', $keys) ->toSql(); @@ -105,20 +113,19 @@ abstract class SettingsAbstract implements OptionsInterface $sth = $this->connection->con->prepare($sql); $sth->execute(); - $options = $sth->fetchAll(\PDO::FETCH_KEY_PAIR); + $dbOptions = $sth->fetchAll(\PDO::FETCH_KEY_PAIR); + $options += $dbOptions; - if ($options === false) { - return []; // @codeCoverageIgnore + if ($dbOptions === false) { + return \count($options) > 1 ? $options : \reset($options); // @codeCoverageIgnore } - $this->setOptions($options); - - return \count($options) > 1 ? $options : \reset($options); - } catch (\PDOException $e) { - // @codeCoverageIgnoreStart - echo $e->getMessage(); - // @codeCoverageIgnoreEnd + $this->setOptions($dbOptions); + } catch (\Throwable $e) { + throw $e; } + + return \count($options) > 1 ? $options : \reset($options); } /** @@ -138,7 +145,7 @@ abstract class SettingsAbstract implements OptionsInterface if ($store) { $this->connection->con->beginTransaction(); - foreach ($this->options as $key => $option) { + foreach ($options as $key => $option) { if (\is_string($key)) { $key = (int) \preg_replace('/[^0-9.]/', '', $key); } @@ -156,4 +163,33 @@ abstract class SettingsAbstract implements OptionsInterface $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/tests/Config/OptionsTraitTest.php b/tests/Config/OptionsTraitTest.php index 466dad93b..51c469dca 100644 --- a/tests/Config/OptionsTraitTest.php +++ b/tests/Config/OptionsTraitTest.php @@ -115,4 +115,19 @@ class OptionsTraitTest extends \PHPUnit\Framework\TestCase self::assertEquals(2, $class->getOption('b')); self::assertEquals(3, $class->getOption('c')); } + + /** + * @testdox Multiple options can be retrieved + */ + public function testGetMultiple() : void + { + $class = new class() { + use OptionsTrait; + }; + + self::assertTrue($class->setOption('a', 'value1')); + self::assertTrue($class->setOption('b', 'value2')); + self::assertTrue($class->setOption('c', 'value3')); + self::assertEquals(['a' => 'value1', 'c' => 'value3'], $class->getOptions(['a', 'c'])); + } }