Event.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\EventDispatcher;
  11. /**
  12. * Event.
  13. *
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. */
  16. class Event implements EventInterface
  17. {
  18. protected $processed = false;
  19. protected $subject;
  20. protected $name;
  21. protected $parameters;
  22. /**
  23. * Constructs a new Event.
  24. *
  25. * @param mixed $subject The subject
  26. * @param string $name The event name
  27. * @param array $parameters An array of parameters
  28. */
  29. public function __construct($subject, $name, $parameters = array())
  30. {
  31. $this->subject = $subject;
  32. $this->name = $name;
  33. $this->parameters = $parameters;
  34. }
  35. /**
  36. * Returns the subject.
  37. *
  38. * @return mixed The subject
  39. */
  40. public function getSubject()
  41. {
  42. return $this->subject;
  43. }
  44. /**
  45. * Returns the event name.
  46. *
  47. * @return string The event name
  48. */
  49. public function getName()
  50. {
  51. return $this->name;
  52. }
  53. /**
  54. * Sets the processed flag to true.
  55. *
  56. * This method must be called by listeners when
  57. * it has processed the event (it is only meaninful
  58. * when the event has been notified with the notifyUntil()
  59. * dispatcher method.
  60. */
  61. public function setProcessed()
  62. {
  63. $this->processed = true;
  64. }
  65. /**
  66. * Returns whether the event has been processed by a listener or not.
  67. *
  68. * This method is only meaningful for events notified
  69. * with notifyUntil().
  70. *
  71. * @return Boolean true if the event has been processed, false otherwise
  72. */
  73. public function isProcessed()
  74. {
  75. return $this->processed;
  76. }
  77. /**
  78. * Returns the event parameters.
  79. *
  80. * @return array The event parameters
  81. */
  82. public function all()
  83. {
  84. return $this->parameters;
  85. }
  86. /**
  87. * Returns true if the parameter exists.
  88. *
  89. * @param string $name The parameter name
  90. *
  91. * @return Boolean true if the parameter exists, false otherwise
  92. */
  93. public function has($name)
  94. {
  95. return array_key_exists($name, $this->parameters);
  96. }
  97. /**
  98. * Returns a parameter value.
  99. *
  100. * @param string $name The parameter name
  101. *
  102. * @return mixed The parameter value
  103. *
  104. * @throws \InvalidArgumentException When parameter doesn't exists for this event
  105. */
  106. public function get($name)
  107. {
  108. if (!array_key_exists($name, $this->parameters)) {
  109. throw new \InvalidArgumentException(sprintf('The event "%s" has no "%s" parameter.', $this->name, $name));
  110. }
  111. return $this->parameters[$name];
  112. }
  113. /**
  114. * Sets a parameter.
  115. *
  116. * @param string $name The parameter name
  117. * @param mixed $value The parameter value
  118. */
  119. public function set($name, $value)
  120. {
  121. $this->parameters[$name] = $value;
  122. }
  123. }