From a1ef589ffe61d21c8c8c351d7aa8be29529a3419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20Mazi=C3=A8re?= Date: Mon, 26 Sep 2016 00:09:29 +0200 Subject: [PATCH] [core] extract BridgeAbstract methods to make them functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Method validateData is now afunction in lib/validation.php validateTextValue, validateNumberValue, validateCheckboxValue and validateListValue are now anonymous functions defined in validateData Signed-off-by: Pierre Mazière --- lib/BridgeAbstract.php | 101 +--------------------------------------- lib/RssBridge.php | 2 + lib/validation.php | 103 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 100 deletions(-) create mode 100644 lib/validation.php diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index 67b3ada..69183b2 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -21,105 +21,6 @@ abstract class BridgeAbstract implements BridgeInterface { return $this->items; } - protected function validateTextValue($value, $pattern = null){ - if(!is_null($pattern)){ - $filteredValue = filter_var($value - , FILTER_VALIDATE_REGEXP - , array('options' => array( - 'regexp' => '/^' . $pattern . '$/' - )) - ); - } else { - $filteredValue = filter_var($value); - } - - if($filteredValue === false) - return null; - - return $filteredValue; - } - - protected function validateNumberValue($value){ - $filteredValue = filter_var($value, FILTER_VALIDATE_INT); - - if($filteredValue === false && !empty($value)) - return null; - - return $filteredValue; - } - - protected function validateCheckboxValue($value){ - $filteredValue = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); - - if(is_null($filteredValue)) - return null; - - return $filteredValue; - } - - protected function validateListValue($value, $expectedValues){ - $filteredValue = filter_var($value); - - if($filteredValue === false) - return null; - - if(!in_array($filteredValue, $expectedValues)){ // Check sub-values? - foreach($expectedValues as $subName => $subValue){ - if(is_array($subValue) && in_array($filteredValue, $subValue)) - return $filteredValue; - } - return null; - } - - return $filteredValue; - } - - protected function validateData(&$data){ - if(!is_array($data)) - return false; - - foreach($data as $name => $value){ - $registered = false; - foreach(static::PARAMETERS as $context => $set){ - if(array_key_exists($name, $set)){ - $registered = true; - if(!isset($set[$name]['type'])){ - $set[$name]['type'] = 'text'; - } - - switch($set[$name]['type']){ - case 'number': - $data[$name] = $this->validateNumberValue($value); - break; - case 'checkbox': - $data[$name] = $this->validateCheckboxValue($value); - break; - case 'list': - $data[$name] = $this->validateListValue($value, $set[$name]['values']); - break; - default: - case 'text': - if(isset($set[$name]['pattern'])){ - $data[$name] = $this->validateTextValue($value, $set[$name]['pattern']); - } else { - $data[$name] = $this->validateTextValue($value); - } - break; - } - - if(is_null($data[$name])){ - echo 'Parameter \'' . $name . '\' is invalid!' . PHP_EOL; - return false; - } - } - } - - if(!$registered) - return false; - } - - return true; - } protected function setInputs(array $inputs, $queriedContext){ // Import and assign all inputs to their context @@ -256,7 +157,7 @@ abstract class BridgeAbstract implements BridgeInterface { return; } - if(!$this->validateData($inputs)){ + if(!validateData($inputs, static::PARAMETERS)){ returnClientError('Invalid parameters value(s)'); } diff --git a/lib/RssBridge.php b/lib/RssBridge.php index 86ad644..0052abc 100644 --- a/lib/RssBridge.php +++ b/lib/RssBridge.php @@ -15,6 +15,8 @@ require __DIR__ . '/BridgeAbstract.php'; require __DIR__ . '/FeedExpander.php'; require __DIR__ . '/Cache.php'; require __DIR__ . '/CacheAbstract.php'; + +require __DIR__ . '/validation.php'; require __DIR__ . '/html.php'; require __DIR__ . '/error.php'; require __DIR__ . '/contents.php'; diff --git a/lib/validation.php b/lib/validation.php new file mode 100644 index 0000000..c6c3e27 --- /dev/null +++ b/lib/validation.php @@ -0,0 +1,103 @@ + array( + 'regexp' => '/^' . $pattern . '$/' + )) + ); + } else { + $filteredValue = filter_var($value); + } + + if($filteredValue === false) + return null; + + return $filteredValue; + }; + + $validateNumberValue = function($value){ + $filteredValue = filter_var($value, FILTER_VALIDATE_INT); + + if($filteredValue === false && !empty($value)) + return null; + + return $filteredValue; + }; + + $validateCheckboxValue = function($value){ + $filteredValue = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); + + if(is_null($filteredValue)) + return null; + + return $filteredValue; + }; + + $validateListValue = function($value, $expectedValues){ + $filteredValue = filter_var($value); + + if($filteredValue === false) + return null; + + if(!in_array($filteredValue, $expectedValues)){ // Check sub-values? + foreach($expectedValues as $subName => $subValue){ + if(is_array($subValue) && in_array($filteredValue, $subValue)) + return $filteredValue; + } + return null; + } + + return $filteredValue; + }; + + + if(!is_array($data)) + return false; + + foreach($data as $name => $value){ + $registered = false; + foreach($parameters as $context => $set){ + if(array_key_exists($name, $set)){ + $registered = true; + if(!isset($set[$name]['type'])){ + $set[$name]['type'] = 'text'; + } + + switch($set[$name]['type']){ + case 'number': + $data[$name] = $validateNumberValue($value); + break; + case 'checkbox': + $data[$name] = $validateCheckboxValue($value); + break; + case 'list': + $data[$name] = $validateListValue($value, $set[$name]['values']); + break; + default: + case 'text': + if(isset($set[$name]['pattern'])){ + $data[$name] = $validateTextValue($value, $set[$name]['pattern']); + } else { + $data[$name] = $validateTextValue($value); + } + break; + } + + if(is_null($data[$name])){ + echo 'Parameter \'' . $name . '\' is invalid!' . PHP_EOL; + return false; + } + } + } + + if(!$registered) + return false; + } + + return true; +} + +?>