| Server IP : 121.121.20.254 / Your IP : 216.73.216.202 Web Server : Microsoft-IIS/10.0 System : Windows NT WEB-SERVER 10.0 build 20348 (Windows Server 2022) AMD64 User : IUSR ( 0) PHP Version : 8.3.28 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/inetpub/wwwroot/WPCAFE/wp-content/plugins/wp-cafe/base/validation/ |
Upload File : |
<?php
namespace WpCafe\Validation;
/**
* Validator class to validate data against rules.
*/
class Validator {
/**
* @var array
*/
protected $data;
/**
* @var array<string, ValidationRuleInterface[]>
*/
protected $rules;
/**
* @var array
*/
protected $errors = [];
/**
* @param array $data
* @param array $rules
*/
public function __construct(array $data, array $rules) {
$this->data = $data;
$this->rules = $rules;
}
/**
* Run the validator.
*
* @throws Validation_Exception
*/
public function validate(): void {
foreach ($this->rules as $field => $field_rules) {
$value = $this->data[$field] ?? null;
foreach ( $field_rules as $rule ) {
if ( ! $rule->validate( $field, $value, $this->data ) ) {
$this->errors[$field][] = $rule->message( $field );
}
}
}
if ( ! empty( $this->errors ) ) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Errors are stored as array and escaped when displayed
throw new Validation_Exception( $this->errors );
}
}
/**
* Check if the validation passed.
*
* @return bool True if validation passed.
*/
public function passes(): bool {
return empty($this->errors);
}
/**
* Retrieve validation error messages.
*
* @return array Associative array of validation errors.
*/
public function errors(): array {
return $this->errors;
}
}