* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBEvent_Handler'); /** * Event Handler Config to Event Data * * Set event data by config * * @version 1.1.0 * @package WB * @subpackage base */ class WBEvent_Handler_Config2Event extends WBEvent_Handler { /** * Handler config * * Parameters: * - condition: default null * - switch: * - default: * - target: * * If parameter condition is empty, any config parameter will be copied to event data. * * In case there is condition, it will be used as some kind of switch-case. Where condition * is the event data variable's name * * @var array */ protected $config = array( 'condition' => null, 'default' => null, 'target' => null, 'switch' => array() ); /** * Set Event Data From Config * * @see isRecoverable() * @param WBEvent $e * @return bool true to continue, false to stop processing */ public function process(WBEvent $e) { // copy everything if (!isset($this->config['condition'])) { foreach ($this->config as $k => $v) { if (is_null($v)) { continue; } $e->set($k, $v); } return true; } if (empty($this->config['target'])) { WBClass::load('WBException_Config'); throw new WBException_Datasource('Check config, there is no target set.', 1, __CLASS__); return false; } $cond = $e->get($this->config['condition'], ''); if (empty($cond)) { WBClass::load('WBException_Config'); throw new WBException_Datasource('Check config, condition variable of event must not be empty.', 2, __CLASS__); return false; } if (empty($this->config['switch']) || !is_array($this->config['switch'])) { WBClass::load('WBException_Config'); throw new WBException_Datasource('Check config, switch is empty or not an (associative) array.', 3, __CLASS__); return false; } if (isset($this->config['switch'][$cond])) { $e->set($this->config['target'], $this->config['switch'][$cond]); return true; } if (isset($this->config['default'])) { $e->set($this->config['target'], $this->config['default']); } return true; } }