Skip to content

Commit

Permalink
IniUserGroupBackend: Convert timestamps and arrays...
Browse files Browse the repository at this point in the history
...to formatted datetime strings and comma separated strings respectively

refs #8826
  • Loading branch information
Johannes Meyer committed May 8, 2015
1 parent 59ec11f commit f1c82fc
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
11 changes: 11 additions & 0 deletions library/Icinga/Authentication/UserGroup/IniUserGroupBackend.php
Expand Up @@ -49,6 +49,17 @@ class IniUserGroupBackend extends IniRepository implements UserGroupBackendInter
)
);

/**
* The value conversion rules to apply on a query
*
* @var array
*/
protected $conversionRules = array(
'created_at' => 'date_time',
'last_modified' => 'date_time',
'users' => 'comma_separated_string'
);

/**
* Initialize this ini user group backend
*/
Expand Down
100 changes: 100 additions & 0 deletions library/Icinga/Repository/Repository.php
Expand Up @@ -3,12 +3,14 @@

namespace Icinga\Repository;

use DateTime;
use Icinga\Application\Logger;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Selectable;
use Icinga\Exception\ProgrammingError;
use Icinga\Exception\QueryException;
use Icinga\Exception\StatementException;
use Icinga\Util\String;

/**
* Abstract base class for concrete repository implementations
Expand All @@ -22,6 +24,11 @@
*/
abstract class Repository implements Selectable
{
/**
* The format to use when converting values of type date_time
*/
const DATETIME_FORMAT = 'd/m/Y g:i A';

/**
* The name of this repository
*
Expand Down Expand Up @@ -473,6 +480,99 @@ protected function getConverter($name, $context)
}
}

/**
* Convert a timestamp or DateTime object to a string formatted using static::DATETIME_FORMAT
*
* @param mixed $value
*
* @return string
*/
protected function persistDateTime($value)
{
if (is_numeric($value)) {
$value = date(static::DATETIME_FORMAT, $value);
} elseif ($value instanceof DateTime) {
$value = date(static::DATETIME_FORMAT, $value->getTimestamp()); // Using date here, to ignore any timezone
} elseif ($value !== null) {
throw new ProgrammingError(
'Cannot persist value "%s" as type date_time. It\'s not a timestamp or DateTime object',
$value
);
}

return $value;
}

/**
* Convert a string formatted using static::DATETIME_FORMAT to a unix timestamp
*
* @param string $value
*
* @return int
*/
protected function retrieveDateTime($value)
{
if (is_numeric($value)) {
$value = (int) $value;
} elseif (is_string($value)) {
$dateTime = DateTime::createFromFormat(static::DATETIME_FORMAT, $value);
if ($dateTime === false) {
Logger::debug(
'Unable to parse string "%s" as type date_time with format "%s" in repository "%s"',
$value,
static::DATETIME_FORMAT,
$this->getName()
);
$value = null;
} else {
$value = $dateTime->getTimestamp();
}
} elseif ($value !== null) {
throw new ProgrammingError(
'Cannot retrieve value "%s" as type date_time. It\'s not a integer or (numeric) string',
$value
);
}

return $value;
}

/**
* Convert the given array to an comma separated string
*
* @param array|string $value
*
* @return string
*/
protected function persistCommaSeparatedString($value)
{
if (is_array($value)) {
$value = join(',', array_map('trim', $value));
} elseif ($value !== null && !is_string($value)) {
throw new ProgrammingError('Cannot persist value "%s" as comma separated string', $value);
}

return $value;
}

/**
* Convert the given comma separated string to an array
*
* @param string $value
*
* @return array
*/
protected function retrieveCommaSeparatedString($value)
{
if ($value && is_string($value)) {
$value = String::trimSplit($value);
} elseif ($value !== null) {
throw new ProgrammingError('Cannot retrieve value "%s" as array. It\'s not a string', $value);
}

return $value;
}

/**
* Recurse the given filter, require each filter column and convert all values
*
Expand Down

0 comments on commit f1c82fc

Please sign in to comment.