diff --git a/src/Symfony/Components/EventDispatcher/Event.php b/src/Symfony/Components/EventDispatcher/Event.php index 5b81d4cb67..21c79d1fd5 100644 --- a/src/Symfony/Components/EventDispatcher/Event.php +++ b/src/Symfony/Components/EventDispatcher/Event.php @@ -109,6 +109,46 @@ class Event implements \ArrayAccess return $this->parameters; } + /** + * Returns true if the parameter exists. + * + * @param string $name The parameter name + * + * @return Boolean true if the parameter exists, false otherwise + */ + public function hasParameter($name) + { + return array_key_exists($name, $this->parameters); + } + + /** + * Returns a parameter value. + * + * @param string $name The parameter name + * + * @return mixed The parameter value + */ + public function getParameter($name) + { + if (!array_key_exists($name, $this->parameters)) + { + throw new \InvalidArgumentException(sprintf('The event "%s" has no "%s" parameter.', $this->name, $name)); + } + + return $this->parameters[$name]; + } + + /** + * Sets a parameter. + * + * @param string $name The parameter name + * @param mixed $value The parameter value + */ + public function setParameter($name, $value) + { + $this->parameters[$name] = $value; + } + /** * Returns true if the parameter exists (implements the ArrayAccess interface). * diff --git a/tests/unit/Symfony/Components/EventDispatcher/EventTest.php b/tests/unit/Symfony/Components/EventDispatcher/EventTest.php index 024801b429..0d4961437c 100644 --- a/tests/unit/Symfony/Components/EventDispatcher/EventTest.php +++ b/tests/unit/Symfony/Components/EventDispatcher/EventTest.php @@ -12,7 +12,7 @@ require_once __DIR__.'/../../../bootstrap.php'; use Symfony\Components\EventDispatcher\Event; -$t = new LimeTest(11); +$t = new LimeTest(16); $subject = new stdClass(); $parameters = array('foo' => 'bar'); @@ -26,9 +26,26 @@ $t->is($event->getSubject(), $subject, '->getSubject() returns the event subject $t->diag('->getName()'); $t->is($event->getName(), 'name', '->getName() returns the event name'); -// ->getParameters() +// ->getParameters() ->setParameter() ->hasParameter() ->getParameter() $t->diag('->getParameters()'); $t->is($event->getParameters(), $parameters, '->getParameters() returns the event parameters'); +$t->is($event->getParameter('foo'), 'bar', '->getParameter() returns the value of a parameter'); +$event->setParameter('foo', 'foo'); +$t->is($event->getParameter('foo'), 'foo', '->setParameter() changes the value of a parameter'); +$t->ok($event->hasParameter('foo'), '->hasParameter() returns true if the parameter is defined'); +unset($event['foo']); +$t->ok(!$event->hasParameter('foo'), '->hasParameter() returns false if the parameter is not defined'); + +try +{ + $event->getParameter('foobar'); + $t->fail('->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist'); +} +catch (\InvalidArgumentException $e) +{ + $t->pass('->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist'); +} +$event = new Event($subject, 'name', $parameters); // ->getReturnValue() ->setReturnValue() $t->diag('->getReturnValue() ->setReturnValue()');