From 556b8a245288dd417cd37f2eecd95a2b578af7aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20Mazi=C3=A8re?= Date: Tue, 30 Aug 2016 11:23:15 +0200 Subject: [PATCH] [core] transform some BridgeAbstract members to class constants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This concerns $uri, $name, $maintainer, $parameters and $description Signed-off-by: Pierre Mazière --- lib/Bridge.php | 72 ++++++++++++++++++++++++++++------------------- lib/HTMLUtils.php | 15 +++++----- 2 files changed, 51 insertions(+), 36 deletions(-) diff --git a/lib/Bridge.php b/lib/Bridge.php index 8f0c356..d6b1587 100644 --- a/lib/Bridge.php +++ b/lib/Bridge.php @@ -114,12 +114,12 @@ abstract class BridgeAbstract implements BridgeInterface { protected $cache; protected $items = array(); - public $name = 'Unnamed bridge'; - public $uri = ''; - public $description = 'No description provided'; - public $maintainer = 'No maintainer'; + const NAME = 'Unnamed bridge'; + const URI = ''; + const DESCRIPTION = 'No description provided'; + const MAINTAINER = 'No maintainer'; + const PARAMETERS = array(); public $useProxy = true; - public $parameters = array(); public $inputs = array(); protected $queriedContext=''; @@ -199,28 +199,28 @@ abstract class BridgeAbstract implements BridgeInterface { 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'; + $validated=true; + 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': + case 'number': $data[$name] = $this->isValidNumberValue($value); break; - case 'checkbox': + case 'checkbox': $data[$name] = $this->isValidCheckboxValue($value); break; - case 'list': + case 'list': $data[$name] = $this->isValidListValue($value, $set[$name]['values']); break; default: - case 'text': + case 'text': if(isset($set[$name]['pattern'])){ $data[$name] = $this->isValidTextValue($value, $set[$name]['pattern']); } else { @@ -245,7 +245,7 @@ abstract class BridgeAbstract implements BridgeInterface { protected function getQueriedContext(){ $queriedContexts=array(); - foreach($this->parameters as $context=>$set){ + foreach(static::PARAMETERS as $context=>$set){ $queriedContexts[$context]=null; foreach($set as $id=>$properties){ if(isset($properties['value']) && @@ -259,7 +259,7 @@ abstract class BridgeAbstract implements BridgeInterface { } } - if(isset($this->parameters['global']) && + if(array_key_exists('global',static::PARAMETERS) && $queriedContexts['global']===false){ return null; } @@ -293,7 +293,7 @@ abstract class BridgeAbstract implements BridgeInterface { } } - if(empty($this->parameters)){ + if(empty(static::PARAMETERS)){ if(!empty($inputs)){ $this->returnClientError('Invalid parameters value(s)'); } @@ -311,8 +311,8 @@ abstract class BridgeAbstract implements BridgeInterface { // Populate BridgeAbstract::parameters with sanitized data foreach($inputs as $name=>$value){ - foreach($this->parameters as $context=>$set){ - if(isset($this->parameters[$context][$name])){ + foreach(static::PARAMETERS as $context=>$set){ + if(array_key_exists($name,static::PARAMETERS[$context])){ $this->inputs[$context][$name]['value']=$value; $this->parameters[$context][$name]['value']=$value; } @@ -331,13 +331,15 @@ abstract class BridgeAbstract implements BridgeInterface { // Apply default values to missing data $contexts=array($this->queriedContext); - if(isset($this->parameters['global'])){ + if(array_key_exists('global',static::PARAMETERS)){ $contexts[]='global'; } foreach($contexts as $context){ - foreach($this->parameters[$context] as $name=>$properties){ + foreach(static::PARAMETERS[$context] as $name=>$properties){ if(!isset($properties['type'])){ - $this->parameters[$context][$name]['type']='text'; + $type='text'; + }else{ + $type=$properties['type']; } if(isset($properties['value'])){ continue; @@ -371,8 +373,8 @@ abstract class BridgeAbstract implements BridgeInterface { } // Copy global parameter values to the guessed context - if(isset($this->parameters['global'])){ - foreach($this->parameters['global'] as $name=>$properties){ + 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'])){ @@ -405,11 +407,11 @@ abstract class BridgeAbstract implements BridgeInterface { } public function getName(){ - return $this->name; + return static::NAME; } public function getURI(){ - return $this->uri; + return static::URI; } public function getCacheDuration(){ @@ -585,6 +587,10 @@ abstract class HttpCachingBridgeAbstract extends BridgeAbstract { abstract class RssExpander extends HttpCachingBridgeAbstract { + private $name; + private $uri; + private $description; + public function collectExpandableDatas($name){ if(empty($name)){ $this->returnServerError('There is no $name for this RSS expander'); @@ -632,6 +638,14 @@ abstract class RssExpander extends HttpCachingBridgeAbstract { */ abstract protected function parseRSSItem($item); + public function getURI(){ + return $this->uri; + } + + public function getName(){ + return $this->name; + } + public function getDescription(){ return $this->description; } diff --git a/lib/HTMLUtils.php b/lib/HTMLUtils.php index cbfa0af..7e0061d 100644 --- a/lib/HTMLUtils.php +++ b/lib/HTMLUtils.php @@ -3,12 +3,13 @@ class HTMLUtils { public static function displayBridgeCard($bridgeName, $formats, $isActive = true){ $bridgeElement = Bridge::create($bridgeName); + $bridgeClass=$bridgeName.'Bridge'; if($bridgeElement == false) return ""; - $name = '' . $bridgeElement->name . ''; - $description = $bridgeElement->description; + $name = '' . $bridgeClass::NAME . ''; + $description = $bridgeClass::DESCRIPTION; $card = << @@ -21,7 +22,7 @@ class HTMLUtils { CARD; // If we don't have any parameter for the bridge, we print a generic form to load it. - if(count($bridgeElement->parameters) == 0) { + if(count($bridgeClass::PARAMETERS) == 0) { $card .= HTMLUtils::getFormHeader($bridgeName); @@ -40,13 +41,13 @@ CARD; $card .= '' . PHP_EOL; } - $hasGlobalParameter = array_key_exists('global', $bridgeElement->parameters); + $hasGlobalParameter = array_key_exists('global', $bridgeClass::PARAMETERS); if($hasGlobalParameter){ - $globalParameters = $bridgeElement->parameters['global']; + $globalParameters = $bridgeClass::PARAMETERS['global']; } - foreach($bridgeElement->parameters as $parameterName => $parameter){ + foreach($bridgeClass::PARAMETERS as $parameterName => $parameter){ if(!is_numeric($parameterName) && $parameterName == 'global') continue; @@ -129,7 +130,7 @@ CARD; } $card .= ''; - $card .= '

' . $bridgeElement->maintainer . '

'; + $card .= '

' . $bridgeClass::MAINTAINER . '

'; $card .= ''; return $card;