|
|
@@ -18,8 +18,13 @@ namespace Cake\Event;
|
|
|
* Represents the transport class of events across the system. It receives a name, subject and an optional
|
|
|
* payload. The name can be any string that uniquely identifies the event across the application, while the subject
|
|
|
* represents the object that the event applies to.
|
|
|
+ *
|
|
|
+ * @property string $name Name of the event
|
|
|
+ * @property object $subject The object this event applies to
|
|
|
+ * @property mixed $result Property used to retain the result value of the event listeners
|
|
|
+ * @property array $data Custom data for the method that receives the event
|
|
|
*/
|
|
|
-class Event
|
|
|
+class Event implements EventInterface
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
@@ -39,16 +44,16 @@ class Event
|
|
|
/**
|
|
|
* Custom data for the method that receives the event
|
|
|
*
|
|
|
- * @var mixed
|
|
|
+ * @var array
|
|
|
*/
|
|
|
- public $data = null;
|
|
|
+ protected $_data;
|
|
|
|
|
|
/**
|
|
|
* Property used to retain the result value of the event listeners
|
|
|
*
|
|
|
* @var mixed
|
|
|
*/
|
|
|
- public $result = null;
|
|
|
+ protected $_result = null;
|
|
|
|
|
|
/**
|
|
|
* Flags an event as stopped or not, default is false
|
|
|
@@ -73,13 +78,17 @@ class Event
|
|
|
*/
|
|
|
public function __construct($name, $subject = null, $data = null)
|
|
|
{
|
|
|
+ if ($data !== null && !is_array($data)) {
|
|
|
+ trigger_error('Data parameter will be changing to array hint typing', E_USER_DEPRECATED);
|
|
|
+ }
|
|
|
+
|
|
|
$this->_name = $name;
|
|
|
- $this->data = $data;
|
|
|
+ $this->_data = (array)$data;
|
|
|
$this->_subject = $subject;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Dynamically returns the name and subject if accessed directly
|
|
|
+ * Provides read-only access for the name and subject properties.
|
|
|
*
|
|
|
* @param string $attribute Attribute name.
|
|
|
* @return mixed
|
|
|
@@ -89,6 +98,36 @@ class Event
|
|
|
if ($attribute === 'name' || $attribute === 'subject') {
|
|
|
return $this->{$attribute}();
|
|
|
}
|
|
|
+ if ($attribute === 'data')
|
|
|
+ {
|
|
|
+ trigger_error('Public read access to data is deprecated, use data()', E_USER_DEPRECATED);
|
|
|
+ return $this->_data;
|
|
|
+ }
|
|
|
+ if ($attribute === 'result')
|
|
|
+ {
|
|
|
+ trigger_error('Public read access to result is deprecated, use result()', E_USER_DEPRECATED);
|
|
|
+ return $this->_result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Provides backward compatibility for write access to data and result properties.
|
|
|
+ *
|
|
|
+ * @param string $attribute Attribute name.
|
|
|
+ * @param mixed $value
|
|
|
+ */
|
|
|
+ public function __set($attribute, $value)
|
|
|
+ {
|
|
|
+ if($attribute === 'data')
|
|
|
+ {
|
|
|
+ trigger_error('Public write access to data is deprecated, use setData()', E_USER_DEPRECATED);
|
|
|
+ $this->_data = (array)$value;
|
|
|
+ }
|
|
|
+ if ($attribute === 'result')
|
|
|
+ {
|
|
|
+ trigger_error('Public write access to result is deprecated, use setResult()', E_USER_DEPRECATED);
|
|
|
+ $this->_result = $value;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -132,12 +171,52 @@ class Event
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * The result value of the event listeners
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function result()
|
|
|
+ {
|
|
|
+ return $this->_result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param null $value
|
|
|
+ * @return $this
|
|
|
+ */
|
|
|
+ public function setResult($value = null)
|
|
|
+ {
|
|
|
+ $this->_result = $value;
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* Access the event data/payload.
|
|
|
*
|
|
|
- * @return array
|
|
|
+ * @param string|null $key
|
|
|
+ * @return array|null The data payload if $key is null, or the data value for the given $key. If the $key does not
|
|
|
+ * exist a null value is returned.
|
|
|
+ */
|
|
|
+ public function data($key = null)
|
|
|
+ {
|
|
|
+ if ($key !== null) {
|
|
|
+ return isset($this->_data[$key]) ? $this->_data[$key] : null;
|
|
|
+ }
|
|
|
+ return (array)$this->_data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param array|string $key
|
|
|
+ * @param mixed $value
|
|
|
+ * @return $this
|
|
|
*/
|
|
|
- public function data()
|
|
|
+ public function setData($key, $value = null)
|
|
|
{
|
|
|
- return (array)$this->data;
|
|
|
+ if(is_array($key)) {
|
|
|
+ $this->_data = $key;
|
|
|
+ } else {
|
|
|
+ $this->_data[$key] = $value;
|
|
|
+ }
|
|
|
+ return $this;
|
|
|
}
|
|
|
}
|