From 5d3eb5e8cb03deb87556f353a17d38667f637caa Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 26 Jun 2015 14:20:35 +0200 Subject: [PATCH] Repository: Allow to check for conversion rules of a specific column --- library/Icinga/Repository/DbRepository.php | 24 ++++++++++++++++++---- library/Icinga/Repository/Repository.php | 18 +++++++++++++--- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/library/Icinga/Repository/DbRepository.php b/library/Icinga/Repository/DbRepository.php index 0ef4d32e9..be9a8b838 100644 --- a/library/Icinga/Repository/DbRepository.php +++ b/library/Icinga/Repository/DbRepository.php @@ -463,17 +463,33 @@ abstract class DbRepository extends Repository implements Extensible, Updatable, } /** - * Return whether this repository is capable of converting values + * Return whether this repository is capable of converting values for the given table and optional column * - * This does not check whether any conversion for the given table is available, as it may be possible - * that columns from another table where joined in which would otherwise not being converted. + * This does not check whether any conversion for the given table is available if $column is not given, as it + * may be possible that columns from another table where joined in which would otherwise not being converted. * * @param array|string $table + * @param string $column * * @return bool */ - public function providesValueConversion($_) + public function providesValueConversion($table, $column = null) { + if ($column !== null) { + if ($this->validateQueryColumnAssociation($table, $column)) { + return parent::providesValueConversion( + $this->removeTablePrefix($this->clearTableAlias($table)), + $column + ); + } + + if (($tableName = $this->findTableName($column))) { + return parent::providesValueConversion($tableName, $column); + } + + return false; + } + $conversionRules = $this->getConversionRules(); return !empty($conversionRules); } diff --git a/library/Icinga/Repository/Repository.php b/library/Icinga/Repository/Repository.php index ada50e3f6..065548fb8 100644 --- a/library/Icinga/Repository/Repository.php +++ b/library/Icinga/Repository/Repository.php @@ -464,16 +464,28 @@ abstract class Repository implements Selectable } /** - * Return whether this repository is capable of converting values for the given table + * Return whether this repository is capable of converting values for the given table and optional column * * @param string $table + * @param string $column * * @return bool */ - public function providesValueConversion($table) + public function providesValueConversion($table, $column = null) { $conversionRules = $this->getConversionRules(); - return !empty($conversionRules) && isset($conversionRules[$table]); + if (empty($conversionRules)) { + return false; + } + + if (! isset($conversionRules[$table])) { + return false; + } elseif ($column === null) { + return true; + } + + $alias = $this->reassembleQueryColumnAlias($table, $column) ?: $column; + return array_key_exists($alias, $conversionRules[$table]) || in_array($alias, $conversionRules[$table]); } /**