Browse Source

Merge pull request #9996 from cakephp/3next-event-methods

Add new style get methods to Event.
Mark Story 9 years ago
parent
commit
c2f0b9dd39
2 changed files with 52 additions and 0 deletions
  1. 46 0
      src/Event/Event.php
  2. 6 0
      tests/TestCase/Event/EventTest.php

+ 46 - 0
src/Event/Event.php

@@ -125,6 +125,7 @@ class Event
      * Returns the name of this event. This is usually used as the event identifier
      *
      * @return string
+     * @deprecated 3.4.0 use getName() instead.
      */
     public function name()
     {
@@ -132,9 +133,20 @@ class Event
     }
 
     /**
+     * Returns the name of this event. This is usually used as the event identifier
+     *
+     * @return string
+     */
+    public function getName()
+    {
+        return $this->_name;
+    }
+
+    /**
      * Returns the subject of this event
      *
      * @return object
+     * @deprecated 3.4.0 use getSubject() instead.
      */
     public function subject()
     {
@@ -142,6 +154,16 @@ class Event
     }
 
     /**
+     * Returns the subject of this event
+     *
+     * @return object
+     */
+    public function getSubject()
+    {
+        return $this->_subject;
+    }
+
+    /**
      * Stops the event from being used anymore
      *
      * @return void
@@ -165,6 +187,7 @@ class Event
      * The result value of the event listeners
      *
      * @return mixed
+     * @deprecated 3.4.0 use getResult() instead.
      */
     public function result()
     {
@@ -172,6 +195,16 @@ class Event
     }
 
     /**
+     * The result value of the event listeners
+     *
+     * @return mixed
+     */
+    public function getResult()
+    {
+        return $this->_result;
+    }
+
+    /**
      * Listeners can attach a result value to the event.
      *
      * @param mixed $value The value to set.
@@ -190,9 +223,22 @@ class Event
      * @param string|null $key The data payload element to return, or null to return all data.
      * @return array|mixed|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.
+     * @deprecated 3.4.0 use getData() instead.
      */
     public function data($key = null)
     {
+        return $this->getData($key);
+    }
+
+    /**
+     * Access the event data/payload.
+     *
+     * @param string|null $key The data payload element to return, or null to return all data.
+     * @return array|mixed|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 getData($key = null)
+    {
         if ($key !== null) {
             return isset($this->_data[$key]) ? $this->_data[$key] : null;
         }

+ 6 - 0
tests/TestCase/Event/EventTest.php

@@ -37,6 +37,7 @@ class EventTest extends TestCase
     {
         $event = new Event('fake.event');
         $this->assertEquals('fake.event', $event->name());
+        $this->assertEquals('fake.event', $event->getName());
     }
 
     /**
@@ -50,6 +51,7 @@ class EventTest extends TestCase
     {
         $event = new Event('fake.event', $this);
         $this->assertSame($this, $event->subject());
+        $this->assertSame($this, $event->getSubject());
 
         $event = new Event('fake.event');
         $this->assertNull($event->subject());
@@ -79,6 +81,10 @@ class EventTest extends TestCase
     {
         $event = new Event('fake.event', $this, ['some' => 'data']);
         $this->assertEquals(['some' => 'data'], $event->data());
+        $this->assertEquals(['some' => 'data'], $event->getData());
+
+        $this->assertEquals('data', $event->getData('some'));
+        $this->assertNull($event->getData('undef'));
     }
 
     /**