* @license PHP License * @package WB * @subpackage base */ /** * Event * * Trigger event and hold event data * * @version 0.4.0 * @package WB * @subpackage base */ class WBEvent extends WBStdClass { /** * event name * @var string */ protected $name; /** * internal event message * @var string */ protected $msg; /** * optional but usefull event data * @var array */ protected $data = array(); /** * creation timestamp of event * @var string */ protected $timestamp; /** * static constructor * * load required classes after loading this class */ static public function staticConstruct() { WBClass::load('WBEvent_Queue'); } /** * Trigger event * * tell system that something has happened * * @param string $name * @param string $msg * @param array $data */ static public function trigger($name, $msg, $data = array()) { if (empty($data) || !is_array($data)) { $data = array(); } $q = WBEvent_Queue::create(); $q->add($name, $msg, $data, gmdate('Y-m-d H:i:s')); } /** * create event object * * use this function instead of constructor * * @param string $name * @param string $msg * @param attay $data * @param string $timestamp * @return WBEvent */ public static function create($name, $msg, $data, $timestamp) { if (empty($data) || !is_array($data)) { $data = array(); } $ev = new WBEvent($name, $msg, $data, $timestamp); return $ev; } /** * private constructor * * make sure nobody uses the constructor * * @see create() * @param string $name * @param string $msg * @param attay $data * @param string $timestamp */ private function __construct($name, $msg, $data, $timestamp) { $this->name = $name; $this->msg = $msg; $this->data = $data; $this->timestamp = $timestamp; } /** * deny cloning object */ private function __clone() { } /** * get event name * * @return string */ public function getName() { return $this->name; } /** * get formatedevent message * * @return string */ public function getMessage() { WBClass::load('WBString'); return WBString::populate($this->msg, $this->data); } /** * Get timestamp * * @return string ISO date time format */ public function getTimestamp() { return $this->timestamp; } /** * Get Value from Data Array * * Key is a path string, like "email" or "save/email". * Path with slashes allows to dig into associative arrays * * @param string $key * @param mixed default value if key was not found * @return mixed */ public function get($key = null, $default = null) { if ($key === null) { return $this->data; } if (strstr($key, '/')) { $key = explode('/', $key); } else { $key = array($key); } $data = $this->data; while (!empty($key)) { $k = array_shift($key); if (isset($data[$k])) { $data = $data[$k]; } else { return $default; } } return $data; } /** * Set event data * * Allow to change event's data during event's live time. * @param string $key * @param mixed $value */ public function set($key, $value = null) { $this->data[$key] = $value; } }