This project demonstrates the implementation of the State Design Pattern in PHP through an event registration system. It showcases how to handle complex state transitions while maintaining clean and maintainable code.
- PHP 8.3 or higher
- Composer (for dependency management)
The State Pattern allows an object to alter its behavior when its internal state changes. In this implementation, we model an event registration system that handles both physical and online events with different workflows.
- Implementation of the State Design Pattern
- Support for both physical and online events
- Administrative approval workflow
- State transition logging
- Comprehensive test coverage
- Applied State
- Fill Form State
- Admin Acceptance/Rejection State
- Paid State
- Done State
- Applied State
- Paid State
- Fill Form State
- Done State
├── Behavioral/
│ └── Event/
│ ├── AdminAcceptedState.php
│ ├── AppliedState.php
│ ├── DoneState.php
│ ├── EventContext.php
│ ├── FillFormState.php
│ ├── PaidState.php
│ ├── RejectedState.php
│ ├── State.php
│ ├── StateEnum.php
│ └── User.php
├── Tests/
│ └── StateEventTest.php
├── composer.json
├── phpunit.xml.dist
└── README.md
- Clone the repository:
git clone https://github.com/Ag-Tawfik/State-Pattern.git- Install dependencies:
composer installExecute the test suite using PHPUnit:
composer test// Create a user for a physical event with admin approval
$user = new User('John Doe', true, true);
$event = new EventContext($user);
// Progress through the states
$event->eventProceed(); // APPLIED
$event->eventProceed(); // FILLFORM
$event->eventProceed(); // ADMINACCEPTED
$event->eventProceed(); // PAID
// Final state: DONE
// Create a user for an online event
$user = new User('Jane Doe', false, false);
$event = new EventContext($user);
// Progress through the states
$event->eventProceed(); // APPLIED
$event->eventProceed(); // PAID
$event->eventProceed(); // FILLFORM
// Final state: DONEThe State Pattern is implemented using the following key components:
State: Abstract base class defining the interface for all concrete statesEventContext: Maintains the current state and delegates state-specific behavior- Concrete States: Individual classes for each state implementing specific behaviors
StateEnum: Enumeration of all possible states
This project is licensed under the MIT License - see the LICENSE file for details.