Advertisement
___CONST___

Example using constants

Jun 29th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.64 KB | None | 0 0
  1. <?php
  2. abstract class AbstractValidator
  3. {
  4.     private $_args;
  5.  
  6.     protected const ALLOWED_VALUES = [];
  7.  
  8.     public function __construct(array $args)
  9.     {
  10.         $this->_args = $args;
  11.     }
  12.  
  13.     public function isValid(): bool
  14.     {
  15.         foreach ($this->_args as $value)
  16.             if (!in_array(static::ALLOWED_VALUES, $value))
  17.                 return false;
  18.         return true;
  19.     }
  20. }
  21.  
  22. class NamesValidator extends AbstractValidator
  23. {
  24.     protected const ALLOWED_VALUES = [
  25.         'Mark', 'Bill', 'Dan'
  26.     ];
  27. }
  28.  
  29. class InvalidNamesValidator extends AbstractValidator
  30. {
  31.     protected const ALLOWED_VALUES = 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement