Advertisement
gzhegow

Untitled

Dec 5th, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.01 KB | None | 0 0
  1. <?php
  2.  
  3. Interface ValidationInterface
  4. {
  5.     public function validate();
  6. }
  7.  
  8. Class Validator
  9. {
  10.     public function createValidation(ValidationInterface $validation)
  11.     {
  12.         return new Validation($validation);
  13.     }
  14. }
  15.  
  16. Class Validation implements ValidationInterface
  17. {
  18.     private $validation;
  19.    
  20.     private $messages;
  21.    
  22.     public function __construct(ValidationInterface $validation)
  23.     {
  24.         $this->validation = $validation;
  25.     }
  26.    
  27.     public function getMessages()
  28.     {
  29.         return $this->messages;
  30.     }
  31.    
  32.     public function validate()
  33.     {
  34.         $messages = $this->validation->validate();
  35.            
  36.         $this->messages = array_merge($this->messages ?? [], $messages);
  37.        
  38.         return true;
  39.     }
  40. }
  41.  
  42. Class MyValidation implements ValidationInterface
  43. {
  44.     public function validate()
  45.     {
  46.         return [ 'Hello World' ];
  47.     }
  48. }
  49.  
  50. $validator = new Validator();
  51. $validation = $validator->createValidation(new MyValidation());
  52. $validation->validate();
  53.  
  54. var_dump($validation->getMessages());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement