From cd9435b9362f7085ec51adbb791f8562a1dca48f Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Thu, 1 Sep 2016 19:43:25 +0200 Subject: [PATCH 1/4] [Bridge] Simplify cache loading --- lib/Bridge.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index 583598d..bbc87be 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -251,13 +251,10 @@ abstract class BridgeAbstract implements BridgeInterface { if(!is_null($this->cache)){ $this->cache->prepare($inputs); $time = $this->cache->getTime(); - } else { - $time = false; - } - - if($time !== false && (time() - $this->getCacheDuration() < $time)){ - $this->items = $this->cache->loadData(); - return; + if($time !== false && (time() - $this->getCacheDuration() < $time)){ + $this->items = $this->cache->loadData(); + return; + } } if(empty($this->parameters)){ From 7c36b51aa23b21a7af6e8e2d71c206d4343244be Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Thu, 1 Sep 2016 20:53:47 +0200 Subject: [PATCH 2/4] [Bridge] Move code into functions --- lib/Bridge.php | 150 +++++++++++++++++++++++++++++-------------------- 1 file changed, 90 insertions(+), 60 deletions(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index bbc87be..2c8722a 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -143,68 +143,98 @@ abstract class BridgeAbstract implements BridgeInterface { return $this->items; } - protected function validateData(&$data){ - $validated=true; - foreach($data as $name=>$value){ - $registered=false; - foreach($this->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]=filter_var($value,FILTER_VALIDATE_INT); - if($data[$name]===false && !empty($value)){ - $validated=false; - } - break; - case 'checkbox': - $data[$name]=filter_var($value,FILTER_VALIDATE_BOOLEAN, - FILTER_NULL_ON_FAILURE); - if(is_null($data[$name])){ - $validated=false; - } - break; - case 'list': - $data[$name]=filter_var($value); - if(!in_array($value,$set[$name]['values'])){ - foreach($set[$name]['values'] as $subName=>$subValue){ - if(is_array($subValue) && - in_array($value,$subValue)){ - $data[$name]=filter_var($value); - break 2; - } - } - $validated=false; - $data[$name]=null; - } - break; - default: - case 'text': - if(isset($set[$name]['pattern'])){ - $data[$name]=filter_var($value,FILTER_VALIDATE_REGEXP, - array('options'=>array( - 'regexp'=>'/^'.$set[$name]['pattern'].'$/' - )) - ); - }else{ - $data[$name]=filter_var($value); - } - if($data[$name]===false && !empty($value)){ - $validated=false; - } - break; - } - } - } - if(!$registered){ - $validated=false; - } + protected function isValidTextValue($value, $pattern){ + if(isset($pattern)){ + $filteredValue = filter_var($value, FILTER_VALIDATE_REGEXP, + array('options' => array( + 'regexp' => '/^' . $pattern . '$/' + )) + ); + } else { + $filteredValue = filter_var($value); } - return $validated; + if($filteredValue === false) + return null; + + return $filteredValue; + } + + protected function isValidNumberValue($value){ + $filteredValue = filter_var($value, FILTER_VALIDATE_INT); + + if($filteredValue === false && !empty($value)) + return null; + + return $filteredValue; + } + + protected function isValidCheckboxValue($value){ + $filteredValue = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); + + if(is_null($filteredValue)) + return null; + + return $filteredValue; + } + + protected function isValidListValue($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($this->parameters as $context => $set){ + if(array_key_exists($name, $set)){ + $registered = true; + + if(!isset($set[$name]['type'])){ // Default to 'text' + $set[$name]['type'] = 'text'; + } + + switch($set[$name]['type']){ + case 'number': + $data[$name] = $this->isValidNumberValue($value); + break; + case 'checkbox': + $data[$name] = $this->isValidCheckboxValue($value); + break; + case 'list': + $data[$name] = $this->isValidListValue($value, $set[$name]['values']); + break; + default: + case 'text': + $data[$name] = $this->isValidTextValue($value, $set[$name]['pattern']); + break; + } + + if(is_null($data[$name])) + return false; + } + } + + if(!$registered) + return false; + } + + return true; } protected function getQueriedContext(){ From 47b6cb8937e65de2431fc276dd58d84e7d2d095e Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Thu, 1 Sep 2016 20:59:49 +0200 Subject: [PATCH 3/4] [Bridge] Return name of the parameter that failed the check --- lib/Bridge.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index 2c8722a..84202d5 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -225,8 +225,10 @@ abstract class BridgeAbstract implements BridgeInterface { break; } - if(is_null($data[$name])) + if(is_null($data[$name])){ + echo 'Parameter \'' . $name . '\' is invalid!' . PHP_EOL; return false; + } } } From 1aa01a1f6776b2df11b04a6c669c2e2e63c50715 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Thu, 1 Sep 2016 21:07:27 +0200 Subject: [PATCH 4/4] [Bridge] Fix invalid text value without pattern --- lib/Bridge.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index 84202d5..8f0c356 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -143,8 +143,8 @@ abstract class BridgeAbstract implements BridgeInterface { return $this->items; } - protected function isValidTextValue($value, $pattern){ - if(isset($pattern)){ + protected function isValidTextValue($value, $pattern = null){ + if(!is_null($pattern)){ $filteredValue = filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array( 'regexp' => '/^' . $pattern . '$/' @@ -221,7 +221,11 @@ abstract class BridgeAbstract implements BridgeInterface { break; default: case 'text': - $data[$name] = $this->isValidTextValue($value, $set[$name]['pattern']); + if(isset($set[$name]['pattern'])){ + $data[$name] = $this->isValidTextValue($value, $set[$name]['pattern']); + } else { + $data[$name] = $this->isValidTextValue($value); + } break; }