From d520e82d9ebd8c9cf59ffd5abe97ad4fc35e8818 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 2 Sep 2016 18:11:02 +0200 Subject: [PATCH 01/10] [Bridge] Fix function names is* implies Boolean return values, which is not true for these functions --- lib/Bridge.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index 831a4f4..789289b 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -143,7 +143,7 @@ abstract class BridgeAbstract implements BridgeInterface { return $this->items; } - protected function isValidTextValue($value, $pattern = null){ + protected function validateTextValue($value, $pattern = null){ if(!is_null($pattern)){ $filteredValue = filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array( @@ -160,7 +160,7 @@ abstract class BridgeAbstract implements BridgeInterface { return $filteredValue; } - protected function isValidNumberValue($value){ + protected function validateNumberValue($value){ $filteredValue = filter_var($value, FILTER_VALIDATE_INT); if($filteredValue === false && !empty($value)) @@ -169,7 +169,7 @@ abstract class BridgeAbstract implements BridgeInterface { return $filteredValue; } - protected function isValidCheckboxValue($value){ + protected function validateCheckboxValue($value){ $filteredValue = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); if(is_null($filteredValue)) @@ -178,7 +178,7 @@ abstract class BridgeAbstract implements BridgeInterface { return $filteredValue; } - protected function isValidListValue($value, $expectedValues){ + protected function validateListValue($value, $expectedValues){ $filteredValue = filter_var($value); if($filteredValue === false) @@ -211,20 +211,20 @@ abstract class BridgeAbstract implements BridgeInterface { switch($set[$name]['type']){ case 'number': - $data[$name] = $this->isValidNumberValue($value); + $data[$name] = $this->validateNumberValue($value); break; case 'checkbox': - $data[$name] = $this->isValidCheckboxValue($value); + $data[$name] = $this->validateCheckboxValue($value); break; case 'list': - $data[$name] = $this->isValidListValue($value, $set[$name]['values']); + $data[$name] = $this->validateListValue($value, $set[$name]['values']); break; default: case 'text': if(isset($set[$name]['pattern'])){ - $data[$name] = $this->isValidTextValue($value, $set[$name]['pattern']); + $data[$name] = $this->validateTextValue($value, $set[$name]['pattern']); } else { - $data[$name] = $this->isValidTextValue($value); + $data[$name] = $this->validateTextValue($value); } break; } From 37f269cf53622aa96c0c7bb43629c8ac8901d5b1 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 2 Sep 2016 18:12:37 +0200 Subject: [PATCH 02/10] [Bridge] Remove unused variable --- lib/Bridge.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index 789289b..657111a 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -199,12 +199,11 @@ abstract class BridgeAbstract implements BridgeInterface { if(!is_array($data)) return false; - $validated=true; foreach($data as $name=>$value){ - $registered=false; + $registered = false; foreach(static::PARAMETERS as $context=>$set){ if(array_key_exists($name,$set)){ - $registered=true; + $registered = true; if(!isset($set[$name]['type'])){ $set[$name]['type']='text'; } From c2e411ba8230a42515a9b8cb1c9f983f32e04c49 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 2 Sep 2016 18:38:07 +0200 Subject: [PATCH 03/10] [Bridge] Add function to set inputs --- lib/Bridge.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index 657111a..eea0c90 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -242,6 +242,16 @@ abstract class BridgeAbstract implements BridgeInterface { return true; } + protected function setInputs(array $inputs){ + foreach($inputs as $name => $value){ + foreach(static::PARAMETERS as $context => $set){ + if(array_key_exists($name, static::PARAMETERS[$context])){ + $this->inputs[$context][$name]['value'] = $value; + } + } + } + } + protected function getQueriedContext(){ $queriedContexts=array(); foreach(static::PARAMETERS as $context=>$set){ @@ -308,14 +318,7 @@ abstract class BridgeAbstract implements BridgeInterface { $this->returnClientError('Invalid parameters value(s)'); } - // Populate BridgeAbstract::parameters with sanitized data - foreach($inputs as $name=>$value){ - foreach(static::PARAMETERS as $context=>$set){ - if(array_key_exists($name,static::PARAMETERS[$context])){ - $this->inputs[$context][$name]['value']=$value; - } - } - } + $this->setInputs($inputs); // Guess the paramter context from input data $queriedContext=$this->getQueriedContext(); From 48db1693a1bd75fd7622db9b1182236a92386e07 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 2 Sep 2016 18:59:55 +0200 Subject: [PATCH 04/10] [Bridge] Use member variable instead of local variable --- lib/Bridge.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index eea0c90..fb846b5 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -321,15 +321,13 @@ abstract class BridgeAbstract implements BridgeInterface { $this->setInputs($inputs); // Guess the paramter context from input data - $queriedContext=$this->getQueriedContext(); - if(is_null($queriedContext)){ + $this->queriedContext = $this->getQueriedContext(); + if(is_null($this->queriedContext)){ $this->returnClientError('Required parameter(s) missing'); - }else if($queriedContext===false){ + } elseif($this->queriedContext === false){ $this->returnClientError('Mixed context parameters'); } - $this->queriedContext=$queriedContext; - // Apply default values to missing data $contexts=array($this->queriedContext); if(array_key_exists('global',static::PARAMETERS)){ @@ -383,7 +381,7 @@ abstract class BridgeAbstract implements BridgeInterface { }else{ continue; } - $this->inputs[$queriedContext][$name]['value']=$value; + $this->inputs[$this->queriedContext][$name]['value']=$value; } } From 41d3aa0695b4b5069a1d24765edc1370b2762159 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 2 Sep 2016 19:01:53 +0200 Subject: [PATCH 05/10] [Bridge] Change scope of member variable 'inputs' --- lib/Bridge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index fb846b5..3f4f530 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -120,7 +120,7 @@ abstract class BridgeAbstract implements BridgeInterface { const MAINTAINER = 'No maintainer'; const PARAMETERS = array(); public $useProxy = true; - public $inputs = array(); + protected $inputs = array(); protected $queriedContext=''; protected function returnError($message, $code){ From 4bc4e03d7b7f552337ced9d2c8760246028b39ed Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 2 Sep 2016 19:03:51 +0200 Subject: [PATCH 06/10] [Bridge] Change order of variable declaration --- lib/Bridge.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index 3f4f530..0f8ad1a 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -111,17 +111,18 @@ interface BridgeInterface { abstract class BridgeAbstract implements BridgeInterface { - protected $cache; - protected $items = array(); - const NAME = 'Unnamed bridge'; const URI = ''; const DESCRIPTION = 'No description provided'; const MAINTAINER = 'No maintainer'; const PARAMETERS = array(); + public $useProxy = true; + + protected $cache; + protected $items = array(); protected $inputs = array(); - protected $queriedContext=''; + protected $queriedContext = ''; protected function returnError($message, $code){ throw new \HttpException($message, $code); From 5f3d60276a4dfe699af91a2382a4eb2ea9afea15 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 2 Sep 2016 19:46:58 +0200 Subject: [PATCH 07/10] [Bridge] Use original input values to guess context --- lib/Bridge.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index 0f8ad1a..f5afc71 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -253,13 +253,12 @@ abstract class BridgeAbstract implements BridgeInterface { } } - protected function getQueriedContext(){ + protected function getQueriedContext(array $inputs){ $queriedContexts=array(); foreach(static::PARAMETERS as $context=>$set){ $queriedContexts[$context]=null; foreach($set as $id=>$properties){ - if(isset($this->inputs[$context][$id]['value']) && - !empty($this->inputs[$context][$id]['value'])){ + if(isset($inputs[$id]) && !empty($inputs[$id])){ $queriedContexts[$context]=true; }elseif(isset($properties['required']) && $properties['required']===true){ @@ -322,7 +321,7 @@ abstract class BridgeAbstract implements BridgeInterface { $this->setInputs($inputs); // Guess the paramter context from input data - $this->queriedContext = $this->getQueriedContext(); + $this->queriedContext = $this->getQueriedContext($inputs); if(is_null($this->queriedContext)){ $this->returnClientError('Required parameter(s) missing'); } elseif($this->queriedContext === false){ From 04bddd075812a4dd1d87b466ac2c270959826d75 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 2 Sep 2016 19:52:47 +0200 Subject: [PATCH 08/10] [Bridge] Apply default value while setting inputs --- lib/Bridge.php | 129 +++++++++++++++++++++++++------------------------ 1 file changed, 65 insertions(+), 64 deletions(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index f5afc71..c6f793e 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -251,6 +251,70 @@ abstract class BridgeAbstract implements BridgeInterface { } } } + + // Apply default values to missing data + $contexts = array($this->queriedContext); + if(array_key_exists('global', static::PARAMETERS)){ + $contexts[] = 'global'; + } + + foreach($contexts as $context){ + foreach(static::PARAMETERS[$context] as $name => $properties){ + if(!isset($properties['type'])){ + $type = 'text'; + } else { + $type = $properties['type']; + } + if(isset($this->inputs[$context][$name]['value'])){ + continue; + } + switch($properties['type']){ + case 'checkbox': + if(!isset($properties['defaultValue'])){ + $this->inputs[$context][$name]['value'] = false; + } else { + $this->inputs[$context][$name]['value'] = $properties['defaultValue']; + } + break; + case 'list': + if(!isset($properties['defaultValue'])){ + $firstItem = reset($properties['values']); + if(is_array($firstItem)){ + $firstItem = reset($firstItem); + } + $this->inputs[$context][$name]['value'] = $firstItem; + } else { + $this->inputs[$context][$name]['value'] = $properties['defaultValue']; + } + break; + default: + if(isset($properties['defaultValue'])){ + $this->inputs[$context][$name]['value'] = $properties['defaultValue']; + } + break; + } + } + } + + // Copy global parameter values to the guessed context + if(array_key_exists('global', static::PARAMETERS)){ + foreach(static::PARAMETERS['global'] as $name => $properties){ + if(isset($inputs[$name])){ + $value = $inputs[$name]; + }else if(isset($properties['value'])){ + $value = $properties['value']; + }else{ + continue; + } + $this->inputs[$this->queriedContext][$name]['value'] = $value; + } + } + + // Only keep guessed context parameters values + if(!isset($this->inputs[$this->queriedContext])){ + $this->inputs[$this->queriedContext] = array(); + } + $this->inputs = array($this->queriedContext=>$this->inputs[$this->queriedContext]); } protected function getQueriedContext(array $inputs){ @@ -318,8 +382,6 @@ abstract class BridgeAbstract implements BridgeInterface { $this->returnClientError('Invalid parameters value(s)'); } - $this->setInputs($inputs); - // Guess the paramter context from input data $this->queriedContext = $this->getQueriedContext($inputs); if(is_null($this->queriedContext)){ @@ -328,68 +390,7 @@ abstract class BridgeAbstract implements BridgeInterface { $this->returnClientError('Mixed context parameters'); } - // Apply default values to missing data - $contexts=array($this->queriedContext); - if(array_key_exists('global',static::PARAMETERS)){ - $contexts[]='global'; - } - foreach($contexts as $context){ - foreach(static::PARAMETERS[$context] as $name=>$properties){ - if(!isset($properties['type'])){ - $type='text'; - }else{ - $type=$properties['type']; - } - if(isset($this->inputs[$context][$name]['value'])){ - continue; - } - switch($properties['type']){ - case 'checkbox': - if(!isset($properties['defaultValue'])){ - $this->inputs[$context][$name]['value']=false; - }else{ - $this->inputs[$context][$name]['value']=$properties['defaultValue']; - } - break; - case 'list': - if(!isset($properties['defaultValue'])){ - $firstItem=reset($properties['values']); - if(is_array($firstItem)){ - $firstItem=reset($firstItem); - } - $this->inputs[$context][$name]['value']=$firstItem; - }else{ - $this->inputs[$context][$name]['value']=$properties['defaultValue']; - } - break; - default: - if(isset($properties['defaultValue'])){ - $this->inputs[$context][$name]['value']=$properties['defaultValue']; - } - break; - } - } - } - - // Copy global parameter values to the guessed context - if(array_key_exists('global',static::PARAMETERS)){ - foreach(static::PARAMETERS['global'] as $name=>$properties){ - if(isset($inputs[$name])){ - $value=$inputs[$name]; - }else if(isset($properties['value'])){ - $value=$properties['value']; - }else{ - continue; - } - $this->inputs[$this->queriedContext][$name]['value']=$value; - } - } - - // Only keep guessed context parameters values - if(!isset($this->inputs[$this->queriedContext])){ - $this->inputs[$this->queriedContext]=array(); - } - $this->inputs=array($this->queriedContext=>$this->inputs[$this->queriedContext]); + $this->setInputs($inputs); $this->collectData(); From d1ff23c7bada1b4a401b63ab1635158b1cd32fac Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 2 Sep 2016 19:53:59 +0200 Subject: [PATCH 09/10] [Bridge] Fix incorrect switch variable --- lib/Bridge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index c6f793e..3062201 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -268,7 +268,7 @@ abstract class BridgeAbstract implements BridgeInterface { if(isset($this->inputs[$context][$name]['value'])){ continue; } - switch($properties['type']){ + switch($type){ case 'checkbox': if(!isset($properties['defaultValue'])){ $this->inputs[$context][$name]['value'] = false; From a8883523f4c2828a6c367a31955bb44f420271c4 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 2 Sep 2016 20:05:18 +0200 Subject: [PATCH 10/10] [Bridge] Pass 'queriedContext' as parameter to 'setInputs' This makes clear that 'setInputs' depends on the 'queriedContext' --- lib/Bridge.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index 3062201..2a83630 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -243,7 +243,8 @@ abstract class BridgeAbstract implements BridgeInterface { return true; } - protected function setInputs(array $inputs){ + protected function setInputs(array $inputs, $queriedContext){ + // Import and assign all inputs to their context foreach($inputs as $name => $value){ foreach(static::PARAMETERS as $context => $set){ if(array_key_exists($name, static::PARAMETERS[$context])){ @@ -253,21 +254,19 @@ abstract class BridgeAbstract implements BridgeInterface { } // Apply default values to missing data - $contexts = array($this->queriedContext); + $contexts = array($queriedContext); if(array_key_exists('global', static::PARAMETERS)){ $contexts[] = 'global'; } foreach($contexts as $context){ foreach(static::PARAMETERS[$context] as $name => $properties){ - if(!isset($properties['type'])){ - $type = 'text'; - } else { - $type = $properties['type']; - } if(isset($this->inputs[$context][$name]['value'])){ - continue; + continue; } + + $type = isset($properties['type']) ? $properties['type'] : 'text'; + switch($type){ case 'checkbox': if(!isset($properties['defaultValue'])){ @@ -301,20 +300,21 @@ abstract class BridgeAbstract implements BridgeInterface { foreach(static::PARAMETERS['global'] as $name => $properties){ if(isset($inputs[$name])){ $value = $inputs[$name]; - }else if(isset($properties['value'])){ + } elseif (isset($properties['value'])){ $value = $properties['value']; - }else{ + } else { continue; } - $this->inputs[$this->queriedContext][$name]['value'] = $value; + $this->inputs[$queriedContext][$name]['value'] = $value; } } // Only keep guessed context parameters values - if(!isset($this->inputs[$this->queriedContext])){ - $this->inputs[$this->queriedContext] = array(); + if(isset($this->inputs[$queriedContext])){ + $this->inputs = array($queriedContext => $this->inputs[$queriedContext]); + } else { + $this->inputs = array(); } - $this->inputs = array($this->queriedContext=>$this->inputs[$this->queriedContext]); } protected function getQueriedContext(array $inputs){ @@ -390,7 +390,7 @@ abstract class BridgeAbstract implements BridgeInterface { $this->returnClientError('Mixed context parameters'); } - $this->setInputs($inputs); + $this->setInputs($inputs, $this->queriedContext); $this->collectData();