1
0

BridgeAbstract.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. require_once(__DIR__ . '/BridgeInterface.php');
  3. abstract class BridgeAbstract implements BridgeInterface {
  4. const NAME = 'Unnamed bridge';
  5. const URI = '';
  6. const DESCRIPTION = 'No description provided';
  7. const MAINTAINER = 'No maintainer';
  8. const CACHE_TIMEOUT = 3600;
  9. const PARAMETERS = array();
  10. protected $cache;
  11. protected $items = array();
  12. protected $inputs = array();
  13. protected $queriedContext = '';
  14. /**
  15. * Return items stored in the bridge
  16. * @return mixed
  17. */
  18. public function getItems(){
  19. return $this->items;
  20. }
  21. protected function setInputs(array $inputs, $queriedContext){
  22. // Import and assign all inputs to their context
  23. foreach($inputs as $name => $value){
  24. foreach(static::PARAMETERS as $context => $set){
  25. if(array_key_exists($name, static::PARAMETERS[$context])){
  26. $this->inputs[$context][$name]['value'] = $value;
  27. }
  28. }
  29. }
  30. // Apply default values to missing data
  31. $contexts = array($queriedContext);
  32. if(array_key_exists('global', static::PARAMETERS)){
  33. $contexts[] = 'global';
  34. }
  35. foreach($contexts as $context){
  36. foreach(static::PARAMETERS[$context] as $name => $properties){
  37. if(isset($this->inputs[$context][$name]['value'])){
  38. continue;
  39. }
  40. $type = isset($properties['type']) ? $properties['type'] : 'text';
  41. switch($type){
  42. case 'checkbox':
  43. if(!isset($properties['defaultValue'])){
  44. $this->inputs[$context][$name]['value'] = false;
  45. } else {
  46. $this->inputs[$context][$name]['value'] = $properties['defaultValue'];
  47. }
  48. break;
  49. case 'list':
  50. if(!isset($properties['defaultValue'])){
  51. $firstItem = reset($properties['values']);
  52. if(is_array($firstItem)){
  53. $firstItem = reset($firstItem);
  54. }
  55. $this->inputs[$context][$name]['value'] = $firstItem;
  56. } else {
  57. $this->inputs[$context][$name]['value'] = $properties['defaultValue'];
  58. }
  59. break;
  60. default:
  61. if(isset($properties['defaultValue'])){
  62. $this->inputs[$context][$name]['value'] = $properties['defaultValue'];
  63. }
  64. break;
  65. }
  66. }
  67. }
  68. // Copy global parameter values to the guessed context
  69. if(array_key_exists('global', static::PARAMETERS)){
  70. foreach(static::PARAMETERS['global'] as $name => $properties){
  71. if(isset($inputs[$name])){
  72. $value = $inputs[$name];
  73. } elseif (isset($properties['value'])){
  74. $value = $properties['value'];
  75. } else {
  76. continue;
  77. }
  78. $this->inputs[$queriedContext][$name]['value'] = $value;
  79. }
  80. }
  81. // Only keep guessed context parameters values
  82. if(isset($this->inputs[$queriedContext])){
  83. $this->inputs = array($queriedContext => $this->inputs[$queriedContext]);
  84. } else {
  85. $this->inputs = array();
  86. }
  87. }
  88. protected function getQueriedContext(array $inputs){
  89. $queriedContexts = array();
  90. foreach(static::PARAMETERS as $context => $set){
  91. $queriedContexts[$context] = null;
  92. foreach($set as $id => $properties){
  93. if(isset($inputs[$id]) && !empty($inputs[$id])){
  94. $queriedContexts[$context] = true;
  95. } elseif(isset($properties['required'])
  96. && $properties['required'] === true){
  97. $queriedContexts[$context] = false;
  98. break;
  99. }
  100. }
  101. }
  102. if(array_key_exists('global', static::PARAMETERS)
  103. && $queriedContexts['global'] === false){
  104. return null;
  105. }
  106. unset($queriedContexts['global']);
  107. switch(array_sum($queriedContexts)){
  108. case 0:
  109. foreach($queriedContexts as $context => $queried){
  110. if (is_null($queried)){
  111. return $context;
  112. }
  113. }
  114. return null;
  115. case 1: return array_search(true, $queriedContexts);
  116. default: return false;
  117. }
  118. }
  119. /**
  120. * Defined datas with parameters depending choose bridge
  121. * Note : you can define a cache with "setCache"
  122. * @param array array with expected bridge paramters
  123. */
  124. public function setDatas(array $inputs){
  125. if(!is_null($this->cache)){
  126. $time = $this->cache->getTime();
  127. if($time !== false
  128. && (time() - static::CACHE_TIMEOUT < $time)
  129. && (!defined('DEBUG') || DEBUG !== true)){
  130. $this->items = $this->cache->loadData();
  131. return;
  132. }
  133. }
  134. if(empty(static::PARAMETERS)){
  135. if(!empty($inputs)){
  136. returnClientError('Invalid parameters value(s)');
  137. }
  138. $this->collectData();
  139. if(!is_null($this->cache)){
  140. $this->cache->saveData($this->getItems());
  141. }
  142. return;
  143. }
  144. if(!validateData($inputs, static::PARAMETERS)){
  145. returnClientError('Invalid parameters value(s)');
  146. }
  147. // Guess the paramter context from input data
  148. $this->queriedContext = $this->getQueriedContext($inputs);
  149. if(is_null($this->queriedContext)){
  150. returnClientError('Required parameter(s) missing');
  151. } elseif($this->queriedContext === false){
  152. returnClientError('Mixed context parameters');
  153. }
  154. $this->setInputs($inputs, $this->queriedContext);
  155. $this->collectData();
  156. if(!is_null($this->cache)){
  157. $this->cache->saveData($this->getItems());
  158. }
  159. }
  160. function getInput($input){
  161. if(!isset($this->inputs[$this->queriedContext][$input]['value'])){
  162. return null;
  163. }
  164. return $this->inputs[$this->queriedContext][$input]['value'];
  165. }
  166. public function getName(){
  167. return static::NAME;
  168. }
  169. public function getURI(){
  170. return static::URI;
  171. }
  172. public function setCache(\CacheInterface $cache){
  173. $this->cache = $cache;
  174. }
  175. }