Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class event implements SplSubject {
- private $pre_storage, $post_storage;
- const PRE_EVENT = 1;
- const POST_EVENT = 2;
- public function __construct() {
- $this->pre_storage = new SplObjectStorage();
- $this->post_storage = new SplObjectStorage();
- }
- public function init()
- {
- // Notify all the observers of a change
- $this->notify( );
- }
- /*
- *
- * @access public
- * @param SplObserver $observer
- * @param integer $event_condition
- *
- */
- public function attach( SplObserver $observer, $event_condition=2 )
- {
- switch ( $event_condition )
- {
- case 1:
- $this->pre_storage->attach( $observer );
- break;
- case 2:
- $this->post_storage->attach( $observer );
- break;
- default:
- throw new Exception("Storage type not available");
- }
- }
- public function detach( SplObserver $observer ) {
- $this->storage->detach( $observer );
- }
- public function notify()
- {
- foreach ( $this->pre_storage as $observer )
- {
- $observer->update( $this );
- }
- foreach ( $this->post_storage as $observer )
- {
- $observer->update( $this );
- }
- }
- } // end events class
- class signupEmailer implements splObserver{
- public function update( SplSubject $SplSubject )
- {
- $status = $SplSubject->getStatus();
- switch ( $status[0] ) {
- case signup::USERNAME_TAKEN:
- echo __CLASS__ . ": Username taken!.\n";
- break;
- case signup::USERNAME_TOO_LONG:
- echo __CLASS__ . ": Username too long.\n";
- break;
- case signup::USERNAME_TOO_SHORT:
- echo __CLASS__ . ": Username is too short.\n";
- break;
- case signup::ALLOW:
- echo __CLASS__ . ": Username is good, Emailing admin success\n";
- break;
- default: throw new Exception( "Invalid status\n" );
- }
- }
- } // end of mailer class
- class logger implements splObserver{
- public function update( SplSubject $SplSubject )
- {
- echo __CLASS__." : Logged an event\n";
- }
- }
- class checkUser implements splObserver{
- public function update( SplSubject $SplSubject )
- {
- echo __CLASS__.": This happens before the event\n";
- }
- } // end checkUser class
- class signup extends event{
- const USERNAME_TAKEN = 1;
- const USERNAME_TOO_SHORT = 2;
- const USERNAME_TOO_LONG = 3;
- const ALLOW = 4;
- public $status = array();
- public $username, $password, $ip_address;
- function init()
- {
- // Let's simulate different signin conditions
- $this->setStatus( rand( 1, 4 ) );
- // Notify all the observers of a change
- $this->notify();
- if ( $this->status[0] == self::ALLOW ) {
- return true;
- }
- return false;
- }
- private function setStatus( $status ) {
- $this->status = array( $status, $this->username, $this->password, $this->ip_address );
- }
- function getStatus() {
- return $this->status;
- }
- }
- // the new login class implements splSubject
- $signup = new signup();
- $signup->username = 'freddy';
- $signup->password = 'queen';
- $signup->ip_address = '127.0.0.1';
- // Attach classes to observer/listen for a login event
- $signup->attach( new signupEmailer() );
- $signup->attach( new logger() );
- $signup->attach( new checkUser, 1 );
- if( $signup->init() !== false )
- {
- echo "Signup success\n";
- }
- else
- {
- echo print_r( $signup->status );
- }
Add Comment
Please, Sign In to add comment