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
This commit is contained in:
Marien Fressinaud
2015-01-07 17:36:29 +01:00
parent 2fd8a80878
commit fb614ab80c
2 changed files with 31 additions and 23 deletions

View File

@@ -1,37 +1,47 @@
<?php
class FreshRSS_ConfigurationSetter {
private $setters = array(
'language' => '_language',
'posts_per_page' => '_posts_per_page',
'view_mode' => '_view_mode',
);
public function handle($key, $value) {
if (isset($this->setters[$key])) {
$value = call_user_func(array($this, $this->setters[$key]), $value);
}
return $value;
/**
* 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));
}
private function _language($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));
}
/**
* 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;
}
}

View File

@@ -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;