Skip to content
Snippets Groups Projects
Select Git revision
  • 2f081692bef70dae62affad84c9a299d50d3a676
  • master default protected
  • fix/body-raw
  • devel
  • 1.0.5
  • 1.0.4
  • 1.0.3
  • 1.0.2
  • 1.0.1
  • 1.0
10 results

bootstrap.php

Blame
  • ChainValidator.php 874 B
    <?php
    
    namespace WPDesk\Forms\Validator;
    
    use WPDesk\Forms\Validator;
    
    class ChainValidator implements Validator {
    
    	/** @var Validator[] */
    	private $validators;
    
    	/** @var array */
    	private $messages;
    
    	public function __construct() {
    		$this->validators = [];
    		$this->messages   = [];
    	}
    
    	/**
    	 * @param Validator $validator
    	 *
    	 * @return $this
    	 */
    	public function attach( Validator $validator ): self {
    		$this->validators[] = $validator;
    
    		return $this;
    	}
    
    	public function is_valid( $value ): bool {
    		$result   = true;
    		$messages = [ [] ];
    		foreach ( $this->validators as $validator ) {
    			if ( ! $validator->is_valid( $value ) ) {
    				$result     = false;
    				$messages[] = $validator->get_messages();
    			}
    		}
    		$this->messages = array_merge( ...$messages );
    
    		return $result;
    	}
    
    	public function get_messages(): array {
    		return $this->messages;
    	}
    
    }