CustomVariables: implement iterator and countable

This commit is contained in:
Thomas Gelf 2015-06-15 17:16:09 +02:00
parent 828a4a709b
commit 49abd08ec3
1 changed files with 91 additions and 3 deletions

View File

@ -4,8 +4,10 @@ namespace Icinga\Module\Director\CustomVariable;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c; use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Iterator;
use Countable;
class CustomVariables class CustomVariables implements Iterator, Countable
{ {
protected $storedVars = array(); protected $storedVars = array();
@ -13,6 +15,45 @@ class CustomVariables
protected $modified = false; protected $modified = false;
private $position = 0;
protected $idx = array();
public function count()
{
return count($this->vars);
}
public function rewind()
{
$this->position = 0;
}
public function current()
{
if (! $this->valid()) {
return null;
}
return $this->vars[$this->idx[$this->position]];
}
public function key()
{
return $this->idx[$this->position];
}
public function next()
{
++$this->position;
}
public function valid()
{
return array_key_exists($this->position, $this->idx);
}
/** /**
* Generic setter * Generic setter
* *
@ -35,10 +76,16 @@ class CustomVariables
$this->vars[$key] = $value; $this->vars[$key] = $value;
$this->modified = true; $this->modified = true;
$this->refreshIndex();
return $this; return $this;
} }
protected function refreshIndex()
{
$this->idx = array_keys($this->vars);
}
public static function loadForStoredObject(IcingaObject $object) public static function loadForStoredObject(IcingaObject $object)
{ {
$db = $object->getDb(); $db = $object->getDb();
@ -56,10 +103,48 @@ class CustomVariables
foreach ($db->fetchAll($query) as $row) { foreach ($db->fetchAll($query) as $row) {
$vars->vars[$row->varname] = CustomVariable::fromDbRow($row); $vars->vars[$row->varname] = CustomVariable::fromDbRow($row);
} }
$vars->refreshIndex();
return $vars; return $vars;
} }
public function storeToDb(IcingaObject $object)
{
$db = $object->getDb();
$table = $object->getVarsTableName();
$foreignColumn = $object->getVarsIdColumn();
$foreignId = $object->getId();
foreach ($this->vars as $var) {
if ($var->isNew()) {
$db->insert(
$table,
array(
$foreignColumn => $foreignId,
'varname' => $var->getKey(),
'varvalue' => $var->getDbValue(),
'format' => $var->getDbFormat()
)
);
continue;
}
$where = $db->quoteInto(sprintf('%s = ?', $foreignColumn), $foreignId)
. $this->quoteInto(' AND varname = ?', $var->getKey());
if ($var->hasBeenDeleted()) {
$db->delete($table, $where);
} elseif ($var->hasBeenModified()) {
$db->update(
$table,
array('varvalue' => $var->getValueForDb()),
$where
);
}
}
}
public function get($key) public function get($key)
{ {
if (array_key_exists($key, $this->vars)) { if (array_key_exists($key, $this->vars)) {
@ -130,10 +215,13 @@ class CustomVariables
*/ */
public function __unset($key) public function __unset($key)
{ {
if (! array_key_exists($key, $this->properties)) { if (! array_key_exists($key, $this->vars)) {
throw new Exception('Trying to unset invalid key'); throw new Exception('Trying to unset invalid key');
} }
$this->properties[$key] = $this->defaultProperties[$key];
unset($this->vars[$key]);
$this->refreshIndex();
} }
public function __toString() public function __toString()