Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. <?php
  2.  
  3. abstract class RoleChecker {
  4.  
  5. /**
  6. * @var RoleChecker
  7. */
  8. protected $nextCheck;
  9.  
  10. abstract public function check(UserRole $userRole);
  11.  
  12. public function next(UserRole $userRole)
  13. {
  14. if ($this->nextCheck) {
  15. $this->nextCheck->check($userRole);
  16. }
  17. }
  18.  
  19. public function setNextChecker(RoleChecker $checker)
  20. {
  21. $this->nextCheck = $checker;
  22. }
  23. }
  24.  
  25. class UserRole {
  26.  
  27. public $type;
  28.  
  29. public $date;
  30. }
  31.  
  32. class AdminCheck extends RoleChecker {
  33.  
  34. public function check(UserRole $userRole)
  35. {
  36. if (!$userRole->type != 'ADMIN') {
  37. throw new Exception('User Not Admin');
  38. }
  39.  
  40. $this->next($userRole);
  41. }
  42. }
  43.  
  44. class RegistrationDate extends RoleChecker {
  45.  
  46.  
  47. public function check(UserRole $userRole)
  48. {
  49. if (strtotime($userRole->date) > strtotime('2018-01-01')) {
  50. throw new Exception('2018 Users only.');
  51. }
  52.  
  53. $this->next($userRole);
  54. }
  55.  
  56. }
  57.  
  58. $role = new UserRole();
  59. $role->type = 'ROLE_USER';
  60. $role->date = '2017-01-01';
  61.  
  62. $adminCheck = new AdminCheck();
  63. $registrationCheck = new RegistrationDate();
  64. $adminCheck->setNextChecker($registrationCheck);
  65.  
  66. $adminCheck->check($role);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement