Subir proyecto

This commit is contained in:
Gabriel
2026-10-08 10:39:09 -06:00
commit 3a971d1d0b
8344 changed files with 2765102 additions and 0 deletions
@@ -0,0 +1,3 @@
# Ignore artifacts:
build
coverage
@@ -0,0 +1,3 @@
{
"plugins": ["@prettier/plugin-php"]
}
@@ -0,0 +1,32 @@
{
"name": "rocklobsterinc/swv",
"description": "Schema-Woven Validation for PHP",
"license": "MIT",
"authors": [
{
"name": "Takayuki Miyoshi",
"role": "Developer"
}
],
"autoload": {
"psr-4": {
"RockLobsterInc\\Swv\\": "src/"
},
"files": [
"src/functions.php"
]
},
"scripts": {
"lint": "parallel-lint src/ tests/",
"test": "phpunit"
},
"require": {
"php": ">=8.3",
"rocklobsterinc/functions": "^v1.0",
"rocklobsterinc/form-data-tree": "^v2.0"
},
"require-dev": {
"phpunit/phpunit": "^12.5",
"php-parallel-lint/php-parallel-lint": "^1.4"
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd"
bootstrap="tests/bootstrap.php"
testdox="true">
<testsuites>
<testsuite name="unit">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
@@ -0,0 +1,42 @@
<?php
namespace RockLobsterInc\Swv;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
/**
* Abstract class that represents the base for all SWV rules.
*/
abstract class AbstractRule
{
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [];
}
}
@@ -0,0 +1,34 @@
<?php
namespace RockLobsterInc\Swv;
/**
* Abstract class that represents the base for all composite-type SWV rules.
*/
abstract class CompositeRule extends AbstractRule
{
/**
* Sub-rules of the rule.
*/
protected array $rules = [];
/**
* Adds a sub-rule to this composite rule.
*
* @param AbstractRule $rule Sub-rule to be added.
*/
public function addRule(AbstractRule $rule): bool
{
$this->rules[] = $rule;
return true;
}
/**
* Returns an iterator of sub-rules.
*/
public function rules(): array
{
return $this->rules;
}
}
@@ -0,0 +1,56 @@
<?php
namespace RockLobsterInc\Swv;
/**
* Exception that represents a validation error.
*/
final class InvalidityException extends \Exception
{
/**
* The SWV rule who has thrown this error.
*/
public readonly AbstractRule $rule;
/**
* The specific cause of this exception.
*/
public readonly mixed $cause;
/**
* Constructor.
*
* @param AbstractRule $rule SWV rule.
*/
public function __construct(AbstractRule $rule, array $options = [])
{
$this->rule = $rule;
$this->cause = $options["cause"] ?? null;
parent::__construct($this->getErrorMessage());
}
/**
* Retrieves the validation error message.
*/
private function getErrorMessage(): string
{
if ($this->cause instanceof self) {
return $this->cause->getMessage();
}
return $this->rule->error ?? "";
}
/**
* Retrieves the field name where the validation error occurs.
*/
public function getField(): string
{
if ($this->cause instanceof self) {
return $this->cause->rule->field ?? "";
}
return $this->rule->field ?? "";
}
}
@@ -0,0 +1,84 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{CompositeRule, InvalidityException as Invalidity};
final class AllRule extends CompositeRule
{
const RULE_NAME = "all";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
foreach ($this->rules() as $rule) {
if ($rule->matches($context)) {
try {
$rule->validate($form_data, $context);
} catch (Invalidity $error) {
throw new Invalidity($this, ["cause" => $error]);
}
}
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
$rules = [];
foreach ($this->rules() as $rule) {
$rules[] = $rule->toArray();
}
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"rules" => $rules,
];
}
}
@@ -0,0 +1,95 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{CompositeRule, InvalidityException as Invalidity};
final class AnyRule extends CompositeRule
{
const RULE_NAME = "any";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$is_valid = null;
foreach ($this->rules() as $rule) {
if ($rule->matches($context)) {
try {
$is_valid = true;
$rule->validate($form_data, $context);
} catch (Invalidity $error) {
$is_valid = false;
}
if ($is_valid) {
break;
}
}
}
if (false === $is_valid) {
throw new Invalidity($this);
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
$rules = [];
foreach ($this->rules() as $rule) {
$rules[] = $rule->toArray();
}
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"rules" => $rules,
];
}
}
@@ -0,0 +1,106 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class DateRule extends AbstractRule
{
const RULE_NAME = "date";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
}
/**
* Returns true if the given string is a valid Gregorian date in
* the YYYY-MM-DD format.
*
* @param string $value String to check.
*/
public static function isDate(string $value): bool
{
$result = preg_match(
'/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/',
$value,
$matches,
);
if (!$result) {
return false;
}
return checkdate($matches[2], $matches[3], $matches[1]);
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values)) {
return true;
}
foreach ($values as $value) {
if (!self::isDate($value)) {
throw new Invalidity($this, ["cause" => $value]);
}
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
];
}
}
@@ -0,0 +1,95 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class DayofweekRule extends AbstractRule
{
const RULE_NAME = "dayofweek";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
public readonly array $accept;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
$this->accept = $properties["accept"] ?? [];
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values)) {
return true;
}
$acceptable_values = array_map("intval", $this->accept);
foreach ($values as $value) {
if (DateRule::isDate($value)) {
$datetime = date_create_immutable($value);
$day_of_week = (int) $datetime->format("N");
if (!in_array($day_of_week, $acceptable_values, true)) {
throw new Invalidity($this, ["cause" => $value]);
}
}
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"accept" => $this->accept,
];
}
}
@@ -0,0 +1,136 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class EmailRule extends AbstractRule
{
const RULE_NAME = "email";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
}
/**
* Returns true if the given string is a valid email address. This method
* follows WordPress's is_email() implementation.
*
* @link https://developer.wordpress.org/reference/functions/is_email/
*
* @param string $value String to check.
*/
public static function isEmail(string $value): bool
{
if (strlen($value) < 6) {
return false;
}
if (false === strpos($value, "@", 1)) {
return false;
}
[$local, $domain] = explode("@", $value, 2);
if (!preg_match('/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local)) {
return false;
}
if (preg_match("/\.{2,}/", $domain)) {
return false;
}
if (trim($domain, " \t\n\r\0\x0B.") !== $domain) {
return false;
}
$subs = explode(".", $domain);
if (count($subs) < 2) {
return false;
}
foreach ($subs as $sub) {
if (trim($sub, " \t\n\r\0\x0B-") !== $sub) {
return false;
}
if (!preg_match('/^[a-z0-9-]+$/i', $sub)) {
return false;
}
}
return true;
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values)) {
return true;
}
foreach ($values as $value) {
if (!self::isEmail($value)) {
throw new Invalidity($this, ["cause" => $value]);
}
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
];
}
}
@@ -0,0 +1,92 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten, canonicalize_newline};
final class EnumRule extends AbstractRule
{
const RULE_NAME = "enum";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
public readonly array $accept;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
$this->accept = $properties["accept"] ?? [];
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values)) {
return true;
}
$acceptable_values = canonicalize_newline($this->accept);
foreach ($values as $value) {
$value = canonicalize_newline($value);
if (!in_array($value, $acceptable_values, true)) {
throw new Invalidity($this, ["cause" => $value]);
}
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"accept" => $this->accept,
];
}
}
@@ -0,0 +1,269 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class FileRule extends AbstractRule
{
const RULE_NAME = "file";
const MIME_TYPES = [
"application/java" => ["class"],
"application/javascript" => ["js"],
"application/msword" => ["doc"],
"application/octet-stream" => ["psd", "xcf"],
"application/onenote" => ["onetoc", "onetoc2", "onetmp", "onepkg"],
"application/oxps" => ["oxps"],
"application/pdf" => ["pdf"],
"application/rar" => ["rar"],
"application/rtf" => ["rtf"],
"application/ttaf+xml" => ["dfxp"],
"application/vnd.apple.keynote" => ["key"],
"application/vnd.apple.numbers" => ["numbers"],
"application/vnd.apple.pages" => ["pages"],
"application/vnd.ms-access" => ["mdb"],
"application/vnd.ms-excel" => ["xla", "xls", "xlt", "xlw"],
"application/vnd.ms-excel.addin.macroEnabled.12" => ["xlam"],
"application/vnd.ms-excel.sheet.binary.macroEnabled.12" => ["xlsb"],
"application/vnd.ms-excel.sheet.macroEnabled.12" => ["xlsm"],
"application/vnd.ms-excel.template.macroEnabled.12" => ["xltm"],
"application/vnd.ms-powerpoint" => ["pot", "pps", "ppt"],
"application/vnd.ms-powerpoint.addin.macroEnabled.12" => ["ppam"],
"application/vnd.ms-powerpoint.presentation.macroEnabled.12" => [
"pptm",
],
"application/vnd.ms-powerpoint.slide.macroEnabled.12" => ["sldm"],
"application/vnd.ms-powerpoint.slideshow.macroEnabled.12" => ["ppsm"],
"application/vnd.ms-powerpoint.template.macroEnabled.12" => ["potm"],
"application/vnd.ms-project" => ["mpp"],
"application/vnd.ms-word.document.macroEnabled.12" => ["docm"],
"application/vnd.ms-word.template.macroEnabled.12" => ["dotm"],
"application/vnd.ms-write" => ["wri"],
"application/vnd.ms-xpsdocument" => ["xps"],
"application/vnd.oasis.opendocument.chart" => ["odc"],
"application/vnd.oasis.opendocument.database" => ["odb"],
"application/vnd.oasis.opendocument.formula" => ["odf"],
"application/vnd.oasis.opendocument.graphics" => ["odg"],
"application/vnd.oasis.opendocument.presentation" => ["odp"],
"application/vnd.oasis.opendocument.spreadsheet" => ["ods"],
"application/vnd.oasis.opendocument.text" => ["odt"],
"application/vnd.openxmlformats-officedocument.presentationml.presentation" => [
"pptx",
],
"application/vnd.openxmlformats-officedocument.presentationml.slide" => [
"sldx",
],
"application/vnd.openxmlformats-officedocument.presentationml.slideshow" => [
"ppsx",
],
"application/vnd.openxmlformats-officedocument.presentationml.template" => [
"potx",
],
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => [
"xlsx",
],
"application/vnd.openxmlformats-officedocument.spreadsheetml.template" => [
"xltx",
],
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" => [
"docx",
],
"application/vnd.openxmlformats-officedocument.wordprocessingml.template" => [
"dotx",
],
"application/wordperfect" => ["wp", "wpd"],
"application/x-7z-compressed" => ["7z"],
"application/x-gzip" => ["gz", "gzip"],
"application/x-msdownload" => ["exe"],
"application/x-shockwave-flash" => ["swf"],
"application/x-tar" => ["tar"],
"application/zip" => ["zip"],
"audio/3gpp" => ["3gp", "3gpp"],
"audio/3gpp2" => ["3g2", "3gp2"],
"audio/aac" => ["aac"],
"audio/flac" => ["flac"],
"audio/midi" => ["mid", "midi"],
"audio/mpeg" => ["mp3", "m4a", "m4b"],
"audio/ogg" => ["ogg", "oga"],
"audio/wav" => ["wav", "x-wav"],
"audio/x-matroska" => ["mka"],
"audio/x-ms-wax" => ["wax"],
"audio/x-ms-wma" => ["wma"],
"audio/x-realaudio" => ["ra", "ram"],
"image/avif" => ["avif"],
"image/bmp" => ["bmp"],
"image/gif" => ["gif"],
"image/heic" => ["heic"],
"image/heic-sequence" => ["heic", "heics"],
"image/heif" => ["heic", "heif"],
"image/heif-sequence" => ["heic", "heifs"],
"image/jpeg" => ["jpg", "jpeg", "jpe"],
"image/png" => ["png"],
"image/tiff" => ["tiff", "tif"],
"image/webp" => ["webp"],
"image/x-icon" => ["ico"],
"text/calendar" => ["ics"],
"text/css" => ["css"],
"text/csv" => ["csv"],
"text/html" => ["htm", "html"],
"text/plain" => ["txt", "asc", "c", "cc", "h", "srt"],
"text/richtext" => ["rtx"],
"text/tab-separated-values" => ["tsv"],
"text/vtt" => ["vtt"],
"video/3gpp" => ["3gp", "3gpp"],
"video/3gpp2" => ["3g2", "3gp2"],
"video/avi" => ["avi"],
"video/divx" => ["divx"],
"video/mp4" => ["mp4", "m4v"],
"video/mpeg" => ["mpeg", "mpg", "mpe"],
"video/ogg" => ["ogv"],
"video/quicktime" => ["mov", "qt"],
"video/webm" => ["webm"],
"video/x-flv" => ["flv"],
"video/x-matroska" => ["mkv"],
"video/x-ms-asf" => ["asf", "asx"],
"video/x-ms-wm" => ["wm"],
"video/x-ms-wmv" => ["wmv"],
"video/x-ms-wmx" => ["wmx"],
];
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
public readonly array $accept;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
$this->accept = $properties["accept"] ?? [];
}
/**
* Converts a MIME type string to an array of corresponding file extensions.
*
* @param string $mime MIME type. Wildcard (*) is available for the subtype.
* @return array Corresponding file extensions.
*/
public static function convertMimeToExt(string $mime): array
{
$results = [];
if (preg_match('%^([a-z]+)/([*]|[a-z0-9.+-]+)$%i', $mime, $matches)) {
$maintype = $matches[1];
$subtype = $matches[2];
if ("*" !== $subtype) {
$results = self::MIME_TYPES[$mime] ?? [];
} else {
$mime_types = array_filter(
self::MIME_TYPES,
static function ($mime_type) use ($maintype) {
return 0 === strpos($mime_type, $maintype . "/");
},
ARRAY_FILTER_USE_KEY,
);
$results = array_merge(...array_values($mime_types));
}
}
return array_values(array_unique($results));
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["file"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$files = $form_data->getAllFiles($this->field);
$files = array_flatten($files);
if (empty($files)) {
return true;
}
$acceptable_filetypes = [];
foreach ($this->accept as $accept) {
if (preg_match('/^\.[a-z0-9]+$/i', $accept)) {
$acceptable_filetypes[] = $accept;
} else {
foreach (self::convertMimeToExt($accept) as $extension) {
$acceptable_filetypes[] = sprintf(
".%s",
trim($extension, " ."),
);
}
}
}
$acceptable_filetypes = array_map("strtolower", $acceptable_filetypes);
$acceptable_filetypes = array_unique($acceptable_filetypes);
foreach ($files as $file) {
$file_name = $file->name();
$last_period_pos = strrpos($file_name, ".");
if (false === $last_period_pos) {
// No period.
throw new Invalidity($this, ["cause" => $file]);
}
$suffix = strtolower(substr($file_name, $last_period_pos));
if (!in_array($suffix, $acceptable_filetypes, true)) {
throw new Invalidity($this, ["cause" => $file]);
}
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"accept" => $this->accept,
];
}
}
@@ -0,0 +1,88 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class MaxDateRule extends AbstractRule
{
const RULE_NAME = "maxdate";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
public readonly string $threshold;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
$this->threshold = $properties["threshold"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values) or !DateRule::isDate($this->threshold)) {
return true;
}
foreach ($values as $value) {
if (DateRule::isDate($value) and $this->threshold < $value) {
throw new Invalidity($this, ["cause" => $value]);
}
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"threshold" => $this->threshold,
];
}
}
@@ -0,0 +1,95 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class MaxFilesizeRule extends AbstractRule
{
const RULE_NAME = "maxfilesize";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
public readonly string $threshold;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
$this->threshold = $properties["threshold"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["file"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$files = $form_data->getAllFiles($this->field);
$files = array_flatten($files);
if (empty($files)) {
return true;
}
$file_size = array_reduce(
$files,
static function ($carry, $item) {
$carry += $item->size();
return $carry;
},
0,
);
if ((int) $this->threshold < $file_size) {
throw new Invalidity($this, ["cause" => $file_size]);
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"threshold" => $this->threshold,
];
}
}
@@ -0,0 +1,86 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class MaxItemsRule extends AbstractRule
{
const RULE_NAME = "maxitems";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
public readonly string $threshold;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
$this->threshold = $properties["threshold"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values) or !is_numeric($this->threshold)) {
return true;
}
if ((int) $this->threshold < count($values)) {
throw new Invalidity($this, ["cause" => count($values)]);
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"threshold" => $this->threshold,
];
}
}
@@ -0,0 +1,100 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
use function RockLobsterInc\Swv\{count_code_units};
final class MaxLengthRule extends AbstractRule
{
const RULE_NAME = "maxlength";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
public readonly string $threshold;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
$this->threshold = $properties["threshold"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values) or !is_numeric($this->threshold)) {
return true;
}
$total = 0;
foreach ($values as $value) {
$length = count_code_units($value);
if (false === $length) {
// mbstring is not loaded.
return true;
}
$total += $length;
}
if ((int) $this->threshold < $total) {
throw new Invalidity($this, ["cause" => $total]);
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"threshold" => $this->threshold,
];
}
}
@@ -0,0 +1,91 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class MaxNumberRule extends AbstractRule
{
const RULE_NAME = "maxnumber";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
public readonly string $threshold;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
$this->threshold = $properties["threshold"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values) or !is_numeric($this->threshold)) {
return true;
}
foreach ($values as $value) {
if (
NumberRule::isNumber($value) and
(float) $this->threshold < (float) $value
) {
throw new Invalidity($this, ["cause" => $value]);
}
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"threshold" => $this->threshold,
];
}
}
@@ -0,0 +1,88 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class MinDateRule extends AbstractRule
{
const RULE_NAME = "mindate";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
public readonly string $threshold;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
$this->threshold = $properties["threshold"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values) or !DateRule::isDate($this->threshold)) {
return true;
}
foreach ($values as $value) {
if (DateRule::isDate($value) and $value < $this->threshold) {
throw new Invalidity($this, ["cause" => $value]);
}
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"threshold" => $this->threshold,
];
}
}
@@ -0,0 +1,95 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class MinFilesizeRule extends AbstractRule
{
const RULE_NAME = "minfilesize";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
public readonly string $threshold;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
$this->threshold = $properties["threshold"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["file"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$files = $form_data->getAllFiles($this->field);
$files = array_flatten($files);
if (empty($files)) {
return true;
}
$file_size = array_reduce(
$files,
static function ($carry, $item) {
$carry += $item->size();
return $carry;
},
0,
);
if ($file_size < (int) $this->threshold) {
throw new Invalidity($this, ["cause" => $file_size]);
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"threshold" => $this->threshold,
];
}
}
@@ -0,0 +1,86 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class MinItemsRule extends AbstractRule
{
const RULE_NAME = "minitems";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
public readonly string $threshold;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
$this->threshold = $properties["threshold"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values) or !is_numeric($this->threshold)) {
return true;
}
if (count($values) < (int) $this->threshold) {
throw new Invalidity($this, ["cause" => count($values)]);
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"threshold" => $this->threshold,
];
}
}
@@ -0,0 +1,100 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
use function RockLobsterInc\Swv\{count_code_units};
final class MinLengthRule extends AbstractRule
{
const RULE_NAME = "minlength";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
public readonly string $threshold;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
$this->threshold = $properties["threshold"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values) or !is_numeric($this->threshold)) {
return true;
}
$total = 0;
foreach ($values as $value) {
$length = count_code_units($value);
if (false === $length) {
// mbstring is not loaded.
return true;
}
$total += $length;
}
if ($total < (int) $this->threshold) {
throw new Invalidity($this, ["cause" => $total]);
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"threshold" => $this->threshold,
];
}
}
@@ -0,0 +1,91 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class MinNumberRule extends AbstractRule
{
const RULE_NAME = "minnumber";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
public readonly string $threshold;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
$this->threshold = $properties["threshold"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values) or !is_numeric($this->threshold)) {
return true;
}
foreach ($values as $value) {
if (
NumberRule::isNumber($value) and
(float) $value < (float) $this->threshold
) {
throw new Invalidity($this, ["cause" => $value]);
}
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"threshold" => $this->threshold,
];
}
}
@@ -0,0 +1,108 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class NumberRule extends AbstractRule
{
const RULE_NAME = "number";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
}
/**
* Returns true if the given string is a well-formed number.
*
* @link https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number)
*
* @param string $value String to check.
*/
public static function isNumber(string $value): bool
{
$patterns = [
'/^[-]?[0-9]+(?:[eE][+-]?[0-9]+)?$/',
'/^[-]?(?:[0-9]+)?[.][0-9]+(?:[eE][+-]?[0-9]+)?$/',
];
foreach ($patterns as $pattern) {
if (preg_match($pattern, $value)) {
return true;
}
}
return false;
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values)) {
return true;
}
foreach ($values as $value) {
if (!self::isNumber($value)) {
throw new Invalidity($this, ["cause" => $value]);
}
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
];
}
}
@@ -0,0 +1,79 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class RequiredFileRule extends AbstractRule
{
const RULE_NAME = "requiredfile";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["file"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$files = $form_data->getAllFiles($this->field);
$files = array_flatten($files);
if (empty($files)) {
throw new Invalidity($this);
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
];
}
}
@@ -0,0 +1,79 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class RequiredRule extends AbstractRule
{
const RULE_NAME = "required";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values)) {
throw new Invalidity($this);
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
];
}
}
@@ -0,0 +1,109 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class StepNumberRule extends AbstractRule
{
const RULE_NAME = "stepnumber";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
public readonly string $base;
public readonly string $interval;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
$this->base = $properties["base"] ?? "";
$this->interval = $properties["interval"] ?? "";
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values)) {
return true;
}
$base = floatval($this->base);
$interval = floatval($this->interval);
if (!(0 < $interval)) {
return true;
}
foreach ($values as $value) {
if (!NumberRule::isNumber($value)) {
continue;
}
$remainder = fmod(floatval($value) - $base, $interval);
if (
0.0 === round(abs($remainder), 6) or
0.0 === round(abs($remainder - $interval), 6)
) {
continue;
}
throw new Invalidity($this, ["cause" => $value]);
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
"base" => $this->base,
"interval" => $this->interval,
];
}
}
@@ -0,0 +1,113 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class TelRule extends AbstractRule
{
const RULE_NAME = "tel";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
}
/**
* Returns true if the given string is a well-formed telephone number.
*
* @param string $value String to check.
*/
public static function isTel(string $value): bool
{
$value = preg_replace('/[#*].*$/', "", $value); // Remove extension.
$value = preg_replace("%[()/.*#\s-]+%", "", $value);
$is_international =
"+" === substr($value, 0, 1) || "00" === substr($value, 0, 2);
if ($is_international) {
$value = "+" . preg_replace("/^[+0]+/", "", $value);
}
if (!preg_match('/^[+]?[0-9]+$/', $value)) {
return false;
}
if (!(5 < strlen($value) and strlen($value) < 16)) {
return false;
}
return true;
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values)) {
return true;
}
foreach ($values as $value) {
if (!self::isTel($value)) {
throw new Invalidity($this, ["cause" => $value]);
}
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
];
}
}
@@ -0,0 +1,112 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class TimeRule extends AbstractRule
{
const RULE_NAME = "time";
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
}
/**
* Returns true if the given string is a valid time.
*
* @link https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time)
*
* @param string $value String to check.
*/
public static function isTime(string $value): bool
{
$time_pattern = '/^([0-9]{2})\:([0-9]{2})(?:\:([0-9]{2}))?$/';
if (!preg_match($time_pattern, $value, $matches)) {
return false;
}
$hour = (int) $matches[1];
$minute = (int) $matches[2];
$second = empty($matches[3]) ? 0 : (int) $matches[3];
return 0 <= $hour &&
$hour <= 23 &&
0 <= $minute &&
$minute <= 59 &&
0 <= $second &&
$second <= 59;
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values)) {
return true;
}
foreach ($values as $value) {
if (!self::isTime($value)) {
throw new Invalidity($this, ["cause" => $value]);
}
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
];
}
}
@@ -0,0 +1,99 @@
<?php
namespace RockLobsterInc\Swv\Rules;
use RockLobsterInc\FormDataTree\{FormDataTreeInterface as FormDataTree};
use RockLobsterInc\Swv\{AbstractRule, InvalidityException as Invalidity};
use function RockLobsterInc\Functions\{array_flatten};
final class UrlRule extends AbstractRule
{
const RULE_NAME = "url";
const ALLOWED_PROTOCOLS = ["http", "https"];
/**
* Rule properties.
*/
public readonly string $field;
public readonly string $error;
/**
* Constructor.
*
* @param array $properties Rule properties.
*/
public function __construct(array $properties = [])
{
$this->field = $properties["field"] ?? "";
$this->error = $properties["error"] ?? "";
}
/**
* Returns true if the given string is a well-formed URL.
*
* @param string $value String to check.
*/
public static function isUrl(string $value): bool
{
$scheme = parse_url($value, PHP_URL_SCHEME);
return $scheme && in_array($scheme, self::ALLOWED_PROTOCOLS, true);
}
/**
* Returns true if this rule matches the given context.
*
* @param array $context Context.
*/
public function matches(array $context): bool
{
if (false === parent::matches($context)) {
return false;
}
if (empty($context["text"])) {
return false;
}
return true;
}
/**
* Validates the form data according to the logic defined by this rule.
*
* @param FormDataTree $form_data Form data.
* @param array $context Optional context.
*/
public function validate(FormDataTree $form_data, array $context = [])
{
$values = $form_data->getAll($this->field);
$values = array_flatten($values);
if (empty($values)) {
return true;
}
foreach ($values as $value) {
if (!self::isUrl($value)) {
throw new Invalidity($this, ["cause" => $value]);
}
}
return true;
}
/**
* Returns an array that represents the rule properties.
*
* @return array Array of rule properties.
*/
public function toArray(): array
{
return [
"rule" => self::RULE_NAME,
"field" => $this->field,
"error" => $this->error,
];
}
}
@@ -0,0 +1,30 @@
<?php
namespace RockLobsterInc\Swv;
use function RockLobsterInc\Functions\{canonicalize_newline};
/**
* Returns the number of code units in a string.
*
* @link http://www.w3.org/TR/html5/infrastructure.html#code-unit-length
*
* @param string $input Input string in the UTF-8 encoding.
* @return int|false The number of code units, or false if mbstring is
* not available.
*/
function count_code_units(string $input): int|bool
{
if (!extension_loaded("mbstring")) {
return false;
}
if ("" === $input) {
return 0;
}
$input = canonicalize_newline($input);
$input = mb_convert_encoding($input, "UTF-16", "UTF-8");
return intdiv(mb_strlen($input, "8bit"), 2);
}
@@ -0,0 +1,91 @@
<?php
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
use RockLobsterInc\Swv\Rules\{AllRule, RequiredRule};
final class AllRuleTest extends TestCase
{
public function testValidity(): void
{
$rule = new AllRule();
$rule->addRule(
new RequiredRule([
"field" => "field-1",
"error" => "Error in field-1.",
]),
);
$rule->addRule(
new RequiredRule([
"field" => "field-2",
"error" => "Error in field-2.",
]),
);
$rule->addRule(
new RequiredRule([
"field" => "field-3",
"error" => "Error in field-3.",
]),
);
$form_data = new FormDataTree([
"post" => [
"field-1" => "1",
"field-2" => "1",
"field-3" => "1",
],
]);
$result = false;
try {
$result = $rule->validate($form_data, ["text" => true]);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
public function testInvalidity(): void
{
$rule = new AllRule();
$rule->addRule(
new RequiredRule([
"field" => "field-1",
"error" => "Error in field-1.",
]),
);
$rule->addRule(
new RequiredRule([
"field" => "field-2",
"error" => "Error in field-2.",
]),
);
$rule->addRule(
new RequiredRule([
"field" => "field-3",
"error" => "Error in field-3.",
]),
);
$form_data = new FormDataTree([
"post" => [
"field-1" => "1",
"field-2" => "",
"field-3" => "1",
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Error in field-2.");
$rule->validate($form_data, ["text" => true]);
}
}
@@ -0,0 +1,94 @@
<?php
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
use RockLobsterInc\Swv\Rules\{AnyRule, RequiredRule};
final class AnyRuleTest extends TestCase
{
public function testValidity(): void
{
$rule = new AnyRule();
$rule->addRule(
new RequiredRule([
"field" => "field-1",
"error" => "Error in field-1.",
]),
);
$rule->addRule(
new RequiredRule([
"field" => "field-2",
"error" => "Error in field-2.",
]),
);
$rule->addRule(
new RequiredRule([
"field" => "field-3",
"error" => "Error in field-3.",
]),
);
$form_data = new FormDataTree([
"post" => [
"field-1" => "",
"field-2" => "1",
"field-3" => "",
],
]);
$result = false;
try {
$result = $rule->validate($form_data, ["text" => true]);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
public function testInvalidity(): void
{
$rule = new AnyRule([
"field" => "field-3",
"error" => "Just another error message.",
]);
$rule->addRule(
new RequiredRule([
"field" => "field-1",
"error" => "Error in field-1.",
]),
);
$rule->addRule(
new RequiredRule([
"field" => "field-2",
"error" => "Error in field-2.",
]),
);
$rule->addRule(
new RequiredRule([
"field" => "field-3",
"error" => "Error in field-3.",
]),
);
$form_data = new FormDataTree([
"post" => [
"field-1" => "",
"field-2" => "",
"field-3" => "",
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data, ["text" => true]);
}
}
@@ -0,0 +1,70 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class DateRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\DateRule";
public static function validValueProvider(): array
{
return [
"blank" => [""],
"valid" => ["2026-07-26"],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["invalid date"],
"non-bissextile" => ["2026-02-29"],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,72 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class DayofweekRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\DayofweekRule";
public static function validValueProvider(): array
{
return [
"blank" => ["", [1, 3, 5]],
"non-date" => ["Monday", [1, 3, 5]],
"valid" => ["2026-07-27", [1, 3, 5]],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["2026-07-26", [1, 3, 5]],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value, $accept): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"accept" => $accept,
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value, $accept): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"accept" => $accept,
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,69 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class EmailRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\EmailRule";
public static function validValueProvider(): array
{
return [
"blank" => [""],
"valid" => ["testing@example.com"],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["invalid email"],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,71 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class EnumRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\EnumRule";
public static function validValueProvider(): array
{
return [
"blank" => ["", ["a", "b", "c"]],
"valid" => ["c", ["a", "b", "c"]],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["z", ["a", "b", "c"]],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value, $accept): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"accept" => $accept,
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value, $accept): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"accept" => $accept,
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,87 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class FileRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\FileRule";
public static function validValueProvider(): array
{
$file = new FileMock([
"name" => "example.png",
"size" => 1000,
"temporaryFilePath" => "/temporary/file/path/example.png",
"error" => UPLOAD_ERR_OK,
]);
return [
"blank" => [null, ["image/png"]],
"valid" => [$file, ["image/png"]],
"wildcard" => [$file, ["image/*"]],
"extension" => [$file, [".png"]],
];
}
public static function invalidValueProvider(): array
{
$file = new FileMock([
"name" => "example.png",
"size" => 1000,
"temporaryFilePath" => "/temporary/file/path/example.png",
"error" => UPLOAD_ERR_OK,
]);
return [
"invalid" => [$file, ["image/jpeg"]],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value, $accept): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"accept" => $accept,
]);
$form_data = new FormDataTree([
"files" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value, $accept): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"accept" => $accept,
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"files" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,72 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class MaxDateRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\MaxDateRule";
public static function validValueProvider(): array
{
return [
"blank" => ["", "2026-07-28"],
"non-date" => ["xxx", "2026-07-28"],
"valid" => ["2026-07-28", "2026-07-28"],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["2026-07-29", "2026-07-28"],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,85 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class MaxFilesizeRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\MaxFilesizeRule";
public static function validValueProvider(): array
{
$file = new FileMock([
"name" => "example.png",
"size" => 1000,
"temporaryFilePath" => "/temporary/file/path/example.png",
"error" => UPLOAD_ERR_OK,
]);
return [
"blank" => [null, 1000],
"valid" => [$file, 1000],
];
}
public static function invalidValueProvider(): array
{
$file = new FileMock([
"name" => "example.png",
"size" => 1000,
"temporaryFilePath" => "/temporary/file/path/example.png",
"error" => UPLOAD_ERR_OK,
]);
return [
"invalid" => [$file, 999],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
]);
$form_data = new FormDataTree([
"files" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"files" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,71 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class MaxItemsRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\MaxItemsRule";
public static function validValueProvider(): array
{
return [
"blank" => [[], 3],
"valid" => [["a", "b", "c"], 3],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => [["a", "b", "c", "d"], 3],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,71 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class MaxLengthRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\MaxLengthRule";
public static function validValueProvider(): array
{
return [
"blank" => ["", 6],
"valid" => ["abc一二三", 6],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["abc一二🥺", 6],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,72 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class MaxNumberRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\MaxNumberRule";
public static function validValueProvider(): array
{
return [
"blank" => ["", 12],
"non-number" => ["xxx", 12],
"valid" => ["12.0", 12],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["12.01", 12],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,72 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class MinDateRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\MinDateRule";
public static function validValueProvider(): array
{
return [
"blank" => ["", "2026-07-28"],
"non-date" => ["xxx", "2026-07-28"],
"valid" => ["2026-07-28", "2026-07-28"],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["2026-07-27", "2026-07-28"],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,85 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class MinFilesizeRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\MinFilesizeRule";
public static function validValueProvider(): array
{
$file = new FileMock([
"name" => "example.png",
"size" => 1000,
"temporaryFilePath" => "/temporary/file/path/example.png",
"error" => UPLOAD_ERR_OK,
]);
return [
"blank" => [null, 1000],
"valid" => [$file, 1000],
];
}
public static function invalidValueProvider(): array
{
$file = new FileMock([
"name" => "example.png",
"size" => 1000,
"temporaryFilePath" => "/temporary/file/path/example.png",
"error" => UPLOAD_ERR_OK,
]);
return [
"invalid" => [$file, 1001],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
]);
$form_data = new FormDataTree([
"files" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"files" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,71 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class MinItemsRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\MinItemsRule";
public static function validValueProvider(): array
{
return [
"blank" => [[], 3],
"valid" => [["a", "b", "c"], 3],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => [["a", "b"], 3],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,71 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class MinLengthRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\MinLengthRule";
public static function validValueProvider(): array
{
return [
"blank" => ["", 6],
"valid" => ["abc一二三", 6],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["abc一二", 6],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,72 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class MinNumberRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\MinNumberRule";
public static function validValueProvider(): array
{
return [
"blank" => ["", 12],
"non-number" => ["xxx", 12],
"valid" => ["12.0", 12],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["11.99", 12],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value, $threshold): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"threshold" => $threshold,
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,70 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class NumberRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\NumberRule";
public static function validValueProvider(): array
{
return [
"blank" => [""],
"integer" => ["12345"],
"decimal" => ["0.12345"],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["invalid number"],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,75 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class RequiredFileRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\RequiredFileRule";
public static function validValueProvider(): array
{
$file = new FileMock([
"name" => "example.png",
"size" => 1000,
"temporaryFilePath" => "/temporary/file/path/example.png",
"error" => UPLOAD_ERR_OK,
]);
return [
"has-value" => [$file],
];
}
public static function invalidValueProvider(): array
{
return [
"blank" => [null],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
]);
$form_data = new FormDataTree([
"files" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"files" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,68 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class RequiredRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\RequiredRule";
public static function validValueProvider(): array
{
return [
"has-value" => ["some text"],
];
}
public static function invalidValueProvider(): array
{
return [
"blank" => [""],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,74 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class StepNumberRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\StepNumberRule";
public static function validValueProvider(): array
{
return [
"blank" => ["", 1.3, 3],
"non-number" => ["xxx", 1.3, 3],
"valid" => ["10.3", 1.3, 3],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["11.3", 1.3, 3],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value, $base, $interval): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"base" => $base,
"interval" => $interval,
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value, $base, $interval): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"base" => $base,
"interval" => $interval,
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,73 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class TelRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\TelRule";
public static function validValueProvider(): array
{
return [
"blank" => [""],
"valid" => ["1234567"],
"country-coded" => ["+81 1234567"],
"with-extension" => ["1234567#abcdef"],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["invalid tel"],
"too-long" => ["1234567890123456"],
"too-short" => ["12345"],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,70 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class TimeRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\TimeRule";
public static function validValueProvider(): array
{
return [
"blank" => [""],
"valid" => ["16:08:12"],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["invalid time"],
"non-time" => ["24:60:60"],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,70 @@
<?php
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RockLobsterInc\FormDataTree\FormDataTree;
use RockLobsterInc\Swv\InvalidityException;
final class UrlRuleTest extends TestCase
{
public static string $ruleClass = "\RockLobsterInc\Swv\Rules\UrlRule";
public static function validValueProvider(): array
{
return [
"blank" => [""],
"valid" => ["http://example.com/example"],
];
}
public static function invalidValueProvider(): array
{
return [
"invalid" => ["invalid url"],
"telnet" => ["telnet://203.0.113.0"],
];
}
#[DataProvider("validValueProvider")]
public function testValidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$result = false;
try {
$result = $rule->validate($form_data);
} catch (InvalidityException $error) {
}
$this->assertTrue($result);
}
#[DataProvider("invalidValueProvider")]
public function testInvalidity($field_value): void
{
$rule = new self::$ruleClass([
"field" => "the-field-name",
"error" => "Just another error message.",
]);
$form_data = new FormDataTree([
"post" => [
"the-field-name" => $field_value,
],
]);
$this->expectException(InvalidityException::class);
$this->expectExceptionMessage("Just another error message.");
$rule->validate($form_data);
}
}
@@ -0,0 +1,41 @@
<?php
require __DIR__ . "/../vendor/autoload.php";
use RockLobsterInc\FormDataTree\FileInterface;
class FileMock implements FileInterface
{
private string $name;
private int $size;
private string $temporaryFilePath;
private int $error;
public function __construct(array $properties = [])
{
$this->name = $properties["name"];
$this->size = $properties["size"];
$this->temporaryFilePath = $properties["temporaryFilePath"];
$this->error = $properties["error"];
}
public function name(): string
{
return $this->name;
}
public function size(): int
{
return $this->size;
}
public function temporaryFilePath(): string
{
return $this->temporaryFilePath;
}
public function error(): int
{
return $this->error;
}
}