Guest User

Untitled

a guest
Jan 18th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. <?php
  2.  
  3. class event implements SplSubject {
  4.  
  5. private $pre_storage, $post_storage;
  6.  
  7. const PRE_EVENT = 1;
  8. const POST_EVENT = 2;
  9.  
  10. public function __construct() {
  11. $this->pre_storage = new SplObjectStorage();
  12. $this->post_storage = new SplObjectStorage();
  13. }
  14.  
  15. public function init()
  16. {
  17. // Notify all the observers of a change
  18. $this->notify( );
  19. }
  20.  
  21. /*
  22. *
  23. * @access public
  24. * @param SplObserver $observer
  25. * @param integer $event_condition
  26. *
  27. */
  28. public function attach( SplObserver $observer, $event_condition=2 )
  29. {
  30. switch ( $event_condition )
  31. {
  32. case 1:
  33. $this->pre_storage->attach( $observer );
  34. break;
  35.  
  36. case 2:
  37. $this->post_storage->attach( $observer );
  38. break;
  39.  
  40. default:
  41. throw new Exception("Storage type not available");
  42. }
  43. }
  44.  
  45.  
  46. public function detach( SplObserver $observer ) {
  47. $this->storage->detach( $observer );
  48. }
  49.  
  50. public function notify()
  51. {
  52. foreach ( $this->pre_storage as $observer )
  53. {
  54. $observer->update( $this );
  55. }
  56. foreach ( $this->post_storage as $observer )
  57. {
  58. $observer->update( $this );
  59. }
  60. }
  61. } // end events class
  62.  
  63.  
  64. class signupEmailer implements splObserver{
  65.  
  66. public function update( SplSubject $SplSubject )
  67. {
  68. $status = $SplSubject->getStatus();
  69. switch ( $status[0] ) {
  70.  
  71. case signup::USERNAME_TAKEN:
  72. echo __CLASS__ . ": Username taken!.\n";
  73. break;
  74.  
  75. case signup::USERNAME_TOO_LONG:
  76. echo __CLASS__ . ": Username too long.\n";
  77. break;
  78.  
  79. case signup::USERNAME_TOO_SHORT:
  80. echo __CLASS__ . ": Username is too short.\n";
  81. break;
  82.  
  83. case signup::ALLOW:
  84. echo __CLASS__ . ": Username is good, Emailing admin success\n";
  85. break;
  86.  
  87. default: throw new Exception( "Invalid status\n" );
  88. }
  89. }
  90. } // end of mailer class
  91.  
  92.  
  93. class logger implements splObserver{
  94.  
  95. public function update( SplSubject $SplSubject )
  96. {
  97. echo __CLASS__." : Logged an event\n";
  98. }
  99. }
  100.  
  101. class checkUser implements splObserver{
  102.  
  103. public function update( SplSubject $SplSubject )
  104. {
  105. echo __CLASS__.": This happens before the event\n";
  106. }
  107. } // end checkUser class
  108.  
  109.  
  110. class signup extends event{
  111.  
  112. const USERNAME_TAKEN = 1;
  113. const USERNAME_TOO_SHORT = 2;
  114. const USERNAME_TOO_LONG = 3;
  115. const ALLOW = 4;
  116.  
  117. public $status = array();
  118. public $username, $password, $ip_address;
  119.  
  120. function init()
  121. {
  122.  
  123. // Let's simulate different signin conditions
  124. $this->setStatus( rand( 1, 4 ) );
  125.  
  126. // Notify all the observers of a change
  127. $this->notify();
  128.  
  129. if ( $this->status[0] == self::ALLOW ) {
  130. return true;
  131. }
  132. return false;
  133. }
  134.  
  135. private function setStatus( $status ) {
  136. $this->status = array( $status, $this->username, $this->password, $this->ip_address );
  137. }
  138.  
  139. function getStatus() {
  140. return $this->status;
  141. }
  142.  
  143. }
  144.  
  145. // the new login class implements splSubject
  146. $signup = new signup();
  147. $signup->username = 'freddy';
  148. $signup->password = 'queen';
  149. $signup->ip_address = '127.0.0.1';
  150.  
  151. // Attach classes to observer/listen for a login event
  152. $signup->attach( new signupEmailer() );
  153. $signup->attach( new logger() );
  154. $signup->attach( new checkUser, 1 );
  155.  
  156. if( $signup->init() !== false )
  157. {
  158. echo "Signup success\n";
  159. }
  160. else
  161. {
  162. echo print_r( $signup->status );
  163. }
Add Comment
Please, Sign In to add comment