2016-09-05 19:05:19 +03:00
|
|
|
<?php
|
|
|
|
require_once(__DIR__ . '/BridgeInterface.php');
|
|
|
|
abstract class BridgeAbstract implements BridgeInterface {
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
const NAME = 'Unnamed bridge';
|
|
|
|
const URI = '';
|
|
|
|
const DESCRIPTION = 'No description provided';
|
|
|
|
const MAINTAINER = 'No maintainer';
|
2016-09-25 18:04:28 +03:00
|
|
|
const CACHE_TIMEOUT = 3600;
|
2016-09-10 21:41:11 +03:00
|
|
|
const PARAMETERS = array();
|
|
|
|
|
|
|
|
protected $items = array();
|
|
|
|
protected $inputs = array();
|
|
|
|
protected $queriedContext = '';
|
2017-01-03 13:28:47 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
/**
|
|
|
|
* Return items stored in the bridge
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getItems(){
|
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
2017-02-13 21:26:39 +03:00
|
|
|
/**
|
2017-02-13 22:56:19 +03:00
|
|
|
* Sets the input values for a given context. Existing values are
|
2017-02-13 21:26:39 +03:00
|
|
|
* overwritten.
|
|
|
|
*
|
|
|
|
* @param array $inputs Associative array of inputs
|
|
|
|
* @param string $context The context name
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
protected function setInputs(array $inputs, $queriedContext){
|
|
|
|
// Import and assign all inputs to their context
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach($inputs as $name => $value) {
|
|
|
|
foreach(static::PARAMETERS as $context => $set) {
|
|
|
|
if(array_key_exists($name, static::PARAMETERS[$context])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$this->inputs[$context][$name]['value'] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply default values to missing data
|
|
|
|
$contexts = array($queriedContext);
|
2017-07-29 20:28:00 +03:00
|
|
|
if(array_key_exists('global', static::PARAMETERS)) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$contexts[] = 'global';
|
|
|
|
}
|
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach($contexts as $context) {
|
|
|
|
foreach(static::PARAMETERS[$context] as $name => $properties) {
|
|
|
|
if(isset($this->inputs[$context][$name]['value'])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = isset($properties['type']) ? $properties['type'] : 'text';
|
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
switch($type) {
|
2016-09-10 21:41:11 +03:00
|
|
|
case 'checkbox':
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!isset($properties['defaultValue'])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$this->inputs[$context][$name]['value'] = false;
|
|
|
|
} else {
|
|
|
|
$this->inputs[$context][$name]['value'] = $properties['defaultValue'];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'list':
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!isset($properties['defaultValue'])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$firstItem = reset($properties['values']);
|
2017-07-29 20:28:00 +03:00
|
|
|
if(is_array($firstItem)) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$firstItem = reset($firstItem);
|
|
|
|
}
|
|
|
|
$this->inputs[$context][$name]['value'] = $firstItem;
|
|
|
|
} else {
|
|
|
|
$this->inputs[$context][$name]['value'] = $properties['defaultValue'];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2017-07-29 20:28:00 +03:00
|
|
|
if(isset($properties['defaultValue'])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$this->inputs[$context][$name]['value'] = $properties['defaultValue'];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy global parameter values to the guessed context
|
2017-07-29 20:28:00 +03:00
|
|
|
if(array_key_exists('global', static::PARAMETERS)) {
|
|
|
|
foreach(static::PARAMETERS['global'] as $name => $properties) {
|
|
|
|
if(isset($inputs[$name])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$value = $inputs[$name];
|
2017-07-29 20:28:00 +03:00
|
|
|
} elseif(isset($properties['value'])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$value = $properties['value'];
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->inputs[$queriedContext][$name]['value'] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only keep guessed context parameters values
|
2017-07-29 20:28:00 +03:00
|
|
|
if(isset($this->inputs[$queriedContext])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
$this->inputs = array($queriedContext => $this->inputs[$queriedContext]);
|
|
|
|
} else {
|
|
|
|
$this->inputs = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defined datas with parameters depending choose bridge
|
|
|
|
* @param array array with expected bridge paramters
|
|
|
|
*/
|
|
|
|
public function setDatas(array $inputs){
|
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
if(empty(static::PARAMETERS)) {
|
2018-10-15 18:21:43 +03:00
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!empty($inputs)) {
|
2016-09-26 00:22:33 +03:00
|
|
|
returnClientError('Invalid parameters value(s)');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2018-10-15 18:21:43 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
2018-09-22 17:42:04 +03:00
|
|
|
$validator = new ParameterValidator();
|
|
|
|
|
|
|
|
if(!$validator->validateData($inputs, static::PARAMETERS)) {
|
|
|
|
$parameters = array_map(
|
|
|
|
function($i){ return $i['name']; }, // Just display parameter names
|
|
|
|
$validator->getInvalidParameters()
|
|
|
|
);
|
|
|
|
|
|
|
|
returnClientError(
|
|
|
|
'Invalid parameters value(s): '
|
|
|
|
. implode(', ', $parameters)
|
|
|
|
);
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Guess the paramter context from input data
|
2018-09-22 18:04:55 +03:00
|
|
|
$this->queriedContext = $validator->getQueriedContext($inputs, static::PARAMETERS);
|
2017-07-29 20:28:00 +03:00
|
|
|
if(is_null($this->queriedContext)) {
|
2016-09-26 00:22:33 +03:00
|
|
|
returnClientError('Required parameter(s) missing');
|
2017-07-29 20:28:00 +03:00
|
|
|
} elseif($this->queriedContext === false) {
|
2016-09-26 00:22:33 +03:00
|
|
|
returnClientError('Mixed context parameters');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->setInputs($inputs, $this->queriedContext);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-13 21:26:39 +03: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 20:28:00 +03:00
|
|
|
if(!isset($this->inputs[$this->queriedContext][$input]['value'])) {
|
2016-09-10 21:41:11 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->inputs[$this->queriedContext][$input]['value'];
|
|
|
|
}
|
|
|
|
|
2017-02-13 22:56:19 +03:00
|
|
|
public function getDescription(){
|
|
|
|
return static::DESCRIPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMaintainer(){
|
|
|
|
return static::MAINTAINER;
|
|
|
|
}
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function getName(){
|
|
|
|
return static::NAME;
|
|
|
|
}
|
|
|
|
|
2018-08-21 18:46:47 +03:00
|
|
|
public function getIcon(){
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2017-02-13 22:56:19 +03:00
|
|
|
public function getParameters(){
|
|
|
|
return static::PARAMETERS;
|
|
|
|
}
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function getURI(){
|
|
|
|
return static::URI;
|
|
|
|
}
|
2016-12-06 03:01:07 +03:00
|
|
|
|
2018-03-14 20:06:36 +03:00
|
|
|
public function getCacheTimeout(){
|
2018-10-15 18:21:43 +03:00
|
|
|
return static::CACHE_TIMEOUT;
|
2018-03-14 20:06:36 +03:00
|
|
|
}
|
2018-08-25 22:00:38 +03:00
|
|
|
|
2016-09-05 19:05:19 +03:00
|
|
|
}
|