Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- abstract class RoleChecker {
- /**
- * @var RoleChecker
- */
- protected $nextCheck;
- abstract public function check(UserRole $userRole);
- public function next(UserRole $userRole)
- {
- if ($this->nextCheck) {
- $this->nextCheck->check($userRole);
- }
- }
- public function setNextChecker(RoleChecker $checker)
- {
- $this->nextCheck = $checker;
- }
- }
- class UserRole {
- public $type;
- public $date;
- }
- class AdminCheck extends RoleChecker {
- public function check(UserRole $userRole)
- {
- if (!$userRole->type != 'ADMIN') {
- throw new Exception('User Not Admin');
- }
- $this->next($userRole);
- }
- }
- class RegistrationDate extends RoleChecker {
- public function check(UserRole $userRole)
- {
- if (strtotime($userRole->date) > strtotime('2018-01-01')) {
- throw new Exception('2018 Users only.');
- }
- $this->next($userRole);
- }
- }
- $role = new UserRole();
- $role->type = 'ROLE_USER';
- $role->date = '2017-01-01';
- $adminCheck = new AdminCheck();
- $registrationCheck = new RegistrationDate();
- $adminCheck->setNextChecker($registrationCheck);
- $adminCheck->check($role);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement