Subir proyecto
This commit is contained in:
+3
@@ -0,0 +1,3 @@
|
||||
# Ignore artifacts:
|
||||
build
|
||||
coverage
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["@prettier/plugin-php"]
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"name": "rocklobsterinc/form-data-tree",
|
||||
"description": "FormDataTree for PHP",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Takayuki Miyoshi",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=8.3",
|
||||
"rocklobsterinc/functions": "^v1.0"
|
||||
},
|
||||
"archive": {
|
||||
"exclude": [
|
||||
"/.*",
|
||||
"/package-lock.json",
|
||||
"/package.json"
|
||||
]
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"RockLobsterInc\\FormDataTree\\": "src/"
|
||||
},
|
||||
"files": [
|
||||
"src/functions.php"
|
||||
]
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "parallel-lint src/"
|
||||
},
|
||||
"require-dev": {
|
||||
"php-parallel-lint/php-parallel-lint": "^1.4"
|
||||
}
|
||||
}
|
||||
Generated
Vendored
+126
@@ -0,0 +1,126 @@
|
||||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "95cf8acd6f2155e2ac4066f970bd21a9",
|
||||
"packages": [
|
||||
{
|
||||
"name": "rocklobsterinc/functions",
|
||||
"version": "v1.0.6",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/rocklobster-in/functions-php.git",
|
||||
"reference": "5390f54a2cbcadadc373ca069f46c7e02fe36fc6"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/rocklobster-in/functions-php/zipball/5390f54a2cbcadadc373ca069f46c7e02fe36fc6",
|
||||
"reference": "5390f54a2cbcadadc373ca069f46c7e02fe36fc6",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"php-parallel-lint/php-parallel-lint": "^1.4"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"files": [
|
||||
"src/functions.php"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Takayuki Miyoshi",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"description": "Rock Lobster's PHP function library",
|
||||
"support": {
|
||||
"issues": "https://github.com/rocklobster-in/functions-php/issues",
|
||||
"source": "https://github.com/rocklobster-in/functions-php/tree/v1.0.6"
|
||||
},
|
||||
"time": "2026-08-14T06:28:00+00:00"
|
||||
}
|
||||
],
|
||||
"packages-dev": [
|
||||
{
|
||||
"name": "php-parallel-lint/php-parallel-lint",
|
||||
"version": "v1.4.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/php-parallel-lint/PHP-Parallel-Lint.git",
|
||||
"reference": "6db563514f27e19595a19f45a4bf757b6401194e"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/php-parallel-lint/PHP-Parallel-Lint/zipball/6db563514f27e19595a19f45a4bf757b6401194e",
|
||||
"reference": "6db563514f27e19595a19f45a4bf757b6401194e",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-json": "*",
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"replace": {
|
||||
"grogy/php-parallel-lint": "*",
|
||||
"jakub-onderka/php-parallel-lint": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"nette/tester": "^1.3 || ^2.0",
|
||||
"php-parallel-lint/php-console-highlighter": "0.* || ^1.0",
|
||||
"squizlabs/php_codesniffer": "^3.6"
|
||||
},
|
||||
"suggest": {
|
||||
"php-parallel-lint/php-console-highlighter": "Highlight syntax in code snippet"
|
||||
},
|
||||
"bin": [
|
||||
"parallel-lint"
|
||||
],
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"./src/"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"BSD-2-Clause"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Jakub Onderka",
|
||||
"email": "ahoj@jakubonderka.cz"
|
||||
}
|
||||
],
|
||||
"description": "This tool checks the syntax of PHP files about 20x faster than serial check.",
|
||||
"homepage": "https://github.com/php-parallel-lint/PHP-Parallel-Lint",
|
||||
"keywords": [
|
||||
"lint",
|
||||
"static analysis"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/php-parallel-lint/PHP-Parallel-Lint/issues",
|
||||
"source": "https://github.com/php-parallel-lint/PHP-Parallel-Lint/tree/v1.4.0"
|
||||
},
|
||||
"time": "2024-03-27T12:14:49+00:00"
|
||||
}
|
||||
],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": {},
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": ">=8.3"
|
||||
},
|
||||
"platform-dev": {},
|
||||
"plugin-api-version": "2.9.0"
|
||||
}
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace RockLobsterInc\FormDataTree;
|
||||
|
||||
use function RockLobsterInc\Functions\{strip_whitespaces};
|
||||
|
||||
/**
|
||||
* Class that represents a standard file data in $_FILES.
|
||||
*/
|
||||
class File implements FileInterface
|
||||
{
|
||||
private string $name;
|
||||
private int $size;
|
||||
private string $temporaryFilePath;
|
||||
private int $error;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $properties Properties of the object.
|
||||
*/
|
||||
private function __construct(array $properties = [])
|
||||
{
|
||||
$this->name = $properties["name"];
|
||||
$this->size = $properties["size"];
|
||||
$this->temporaryFilePath = $properties["temporaryFilePath"];
|
||||
$this->error = $properties["error"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Walks through an array and creates File objects if there are
|
||||
* necessary properties.
|
||||
*
|
||||
* @param mixed $array Where to walk.
|
||||
*/
|
||||
private static function walkToFindSelf(mixed $array)
|
||||
{
|
||||
if (
|
||||
isset($array["name"]) and
|
||||
isset($array["size"]) and
|
||||
isset($array["tmp_name"]) and
|
||||
isset($array["error"])
|
||||
) {
|
||||
$name = strip_whitespaces((string) $array["name"]);
|
||||
$size = (int) $array["size"];
|
||||
$tmp_name = strip_whitespaces((string) $array["tmp_name"]);
|
||||
$error = (int) $array["error"];
|
||||
|
||||
if ("" !== $name and 0 !== $size) {
|
||||
return new self([
|
||||
"name" => $name,
|
||||
"size" => $size,
|
||||
"temporaryFilePath" => $tmp_name,
|
||||
"error" => $error,
|
||||
]);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_array($array)) {
|
||||
return array_map("self::walkToFindSelf", $array);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a tree structure based on the $_FILES superglobal. A terminal
|
||||
* node of the tree must be a File object.
|
||||
*
|
||||
* @return array Tree created based on $_FILES.
|
||||
*/
|
||||
public static function buildTree(): array
|
||||
{
|
||||
static $output = [];
|
||||
|
||||
if (!empty($output)) {
|
||||
return $output;
|
||||
}
|
||||
|
||||
$original = $_FILES;
|
||||
|
||||
foreach ($original as $name => $props) {
|
||||
$in_process_array = [];
|
||||
|
||||
foreach ($props as $key => $value) {
|
||||
$in_process_array[] = scalar_to_map($key, $value);
|
||||
}
|
||||
|
||||
$in_process_array = array_replace_recursive(...$in_process_array);
|
||||
|
||||
$output[$name] = self::walkToFindSelf($in_process_array);
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the original name of the file on the client machine.
|
||||
*/
|
||||
public function name(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size, in bytes, of the file.
|
||||
*/
|
||||
public function size(): int
|
||||
{
|
||||
return $this->size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the full path of the file in which the uploaded file was
|
||||
* stored on the server.
|
||||
*/
|
||||
public function temporaryFilePath(): string
|
||||
{
|
||||
return $this->temporaryFilePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the error code associated with this file upload.
|
||||
*/
|
||||
public function error(): int
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
}
|
||||
Vendored
+30
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace RockLobsterInc\FormDataTree;
|
||||
|
||||
/**
|
||||
* Interface that represents an uploaded file.
|
||||
*/
|
||||
interface FileInterface
|
||||
{
|
||||
/**
|
||||
* Returns the original name of the file on the client machine.
|
||||
*/
|
||||
public function name(): string;
|
||||
|
||||
/**
|
||||
* Returns the size, in bytes, of the file.
|
||||
*/
|
||||
public function size(): int;
|
||||
|
||||
/**
|
||||
* Returns the full path of the file in which the uploaded file was
|
||||
* stored on the server.
|
||||
*/
|
||||
public function temporaryFilePath(): string;
|
||||
|
||||
/**
|
||||
* Returns the error code associated with this file upload.
|
||||
*/
|
||||
public function error(): int;
|
||||
}
|
||||
Vendored
+98
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace RockLobsterInc\FormDataTree;
|
||||
|
||||
use function RockLobsterInc\Functions\{strip_whitespaces, exclude_blank};
|
||||
|
||||
/**
|
||||
* A class that implements FormDataTreeInterface. Wraps the PHP superglobals.
|
||||
*/
|
||||
class FormDataTree implements FormDataTreeInterface
|
||||
{
|
||||
/**
|
||||
* Data origin.
|
||||
*/
|
||||
private array $origin;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $origin Optional data origin.
|
||||
*/
|
||||
public function __construct(array $origin = [])
|
||||
{
|
||||
$this->origin = [
|
||||
"post" => $origin["post"] ?? $_POST,
|
||||
"files" => $origin["files"] ?? File::buildTree(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the values associated with a given field name.
|
||||
*
|
||||
* @param string $name Field name.
|
||||
* @return iterable Iterator of the values.
|
||||
*/
|
||||
public function getAll(string $name): iterable
|
||||
{
|
||||
$name_parts = dissolve_name($name);
|
||||
|
||||
if (empty($name_parts)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$posted_value = $this->origin["post"];
|
||||
|
||||
while ($next = array_shift($name_parts)) {
|
||||
if (
|
||||
preg_match('/^[0-9]*$/', $next) or !isset($posted_value[$next])
|
||||
) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$posted_value = $posted_value[$next];
|
||||
}
|
||||
|
||||
if (!is_array($posted_value)) {
|
||||
$posted_value = [$posted_value];
|
||||
}
|
||||
|
||||
$posted_value = strip_whitespaces($posted_value);
|
||||
$posted_value = exclude_blank($posted_value);
|
||||
|
||||
return $posted_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file objects associated with a given field name.
|
||||
*
|
||||
* @param string $name Field name.
|
||||
* @return iterable Iterator of the FileInterface objects.
|
||||
*/
|
||||
public function getAllFiles(string $name): iterable
|
||||
{
|
||||
$name_parts = dissolve_name($name);
|
||||
|
||||
if (empty($name_parts)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$files_tree = $this->origin["files"];
|
||||
|
||||
while ($next = array_shift($name_parts)) {
|
||||
if (preg_match('/^[0-9]*$/', $next) or !isset($files_tree[$next])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$files_tree = $files_tree[$next];
|
||||
}
|
||||
|
||||
if (!is_array($files_tree)) {
|
||||
$files_tree = [$files_tree];
|
||||
}
|
||||
|
||||
$files_tree = exclude_blank($files_tree);
|
||||
|
||||
return $files_tree;
|
||||
}
|
||||
}
|
||||
wp-content/plugins/contact-form-7/vendor/rocklobsterinc/form-data-tree/src/FormDataTreeInterface.php
Vendored
+26
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace RockLobsterInc\FormDataTree;
|
||||
|
||||
/**
|
||||
* An interface that represents form submission data in the key/value pairs
|
||||
* format.
|
||||
*/
|
||||
interface FormDataTreeInterface
|
||||
{
|
||||
/**
|
||||
* Returns the values associated with a given field name.
|
||||
*
|
||||
* @param string $name Field name.
|
||||
* @return iterable Iterator of the values.
|
||||
*/
|
||||
public function getAll(string $name): iterable;
|
||||
|
||||
/**
|
||||
* Returns the file objects associated with a given field name.
|
||||
*
|
||||
* @param string $name Field name.
|
||||
* @return iterable Iterator of the file objects.
|
||||
*/
|
||||
public function getAllFiles(string $name): iterable;
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace RockLobsterInc\FormDataTree;
|
||||
|
||||
use function RockLobsterInc\Functions\{strip_whitespaces};
|
||||
|
||||
/**
|
||||
* Returns components of the given name.
|
||||
*
|
||||
* @param string $name Field name, such as 'abc' or 'abc[de]'.
|
||||
* @return array Single dimension array of name components.
|
||||
*/
|
||||
function dissolve_name(string $name): array
|
||||
{
|
||||
$name = strip_whitespaces($name);
|
||||
|
||||
$s =
|
||||
'[\x09-\x0D\x20\xA0\x{1680}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}\x{FEFF}]*';
|
||||
|
||||
$pattern =
|
||||
"/^" .
|
||||
"([a-z][0-9a-z:_-]*)" . // Core
|
||||
"((?:\[" .
|
||||
$s .
|
||||
"(?:[a-z][0-9a-z:_-]*|[0-9]*)" .
|
||||
$s .
|
||||
"\])*)" . // Layers
|
||||
'$/iu';
|
||||
|
||||
if (!preg_match($pattern, $name, $matches)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$core = $matches[1];
|
||||
$layers = $matches[2];
|
||||
|
||||
preg_match_all("/\[(.*?)\]/u", $layers, $matches);
|
||||
|
||||
$layers = array_map(static function (string $item) {
|
||||
return strip_whitespaces($item);
|
||||
}, $matches[1]);
|
||||
|
||||
return [$core, ...$layers];
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a scalar value into a map with a specified key. The original
|
||||
* array structure will be preserved.
|
||||
*
|
||||
* @param string $key Map key.
|
||||
* @param mixed $value Original value.
|
||||
* @return array Array.
|
||||
*/
|
||||
function scalar_to_map(string $key, mixed $value): array
|
||||
{
|
||||
if (is_scalar($value)) {
|
||||
return [$key => $value];
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
return array_map(static function ($item) use ($key) {
|
||||
return scalar_to_map($key, $item);
|
||||
}, $value);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
# Ignore artifacts:
|
||||
build
|
||||
coverage
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["@prettier/plugin-php"]
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "rocklobsterinc/functions",
|
||||
"description": "Rock Lobster's PHP function library",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Takayuki Miyoshi",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=8.3"
|
||||
},
|
||||
"archive": {
|
||||
"exclude": [
|
||||
"/.*",
|
||||
"/package-lock.json",
|
||||
"/package.json"
|
||||
]
|
||||
},
|
||||
"autoload": {
|
||||
"files": [
|
||||
"src/functions.php"
|
||||
]
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "parallel-lint src/"
|
||||
},
|
||||
"require-dev": {
|
||||
"php-parallel-lint/php-parallel-lint": "^1.4"
|
||||
}
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "0e474568fe06ec195ce4053cb1007d2d",
|
||||
"packages": [],
|
||||
"packages-dev": [
|
||||
{
|
||||
"name": "php-parallel-lint/php-parallel-lint",
|
||||
"version": "v1.4.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/php-parallel-lint/PHP-Parallel-Lint.git",
|
||||
"reference": "6db563514f27e19595a19f45a4bf757b6401194e"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/php-parallel-lint/PHP-Parallel-Lint/zipball/6db563514f27e19595a19f45a4bf757b6401194e",
|
||||
"reference": "6db563514f27e19595a19f45a4bf757b6401194e",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-json": "*",
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"replace": {
|
||||
"grogy/php-parallel-lint": "*",
|
||||
"jakub-onderka/php-parallel-lint": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"nette/tester": "^1.3 || ^2.0",
|
||||
"php-parallel-lint/php-console-highlighter": "0.* || ^1.0",
|
||||
"squizlabs/php_codesniffer": "^3.6"
|
||||
},
|
||||
"suggest": {
|
||||
"php-parallel-lint/php-console-highlighter": "Highlight syntax in code snippet"
|
||||
},
|
||||
"bin": [
|
||||
"parallel-lint"
|
||||
],
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"./src/"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"BSD-2-Clause"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Jakub Onderka",
|
||||
"email": "ahoj@jakubonderka.cz"
|
||||
}
|
||||
],
|
||||
"description": "This tool checks the syntax of PHP files about 20x faster than serial check.",
|
||||
"homepage": "https://github.com/php-parallel-lint/PHP-Parallel-Lint",
|
||||
"keywords": [
|
||||
"lint",
|
||||
"static analysis"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/php-parallel-lint/PHP-Parallel-Lint/issues",
|
||||
"source": "https://github.com/php-parallel-lint/PHP-Parallel-Lint/tree/v1.4.0"
|
||||
},
|
||||
"time": "2024-03-27T12:14:49+00:00"
|
||||
}
|
||||
],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": {},
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": ">=8.3"
|
||||
},
|
||||
"platform-dev": {},
|
||||
"plugin-api-version": "2.9.0"
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace RockLobsterInc\Functions;
|
||||
|
||||
/**
|
||||
* Converts multi-dimensional array to a flat array.
|
||||
*
|
||||
* @param mixed $input Array or item of array.
|
||||
* @return array Flatten array.
|
||||
*/
|
||||
function array_flatten(mixed $input): array
|
||||
{
|
||||
if (!is_array($input)) {
|
||||
return [$input];
|
||||
}
|
||||
|
||||
return array_reduce(
|
||||
$input,
|
||||
static function ($carry, $item) {
|
||||
return array_merge($carry, array_flatten($item));
|
||||
},
|
||||
[],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Strips surrounding whitespaces.
|
||||
*
|
||||
* @param string|array $input Input text or array of text.
|
||||
* @return string|array Output text or array of text.
|
||||
*/
|
||||
function strip_whitespaces(string|array $input): string|array
|
||||
{
|
||||
if (is_array($input)) {
|
||||
return array_map(static function ($item) {
|
||||
return strip_whitespaces($item);
|
||||
}, $input);
|
||||
}
|
||||
|
||||
$whitespaces =
|
||||
'\x09-\x0D\x20\xA0\x{1680}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}\x{FEFF}';
|
||||
|
||||
$input = preg_replace(sprintf("/^[%s]+/u", $whitespaces), "", $input);
|
||||
|
||||
$input = preg_replace(sprintf('/[%s]+$/u', $whitespaces), "", $input);
|
||||
|
||||
return $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts all newline characters into line feed (LF).
|
||||
*
|
||||
* @param string|array $input Input text or array of text.
|
||||
* @return string|array Output text or array of text.
|
||||
*/
|
||||
function canonicalize_newline(string|array $input): string|array
|
||||
{
|
||||
if (is_array($input)) {
|
||||
return array_map(static function ($item) {
|
||||
return canonicalize_newline($item);
|
||||
}, $input);
|
||||
}
|
||||
|
||||
return str_replace(["\r\n", "\r", "\n"], "\n", $input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Excludes unset or blank text values from the given array.
|
||||
*
|
||||
* @param array $input The array.
|
||||
* @return array Array without blank text values.
|
||||
*/
|
||||
function exclude_blank(array $input): array
|
||||
{
|
||||
$output = [];
|
||||
|
||||
foreach ($input as $key => $item) {
|
||||
if (is_array($item)) {
|
||||
$item = exclude_blank($item);
|
||||
|
||||
if (!empty($item)) {
|
||||
$output[$key] = $item;
|
||||
}
|
||||
} elseif (isset($item)) {
|
||||
if ("" !== $item) {
|
||||
$output[$key] = $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
+1848
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>
|
||||
+42
@@ -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 [];
|
||||
}
|
||||
}
|
||||
+34
@@ -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;
|
||||
}
|
||||
}
|
||||
+56
@@ -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 ?? "";
|
||||
}
|
||||
}
|
||||
+84
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+95
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+106
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+95
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+136
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+92
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+269
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+88
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+95
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+86
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+100
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+91
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+88
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+95
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+86
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+100
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+91
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+108
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+79
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+79
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+109
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+113
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+112
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+99
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
+30
@@ -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);
|
||||
}
|
||||
+91
@@ -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]);
|
||||
}
|
||||
}
|
||||
+94
@@ -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]);
|
||||
}
|
||||
}
|
||||
+70
@@ -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);
|
||||
}
|
||||
}
|
||||
+72
@@ -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);
|
||||
}
|
||||
}
|
||||
+69
@@ -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);
|
||||
}
|
||||
}
|
||||
+71
@@ -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);
|
||||
}
|
||||
}
|
||||
+87
@@ -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);
|
||||
}
|
||||
}
|
||||
+72
@@ -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);
|
||||
}
|
||||
}
|
||||
+85
@@ -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);
|
||||
}
|
||||
}
|
||||
+71
@@ -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);
|
||||
}
|
||||
}
|
||||
+71
@@ -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);
|
||||
}
|
||||
}
|
||||
+72
@@ -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);
|
||||
}
|
||||
}
|
||||
+72
@@ -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);
|
||||
}
|
||||
}
|
||||
+85
@@ -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);
|
||||
}
|
||||
}
|
||||
+71
@@ -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);
|
||||
}
|
||||
}
|
||||
+71
@@ -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);
|
||||
}
|
||||
}
|
||||
+72
@@ -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);
|
||||
}
|
||||
}
|
||||
+70
@@ -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);
|
||||
}
|
||||
}
|
||||
+75
@@ -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);
|
||||
}
|
||||
}
|
||||
+68
@@ -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);
|
||||
}
|
||||
}
|
||||
+74
@@ -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);
|
||||
}
|
||||
}
|
||||
+73
@@ -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);
|
||||
}
|
||||
}
|
||||
+70
@@ -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);
|
||||
}
|
||||
}
|
||||
+70
@@ -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);
|
||||
}
|
||||
}
|
||||
+41
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user