| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare( strict_types = 1 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace ValueValidators\PackagePrivate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use ValueValidators\Error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use ValueValidators\Result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use ValueValidators\ValueValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | abstract class ValueValidatorBase implements ValueValidator { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 * A list of allowed values. This means the parameters value(s) must be in the list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * during validation. False for no restriction. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * @var array|false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	protected $allowedValues = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 * A list of prohibited values. This means the parameters value(s) must | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 * not be in the list during validation. False for no restriction. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * @var array|false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	protected $prohibitedValues = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	protected $options = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * @var Error[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	protected $errors = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 * @see ValueValidator::validate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * @return Result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	final public function validate( $value ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		$this->errors = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		if ( $this->enableWhitelistRestrictions() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			$this->valueIsAllowed( $value ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$this->doValidation( $value ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		if ( $this->errors === [] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			return Result::newSuccess(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			return Result::newError( $this->errors ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * Checks the value against the allowed values and prohibited values lists in case they are set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 * @param mixed $value | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 	protected function valueIsAllowed( $value ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 		if ( $this->allowedValues !== false && !in_array( $value, $this->allowedValues, true ) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 			$this->addErrorMessage( 'Value not in whitelist' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 		if ( $this->prohibitedValues !== false && in_array( $value, $this->prohibitedValues, true ) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 			$this->addErrorMessage( 'Value in blacklist' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 * @see ValueValidator::validate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	 * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	abstract public function doValidation( $value ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * Sets the parameter definition values contained in the provided array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * @see ParamDefinition::setArrayValues | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 * @param array $param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	public function setOptions( array $param ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		if ( $this->enableWhitelistRestrictions() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			if ( array_key_exists( 'values', $param ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 				$this->allowedValues = $param['values']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			if ( array_key_exists( 'excluding', $param ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 				$this->prohibitedValues = $param['excluding']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		$this->options = $param; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 * Registers an error message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 * @param string $errorMessage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	protected function addErrorMessage( $errorMessage ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		$this->addError( Error::newError( $errorMessage ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 * Registers an error. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 * @param Error $error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	protected function addError( Error $error ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		$this->errors[] = $error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 * Registers a list of errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 * @param Error[] $errors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	protected function addErrors( array $errors ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		$this->errors = array_merge( $this->errors, $errors ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	 * Runs the value through the provided ValueValidator and registers the errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	 * Options of $this can be mapped to those of the passed ValueValidator using | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	 * the $optionMap parameter in which keys are source names and values are target | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	 * names. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	 * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	 * @param ValueValidator $validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 * @param string|null $property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	 * @param array $optionMap | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	protected function runSubValidator( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		$value, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		ValueValidator $validator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		$property = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		array $optionMap = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		if ( $optionMap !== [] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 			$options = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 			foreach ( $optionMap as $source => $target ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 				if ( array_key_exists( $source, $this->options ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 					$options[$target] = $this->options[$source]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 			$validator->setOptions( $options ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		 * @var Error $error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		foreach ( $validator->validate( $value )->getErrors() as $error ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 			$this->addError( Error::newError( $error->getText(), $property ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 	 * If the "values" and "excluding" arguments should be held into account. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	protected function enableWhitelistRestrictions() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 		return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 	 * Returns the allowed values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	 * TODO: think about how to access set options in general and if we want to have | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 	 * whitelist and baclklist values in the validator objects to begin with. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 	 * @return array|bool false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 	public function getWhitelistedValues() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 		return $this->allowedValues; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 192 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 193 |  |  |  |