Advertisement
___CONST___

Expected

Jun 29th, 2020
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.75 KB | None | 0 0
  1. <?php
  2. abstract class AbstractValidator
  3. {
  4.     private $_args;
  5.  
  6.     protected const array 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.     //type could be omitted since it has to follow the parents signature
  25.     protected const ALLOWED_VALUES = [
  26.         'Mark', 'Bill', 'Dan'
  27.     ];
  28. }
  29.  
  30. class InvalidNamesValidator extends AbstractValidator
  31. {
  32.     //this would throw a TypeError
  33.     protected const ALLOWED_VALUES = 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement