summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2015-01-07 17:36:29 +0100
committerGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2015-01-07 17:36:29 +0100
commitfb614ab80cf038416e5451b4f6f25f421d75d8e4 (patch)
tree94ac657f352caaa6f9086e0382b7f39a133dbe8e
parent2fd8a80878441cf59b1689b1765633694cb320b6 (diff)
Change way to call configuration setter.
- Add a support($key) method which return if the given key is supported by the setter. - Change handle signature by adding a $data param which must be passed by reference. See https://github.com/FreshRSS/FreshRSS/issues/730
-rw-r--r--app/Models/ConfigurationSetter.php44
-rw-r--r--lib/Minz/Configuration.php8
2 files changed, 30 insertions, 22 deletions
diff --git a/app/Models/ConfigurationSetter.php b/app/Models/ConfigurationSetter.php
index e30cb0187..801e11625 100644
--- a/app/Models/ConfigurationSetter.php
+++ b/app/Models/ConfigurationSetter.php
@@ -1,37 +1,47 @@
<?php
class FreshRSS_ConfigurationSetter {
- private $setters = array(
- 'language' => '_language',
- 'posts_per_page' => '_posts_per_page',
- 'view_mode' => '_view_mode',
- );
+ /**
+ * Return if the given key is supported by this setter.
+ * @param $key the key to test.
+ * @return true if the key is supported, false else.
+ */
+ public function support($key) {
+ $name_setter = '_' . $key;
+ return is_callable(array($this, $name_setter));
+ }
- public function handle($key, $value) {
- if (isset($this->setters[$key])) {
- $value = call_user_func(array($this, $this->setters[$key]), $value);
- }
- return $value;
+ /**
+ * Set the given key in data with the current value.
+ * @param $data an array containing the list of all configuration data.
+ * @param $key the key to update.
+ * @param $value the value to set.
+ */
+ public function handle(&$data, $key, $value) {
+ $name_setter = '_' . $key;
+ call_user_func_array(array($this, $name_setter), array(&$data, $value));
}
- private function _language($value) {
+ /**
+ * The (long) list of setters.
+ */
+ private function _language(&$data, $value) {
$languages = Minz_Translate::availableLanguages();
if (!isset($languages[$value])) {
$value = 'en';
}
-
- return $value;
+ $data['language'] = $value;
}
- private function _posts_per_page($value) {
+ private function _posts_per_page(&$data, $value) {
$value = intval($value);
- return $value > 0 ? $value : 10;
+ $data['posts_per_page'] = $value > 0 ? $value : 10;
}
- private function _view_mode($value) {
+ private function _view_mode(&$data, $value) {
if (!in_array($value, array('global', 'normal', 'reader'))) {
$value = 'normal';
}
- return $value;
+ $data['view_mode'] = $value;
}
}
diff --git a/lib/Minz/Configuration.php b/lib/Minz/Configuration.php
index 019b47fae..6044fc269 100644
--- a/lib/Minz/Configuration.php
+++ b/lib/Minz/Configuration.php
@@ -176,11 +176,9 @@ class Minz_Configuration {
* @param $value the value to set. If null, the key is removed from the configuration.
*/
public function _param($key, $value = null) {
- if (!is_null($this->configuration_setter)) {
- $value = $this->configuration_setter->handle($key, $value);
- }
-
- if (isset($this->data[$key]) && is_null($value)) {
+ if (!is_null($this->configuration_setter) && $this->configuration_setter->support($key)) {
+ $this->configuration_setter->handle($this->data, $key, $value);
+ } elseif (isset($this->data[$key]) && is_null($value)) {
unset($this->data[$key]);
} elseif (!is_null($value)) {
$this->data[$key] = $value;