2016-09-05 18:05:19 +02:00
|
|
|
<?php
|
|
|
|
require_once(__DIR__ . '/BridgeInterface.php');
|
|
|
|
abstract class BridgeAbstract implements BridgeInterface {
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
const NAME = 'Unnamed bridge';
|
|
|
|
const URI = '';
|
|
|
|
const DESCRIPTION = 'No description provided';
|
|
|
|
const MAINTAINER = 'No maintainer';
|
2016-09-25 17:04:28 +02:00
|
|
|
const CACHE_TIMEOUT = 3600;
|
2016-09-10 20:41:11 +02:00
|
|
|
const PARAMETERS = array();
|
|
|
|
|
|
|
|
protected $cache;
|
2017-01-03 11:28:47 +01:00
|
|
|
protected $extraInfos;
|
2016-09-10 20:41:11 +02:00
|
|
|
protected $items = array();
|
|
|
|
protected $inputs = array();
|
|
|
|
protected $queriedContext = '';
|
2018-03-14 18:06:36 +01:00
|
|
|
protected $cacheTimeout;
|
2016-09-10 20:41:11 +02:00
|
|
|
|
2017-01-03 11:28:47 +01:00
|
|
|
/**
|
|
|
|
* Return cachable datas (extrainfos and items) stored in the bridge
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getCachable(){
|
2017-02-13 19:26:39 +01:00
|
|
|
return array(
|
2017-02-13 20:56:19 +01:00
|
|
|
'items' => $this->getItems(),
|
2017-02-13 19:26:39 +01:00
|
|
|
'extraInfos' => $this->getExtraInfos()
|
|
|
|
);
|
2017-01-03 11:28:47 +01:00
|
|
|
}
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
/**
|
|
|
|
* Return items stored in the bridge
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getItems(){
|
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
2017-02-13 19:26:39 +01:00
|
|
|
/**
|
2017-02-13 20:56:19 +01:00
|
|
|
* Sets the input values for a given context. Existing values are
|
2017-02-13 19:26:39 +01:00
|
|
|
* overwritten.
|
|
|
|
*
|
|
|
|
* @param array $inputs Associative array of inputs
|
|
|
|
* @param string $context The context name
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
protected function setInputs(array $inputs, $queriedContext){
|
|
|
|
// Import and assign all inputs to their context
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($inputs as $name => $value) {
|
|
|
|
foreach(static::PARAMETERS as $context => $set) {
|
|
|
|
if(array_key_exists($name, static::PARAMETERS[$context])) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$this->inputs[$context][$name]['value'] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply default values to missing data
|
|
|
|
$contexts = array($queriedContext);
|
2017-07-29 19:28:00 +02:00
|
|
|
if(array_key_exists('global', static::PARAMETERS)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$contexts[] = 'global';
|
|
|
|
}
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($contexts as $context) {
|
|
|
|
foreach(static::PARAMETERS[$context] as $name => $properties) {
|
|
|
|
if(isset($this->inputs[$context][$name]['value'])) {
|
2016-09-10 20:41:11 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = isset($properties['type']) ? $properties['type'] : 'text';
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
switch($type) {
|
2016-09-10 20:41:11 +02:00
|
|
|
case 'checkbox':
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!isset($properties['defaultValue'])) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$this->inputs[$context][$name]['value'] = false;
|
|
|
|
} else {
|
|
|
|
$this->inputs[$context][$name]['value'] = $properties['defaultValue'];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'list':
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!isset($properties['defaultValue'])) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$firstItem = reset($properties['values']);
|
2017-07-29 19:28:00 +02:00
|
|
|
if(is_array($firstItem)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$firstItem = reset($firstItem);
|
|
|
|
}
|
|
|
|
$this->inputs[$context][$name]['value'] = $firstItem;
|
|
|
|
} else {
|
|
|
|
$this->inputs[$context][$name]['value'] = $properties['defaultValue'];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2017-07-29 19:28:00 +02:00
|
|
|
if(isset($properties['defaultValue'])) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$this->inputs[$context][$name]['value'] = $properties['defaultValue'];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy global parameter values to the guessed context
|
2017-07-29 19:28:00 +02:00
|
|
|
if(array_key_exists('global', static::PARAMETERS)) {
|
|
|
|
foreach(static::PARAMETERS['global'] as $name => $properties) {
|
|
|
|
if(isset($inputs[$name])) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$value = $inputs[$name];
|
2017-07-29 19:28:00 +02:00
|
|
|
} elseif(isset($properties['value'])) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$value = $properties['value'];
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->inputs[$queriedContext][$name]['value'] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only keep guessed context parameters values
|
2017-07-29 19:28:00 +02:00
|
|
|
if(isset($this->inputs[$queriedContext])) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$this->inputs = array($queriedContext => $this->inputs[$queriedContext]);
|
|
|
|
} else {
|
|
|
|
$this->inputs = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 19:26:39 +01:00
|
|
|
/**
|
|
|
|
* Returns the name of the context matching the provided inputs
|
|
|
|
*
|
|
|
|
* @param array $inputs Associative array of inputs
|
|
|
|
* @return mixed Returns the context name or null if no match was found
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
protected function getQueriedContext(array $inputs){
|
|
|
|
$queriedContexts = array();
|
2017-02-13 19:26:39 +01:00
|
|
|
|
|
|
|
// Detect matching context
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach(static::PARAMETERS as $context => $set) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$queriedContexts[$context] = null;
|
2017-02-13 19:26:39 +01:00
|
|
|
|
|
|
|
// Check if all parameters of the context are satisfied
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($set as $id => $properties) {
|
|
|
|
if(isset($inputs[$id]) && !empty($inputs[$id])) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$queriedContexts[$context] = true;
|
|
|
|
} elseif(isset($properties['required'])
|
2017-07-29 19:28:00 +02:00
|
|
|
&& $properties['required'] === true) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$queriedContexts[$context] = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 19:26:39 +01:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
2017-02-13 19:26:39 +01:00
|
|
|
// Abort if one of the globally required parameters is not satisfied
|
2016-09-10 20:41:11 +02:00
|
|
|
if(array_key_exists('global', static::PARAMETERS)
|
2017-07-29 19:28:00 +02:00
|
|
|
&& $queriedContexts['global'] === false) {
|
2016-09-10 20:41:11 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
unset($queriedContexts['global']);
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
switch(array_sum($queriedContexts)) {
|
2017-02-13 19:26:39 +01:00
|
|
|
case 0: // Found no match, is there a context without parameters?
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($queriedContexts as $context => $queried) {
|
|
|
|
if(is_null($queried)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
return $context;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2017-02-13 19:26:39 +01:00
|
|
|
case 1: // Found unique match
|
|
|
|
return array_search(true, $queriedContexts);
|
2016-09-10 20:41:11 +02:00
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defined datas with parameters depending choose bridge
|
|
|
|
* Note : you can define a cache with "setCache"
|
|
|
|
* @param array array with expected bridge paramters
|
|
|
|
*/
|
|
|
|
public function setDatas(array $inputs){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!is_null($this->cache)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$time = $this->cache->getTime();
|
2016-10-02 17:00:53 +02:00
|
|
|
if($time !== false
|
2018-03-14 18:06:36 +01:00
|
|
|
&& (time() - $this->getCacheTimeout() < $time)
|
2017-07-29 19:28:00 +02:00
|
|
|
&& (!defined('DEBUG') || DEBUG !== true)) {
|
2017-01-03 11:28:47 +01:00
|
|
|
$cached = $this->cache->loadData();
|
2017-07-29 19:28:00 +02:00
|
|
|
if(isset($cached['items']) && isset($cached['extraInfos'])) {
|
2017-01-03 11:28:47 +01:00
|
|
|
$this->items = $cached['items'];
|
|
|
|
$this->extraInfos = $cached['extraInfos'];
|
|
|
|
return;
|
|
|
|
}
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(empty(static::PARAMETERS)) {
|
|
|
|
if(!empty($inputs)) {
|
2016-09-25 23:22:33 +02:00
|
|
|
returnClientError('Invalid parameters value(s)');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->collectData();
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!is_null($this->cache)) {
|
2017-01-03 11:28:47 +01:00
|
|
|
$this->cache->saveData($this->getCachable());
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!validateData($inputs, static::PARAMETERS)) {
|
2016-09-25 23:22:33 +02:00
|
|
|
returnClientError('Invalid parameters value(s)');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Guess the paramter context from input data
|
|
|
|
$this->queriedContext = $this->getQueriedContext($inputs);
|
2017-07-29 19:28:00 +02:00
|
|
|
if(is_null($this->queriedContext)) {
|
2016-09-25 23:22:33 +02:00
|
|
|
returnClientError('Required parameter(s) missing');
|
2017-07-29 19:28:00 +02:00
|
|
|
} elseif($this->queriedContext === false) {
|
2016-09-25 23:22:33 +02:00
|
|
|
returnClientError('Mixed context parameters');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->setInputs($inputs, $this->queriedContext);
|
|
|
|
|
|
|
|
$this->collectData();
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!is_null($this->cache)) {
|
2017-01-03 11:28:47 +01:00
|
|
|
$this->cache->saveData($this->getCachable());
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 19:26:39 +01:00
|
|
|
/**
|
|
|
|
* Returns the value for the provided input
|
|
|
|
*
|
|
|
|
* @param string $input The input name
|
|
|
|
* @return mixed Returns the input value or null if the input is not defined
|
|
|
|
*/
|
|
|
|
protected function getInput($input){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!isset($this->inputs[$this->queriedContext][$input]['value'])) {
|
2016-09-10 20:41:11 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->inputs[$this->queriedContext][$input]['value'];
|
|
|
|
}
|
|
|
|
|
2017-02-13 20:56:19 +01:00
|
|
|
public function getDescription(){
|
|
|
|
return static::DESCRIPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMaintainer(){
|
|
|
|
return static::MAINTAINER;
|
|
|
|
}
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public function getName(){
|
2017-04-23 21:06:23 +02:00
|
|
|
// Return cached name when bridge is using cached data
|
2017-07-29 19:28:00 +02:00
|
|
|
if(isset($this->extraInfos)) {
|
2017-04-23 21:06:23 +02:00
|
|
|
return $this->extraInfos['name'];
|
|
|
|
}
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
return static::NAME;
|
|
|
|
}
|
|
|
|
|
2017-02-13 20:56:19 +01:00
|
|
|
public function getParameters(){
|
|
|
|
return static::PARAMETERS;
|
|
|
|
}
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public function getURI(){
|
2017-04-23 21:06:23 +02:00
|
|
|
// Return cached uri when bridge is using cached data
|
2017-07-29 19:28:00 +02:00
|
|
|
if(isset($this->extraInfos)) {
|
2017-04-23 21:06:23 +02:00
|
|
|
return $this->extraInfos['uri'];
|
|
|
|
}
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
return static::URI;
|
|
|
|
}
|
2016-12-06 01:01:07 +01:00
|
|
|
|
2016-11-29 01:48:59 +01:00
|
|
|
public function getExtraInfos(){
|
2017-02-13 19:26:39 +01:00
|
|
|
return array(
|
2017-02-13 20:56:19 +01:00
|
|
|
'name' => $this->getName(),
|
2017-02-13 19:26:39 +01:00
|
|
|
'uri' => $this->getURI()
|
|
|
|
);
|
2016-11-29 01:48:59 +01:00
|
|
|
}
|
2016-09-10 20:41:11 +02:00
|
|
|
|
2016-10-08 14:52:03 +02:00
|
|
|
public function setCache(\CacheInterface $cache){
|
2016-09-10 20:41:11 +02:00
|
|
|
$this->cache = $cache;
|
|
|
|
}
|
2018-03-14 18:06:36 +01:00
|
|
|
|
|
|
|
public function setCacheTimeout($timeout){
|
|
|
|
if(is_numeric($timeout) && ($timeout < 1 || $timeout > 86400)) {
|
|
|
|
$this->cacheTimeout = static::CACHE_TIMEOUT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cacheTimeout = $timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheTimeout(){
|
|
|
|
return isset($this->cacheTimeout) ? $this->cacheTimeout : static::CACHE_TIMEOUT;
|
|
|
|
}
|
2016-09-05 18:05:19 +02:00
|
|
|
}
|