From 701aff420d947fb76d816f154931c48d272a03ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20Mazi=C3=A8re?= Date: Sat, 27 Aug 2016 23:52:59 +0200 Subject: [PATCH] [core] code simplification MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pierre Mazière --- lib/Bridge.php | 109 ++++++++++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 46 deletions(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index b7445319..2c23cdf3 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -256,55 +256,72 @@ abstract class BridgeAbstract implements BridgeInterface { if($time !== false && (time() - $this->getCacheDuration() < $time)){ $this->items = $this->cache->loadData(); - } else { - if($this->validateData($param)){ - foreach($param as $name=>$value){ - foreach($this->parameters as $context=>$set){ - if(isset($this->parameters[$context][$name])) - $this->parameters[$context][$name]['value']=$value; - } - } - if(!empty($this->parameters)){ - $queriedContext=$this->getQueriedContext(); - if(is_null($queriedContext)){ - $this->returnClientError('Required parameter(s) missing'); - }else if($queriedContext===false){ - $this->returnClientError('Mixed context parameters'); - }else{ - $this->queriedContext=$queriedContext; - $contexts=array($this->queriedContext); - if(isset($this->parameters['global'])){ - $contexts[]='global'; - } - foreach($contexts as $context){ - foreach($this->parameters[$context] as $p=>$props){ - if(isset($props['type']) && - $props['type']==='checkbox' && - !isset($props['value'])){ - if(!isset($props['defaultValue'])){ - $this->parameters[$context][$p]['value']=false; - }else{ - $this->parameters[$context][$p]['value']=$props['defaultValue']; - } - } - } - } - foreach($param as $name=>$value){ - if(isset($this->parameters['global'][$name])){ - $this->parameters[$queriedContext][$name]['value']=$value; - } - } - } - } - }else{ - $this->returnClientError('Invalid parameters value(s)'); - } - $this->collectData(); + return; + } - if(!is_null($this->cache)){ - $this->cache->saveData($this->getDatas()); + if(empty($this->parameters) && !empty($param)){ + $this->returnClientError('Invalid parameters value(s)'); + }; + + if(!$this->validateData($param)){ + $this->returnClientError('Invalid parameters value(s)'); + } + + // Populate BridgeAbstract::parameters with sanitized data + foreach($param as $name=>$value){ + foreach($this->parameters as $context=>$set){ + if(isset($this->parameters[$context][$name])) + $this->parameters[$context][$name]['value']=$value; } } + + // Guess the paramter context from input data + $queriedContext=$this->getQueriedContext(); + if(is_null($queriedContext)){ + $this->returnClientError('Required parameter(s) missing'); + }else if($queriedContext===false){ + $this->returnClientError('Mixed context parameters'); + } + + $this->queriedContext=$queriedContext; + + // Apply default values to missing data + $contexts=array($this->queriedContext); + if(isset($this->parameters['global'])){ + $contexts[]='global'; + } + foreach($contexts as $context){ + foreach($this->parameters[$context] as $p=>$properties){ + if(!isset($properties['type'])){ + $this->parameters[$context][$p]['type']='text'; + } + if(isset($properties['value'])){ + continue; + } + switch($properties['type']){ + case 'checkbox': + if(!isset($properties['defaultValue'])){ + $this->parameters[$context][$p]['value']=false; + }else{ + $this->parameters[$context][$p]['value']=$properties['defaultValue']; + } + break; + } + } + } + + // Copy global parameter values to the guessed context + foreach($param as $name=>$value){ + if(isset($this->parameters['global'][$name])){ + $this->parameters[$queriedContext][$name]['value']=$value; + } + } + + $this->collectData(); + + if(!is_null($this->cache)){ + $this->cache->saveData($this->getDatas()); + } } public function getName(){