1
0

BridgeAbstract.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 $extraInfos;
  12. protected $items = array();
  13. protected $inputs = array();
  14. protected $queriedContext = '';
  15. /**
  16. * Return cachable datas (extrainfos and items) stored in the bridge
  17. * @return mixed
  18. */
  19. public function getCachable(){
  20. return array(
  21. 'items' => $this->getItems(),
  22. 'extraInfos' => $this->getExtraInfos()
  23. );
  24. }
  25. /**
  26. * Return items stored in the bridge
  27. * @return mixed
  28. */
  29. public function getItems(){
  30. return $this->items;
  31. }
  32. /**
  33. * Sets the input values for a given context. Existing values are
  34. * overwritten.
  35. *
  36. * @param array $inputs Associative array of inputs
  37. * @param string $context The context name
  38. */
  39. protected function setInputs(array $inputs, $queriedContext){
  40. // Import and assign all inputs to their context
  41. foreach($inputs as $name => $value) {
  42. foreach(static::PARAMETERS as $context => $set) {
  43. if(array_key_exists($name, static::PARAMETERS[$context])) {
  44. $this->inputs[$context][$name]['value'] = $value;
  45. }
  46. }
  47. }
  48. // Apply default values to missing data
  49. $contexts = array($queriedContext);
  50. if(array_key_exists('global', static::PARAMETERS)) {
  51. $contexts[] = 'global';
  52. }
  53. foreach($contexts as $context) {
  54. foreach(static::PARAMETERS[$context] as $name => $properties) {
  55. if(isset($this->inputs[$context][$name]['value'])) {
  56. continue;
  57. }
  58. $type = isset($properties['type']) ? $properties['type'] : 'text';
  59. switch($type) {
  60. case 'checkbox':
  61. if(!isset($properties['defaultValue'])) {
  62. $this->inputs[$context][$name]['value'] = false;
  63. } else {
  64. $this->inputs[$context][$name]['value'] = $properties['defaultValue'];
  65. }
  66. break;
  67. case 'list':
  68. if(!isset($properties['defaultValue'])) {
  69. $firstItem = reset($properties['values']);
  70. if(is_array($firstItem)) {
  71. $firstItem = reset($firstItem);
  72. }
  73. $this->inputs[$context][$name]['value'] = $firstItem;
  74. } else {
  75. $this->inputs[$context][$name]['value'] = $properties['defaultValue'];
  76. }
  77. break;
  78. default:
  79. if(isset($properties['defaultValue'])) {
  80. $this->inputs[$context][$name]['value'] = $properties['defaultValue'];
  81. }
  82. break;
  83. }
  84. }
  85. }
  86. // Copy global parameter values to the guessed context
  87. if(array_key_exists('global', static::PARAMETERS)) {
  88. foreach(static::PARAMETERS['global'] as $name => $properties) {
  89. if(isset($inputs[$name])) {
  90. $value = $inputs[$name];
  91. } elseif(isset($properties['value'])) {
  92. $value = $properties['value'];
  93. } else {
  94. continue;
  95. }
  96. $this->inputs[$queriedContext][$name]['value'] = $value;
  97. }
  98. }
  99. // Only keep guessed context parameters values
  100. if(isset($this->inputs[$queriedContext])) {
  101. $this->inputs = array($queriedContext => $this->inputs[$queriedContext]);
  102. } else {
  103. $this->inputs = array();
  104. }
  105. }
  106. /**
  107. * Returns the name of the context matching the provided inputs
  108. *
  109. * @param array $inputs Associative array of inputs
  110. * @return mixed Returns the context name or null if no match was found
  111. */
  112. protected function getQueriedContext(array $inputs){
  113. $queriedContexts = array();
  114. // Detect matching context
  115. foreach(static::PARAMETERS as $context => $set) {
  116. $queriedContexts[$context] = null;
  117. // Check if all parameters of the context are satisfied
  118. foreach($set as $id => $properties) {
  119. if(isset($inputs[$id]) && !empty($inputs[$id])) {
  120. $queriedContexts[$context] = true;
  121. } elseif(isset($properties['required'])
  122. && $properties['required'] === true) {
  123. $queriedContexts[$context] = false;
  124. break;
  125. }
  126. }
  127. }
  128. // Abort if one of the globally required parameters is not satisfied
  129. if(array_key_exists('global', static::PARAMETERS)
  130. && $queriedContexts['global'] === false) {
  131. return null;
  132. }
  133. unset($queriedContexts['global']);
  134. switch(array_sum($queriedContexts)) {
  135. case 0: // Found no match, is there a context without parameters?
  136. foreach($queriedContexts as $context => $queried) {
  137. if(is_null($queried)) {
  138. return $context;
  139. }
  140. }
  141. return null;
  142. case 1: // Found unique match
  143. return array_search(true, $queriedContexts);
  144. default: return false;
  145. }
  146. }
  147. /**
  148. * Defined datas with parameters depending choose bridge
  149. * Note : you can define a cache with "setCache"
  150. * @param array array with expected bridge paramters
  151. */
  152. public function setDatas(array $inputs){
  153. if(!is_null($this->cache)) {
  154. $time = $this->cache->getTime();
  155. if($time !== false
  156. && (time() - static::CACHE_TIMEOUT < $time)
  157. && (!defined('DEBUG') || DEBUG !== true)) {
  158. $cached = $this->cache->loadData();
  159. if(isset($cached['items']) && isset($cached['extraInfos'])) {
  160. $this->items = $cached['items'];
  161. $this->extraInfos = $cached['extraInfos'];
  162. return;
  163. }
  164. }
  165. }
  166. if(empty(static::PARAMETERS)) {
  167. if(!empty($inputs)) {
  168. returnClientError('Invalid parameters value(s)');
  169. }
  170. $this->collectData();
  171. if(!is_null($this->cache)) {
  172. $this->cache->saveData($this->getCachable());
  173. }
  174. return;
  175. }
  176. if(!validateData($inputs, static::PARAMETERS)) {
  177. returnClientError('Invalid parameters value(s)');
  178. }
  179. // Guess the paramter context from input data
  180. $this->queriedContext = $this->getQueriedContext($inputs);
  181. if(is_null($this->queriedContext)) {
  182. returnClientError('Required parameter(s) missing');
  183. } elseif($this->queriedContext === false) {
  184. returnClientError('Mixed context parameters');
  185. }
  186. $this->setInputs($inputs, $this->queriedContext);
  187. $this->collectData();
  188. if(!is_null($this->cache)) {
  189. $this->cache->saveData($this->getCachable());
  190. }
  191. }
  192. /**
  193. * Returns the value for the provided input
  194. *
  195. * @param string $input The input name
  196. * @return mixed Returns the input value or null if the input is not defined
  197. */
  198. protected function getInput($input){
  199. if(!isset($this->inputs[$this->queriedContext][$input]['value'])) {
  200. return null;
  201. }
  202. return $this->inputs[$this->queriedContext][$input]['value'];
  203. }
  204. public function getDescription(){
  205. return static::DESCRIPTION;
  206. }
  207. public function getMaintainer(){
  208. return static::MAINTAINER;
  209. }
  210. public function getName(){
  211. // Return cached name when bridge is using cached data
  212. if(isset($this->extraInfos)) {
  213. return $this->extraInfos['name'];
  214. }
  215. return static::NAME;
  216. }
  217. public function getParameters(){
  218. return static::PARAMETERS;
  219. }
  220. public function getURI(){
  221. // Return cached uri when bridge is using cached data
  222. if(isset($this->extraInfos)) {
  223. return $this->extraInfos['uri'];
  224. }
  225. return static::URI;
  226. }
  227. public function getExtraInfos(){
  228. return array(
  229. 'name' => $this->getName(),
  230. 'uri' => $this->getURI()
  231. );
  232. }
  233. public function setCache(\CacheInterface $cache){
  234. $this->cache = $cache;
  235. }
  236. }