| 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/rules/ |
Upload File : |
<?php
namespace WpCafe\Validation\Rules;
/**
* Rule to ensure a value is in a predefined list.
*/
class In implements Validation_Rule_Interface {
/**
* @var array
*/
protected $values;
/**
* Constructor.
*
* @param string $list A comma-separated string of valid values.
*/
public function __construct($list) {
$this->values = array_map('trim', explode(',', $list));
}
/**
* Validate that the value exists in the predefined list.
*
* @param string $field The name of the field being validated.
* @param mixed $value The value to validate.
* @param array $all_data The full dataset being validated.
* @return bool True if valid, false otherwise.
*/
public function validate(string $field, $value, array $all_data): bool {
return in_array($value, $this->values, true);
}
/**
* Get the error message for this rule.
*
* @param string $field The field name.
* @return string The error message.
*/
public function message(string $field): string {
return sprintf(
'The %s field must be one of: %s.',
$field,
implode(', ', $this->values)
);
}
}