expand options/config functionality

This commit is contained in:
Dennis Eichhorn 2019-11-03 23:19:38 +01:00
parent bad1b05de0
commit c65e73d2ac
3 changed files with 89 additions and 22 deletions

View File

@ -51,6 +51,22 @@ trait OptionsTrait
return $this->options[$key] ?? null; 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} * {@inheritdoc}
*/ */

View File

@ -85,19 +85,27 @@ abstract class SettingsAbstract implements OptionsInterface
*/ */
public function get($columns) public function get($columns)
{ {
try { $options = [];
if (!\is_array($columns)) { if (!\is_array($columns)) {
$keys = [$columns]; $keys = [$columns];
} else { } else {
$keys = []; $keys = [];
foreach ($columns as $key) { foreach ($columns as $key) {
$keys[] = \is_string($key) ? (int) \preg_replace('/[^0-9.]/', '', $key) : $key; $keys[] = \is_string($key) ? (int) \preg_replace('/[^0-9.]/', '', $key) : $key;
}
} }
}
$options = []; foreach ($keys as $key) {
$query = new Builder($this->connection); if ($this->exists($key)) {
$sql = $query->select(...static::$columns) $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) ->from($this->connection->prefix . static::$table)
->where(static::$columns[0], 'in', $keys) ->where(static::$columns[0], 'in', $keys)
->toSql(); ->toSql();
@ -105,20 +113,19 @@ abstract class SettingsAbstract implements OptionsInterface
$sth = $this->connection->con->prepare($sql); $sth = $this->connection->con->prepare($sql);
$sth->execute(); $sth->execute();
$options = $sth->fetchAll(\PDO::FETCH_KEY_PAIR); $dbOptions = $sth->fetchAll(\PDO::FETCH_KEY_PAIR);
$options += $dbOptions;
if ($options === false) { if ($dbOptions === false) {
return []; // @codeCoverageIgnore return \count($options) > 1 ? $options : \reset($options); // @codeCoverageIgnore
} }
$this->setOptions($options); $this->setOptions($dbOptions);
} catch (\Throwable $e) {
return \count($options) > 1 ? $options : \reset($options); throw $e;
} catch (\PDOException $e) {
// @codeCoverageIgnoreStart
echo $e->getMessage();
// @codeCoverageIgnoreEnd
} }
return \count($options) > 1 ? $options : \reset($options);
} }
/** /**
@ -138,7 +145,7 @@ abstract class SettingsAbstract implements OptionsInterface
if ($store) { if ($store) {
$this->connection->con->beginTransaction(); $this->connection->con->beginTransaction();
foreach ($this->options as $key => $option) { foreach ($options as $key => $option) {
if (\is_string($key)) { if (\is_string($key)) {
$key = (int) \preg_replace('/[^0-9.]/', '', $key); $key = (int) \preg_replace('/[^0-9.]/', '', $key);
} }
@ -156,4 +163,33 @@ abstract class SettingsAbstract implements OptionsInterface
$this->connection->con->commit(); $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();
}
} }

View File

@ -115,4 +115,19 @@ class OptionsTraitTest extends \PHPUnit\Framework\TestCase
self::assertEquals(2, $class->getOption('b')); self::assertEquals(2, $class->getOption('b'));
self::assertEquals(3, $class->getOption('c')); 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']));
}
} }