Commit inicial powerservice
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
// autoload.php @generated by Composer
|
||||
|
||||
if (PHP_VERSION_ID < 50600) {
|
||||
if (!headers_sent()) {
|
||||
header('HTTP/1.1 500 Internal Server Error');
|
||||
}
|
||||
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
|
||||
if (!ini_get('display_errors')) {
|
||||
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
|
||||
fwrite(STDERR, $err);
|
||||
} elseif (!headers_sent()) {
|
||||
echo $err;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException($err);
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitf850b4a531317914b9045752ab7ba3f8::getLoader();
|
||||
@@ -0,0 +1,579 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Composer.
|
||||
*
|
||||
* (c) Nils Adermann <naderman@naderman.de>
|
||||
* Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
/**
|
||||
* ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
|
||||
*
|
||||
* $loader = new \Composer\Autoload\ClassLoader();
|
||||
*
|
||||
* // register classes with namespaces
|
||||
* $loader->add('Symfony\Component', __DIR__.'/component');
|
||||
* $loader->add('Symfony', __DIR__.'/framework');
|
||||
*
|
||||
* // activate the autoloader
|
||||
* $loader->register();
|
||||
*
|
||||
* // to enable searching the include path (eg. for PEAR packages)
|
||||
* $loader->setUseIncludePath(true);
|
||||
*
|
||||
* In this example, if you try to use a class in the Symfony\Component
|
||||
* namespace or one of its children (Symfony\Component\Console for instance),
|
||||
* the autoloader will first look for the class under the component/
|
||||
* directory, and it will then fallback to the framework/ directory if not
|
||||
* found before giving up.
|
||||
*
|
||||
* This class is loosely based on the Symfony UniversalClassLoader.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
* @see https://www.php-fig.org/psr/psr-0/
|
||||
* @see https://www.php-fig.org/psr/psr-4/
|
||||
*/
|
||||
class ClassLoader
|
||||
{
|
||||
/** @var \Closure(string):void */
|
||||
private static $includeFile;
|
||||
|
||||
/** @var string|null */
|
||||
private $vendorDir;
|
||||
|
||||
// PSR-4
|
||||
/**
|
||||
* @var array<string, array<string, int>>
|
||||
*/
|
||||
private $prefixLengthsPsr4 = array();
|
||||
/**
|
||||
* @var array<string, list<string>>
|
||||
*/
|
||||
private $prefixDirsPsr4 = array();
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
private $fallbackDirsPsr4 = array();
|
||||
|
||||
// PSR-0
|
||||
/**
|
||||
* List of PSR-0 prefixes
|
||||
*
|
||||
* Structured as array('F (first letter)' => array('Foo\Bar (full prefix)' => array('path', 'path2')))
|
||||
*
|
||||
* @var array<string, array<string, list<string>>>
|
||||
*/
|
||||
private $prefixesPsr0 = array();
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
private $fallbackDirsPsr0 = array();
|
||||
|
||||
/** @var bool */
|
||||
private $useIncludePath = false;
|
||||
|
||||
/**
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private $classMap = array();
|
||||
|
||||
/** @var bool */
|
||||
private $classMapAuthoritative = false;
|
||||
|
||||
/**
|
||||
* @var array<string, bool>
|
||||
*/
|
||||
private $missingClasses = array();
|
||||
|
||||
/** @var string|null */
|
||||
private $apcuPrefix;
|
||||
|
||||
/**
|
||||
* @var array<string, self>
|
||||
*/
|
||||
private static $registeredLoaders = array();
|
||||
|
||||
/**
|
||||
* @param string|null $vendorDir
|
||||
*/
|
||||
public function __construct($vendorDir = null)
|
||||
{
|
||||
$this->vendorDir = $vendorDir;
|
||||
self::initializeIncludeClosure();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, list<string>>
|
||||
*/
|
||||
public function getPrefixes()
|
||||
{
|
||||
if (!empty($this->prefixesPsr0)) {
|
||||
return call_user_func_array('array_merge', array_values($this->prefixesPsr0));
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, list<string>>
|
||||
*/
|
||||
public function getPrefixesPsr4()
|
||||
{
|
||||
return $this->prefixDirsPsr4;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public function getFallbackDirs()
|
||||
{
|
||||
return $this->fallbackDirsPsr0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public function getFallbackDirsPsr4()
|
||||
{
|
||||
return $this->fallbackDirsPsr4;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string> Array of classname => path
|
||||
*/
|
||||
public function getClassMap()
|
||||
{
|
||||
return $this->classMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $classMap Class to filename map
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addClassMap(array $classMap)
|
||||
{
|
||||
if ($this->classMap) {
|
||||
$this->classMap = array_merge($this->classMap, $classMap);
|
||||
} else {
|
||||
$this->classMap = $classMap;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a set of PSR-0 directories for a given prefix, either
|
||||
* appending or prepending to the ones previously set for this prefix.
|
||||
*
|
||||
* @param string $prefix The prefix
|
||||
* @param list<string>|string $paths The PSR-0 root directories
|
||||
* @param bool $prepend Whether to prepend the directories
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function add($prefix, $paths, $prepend = false)
|
||||
{
|
||||
$paths = (array) $paths;
|
||||
if (!$prefix) {
|
||||
if ($prepend) {
|
||||
$this->fallbackDirsPsr0 = array_merge(
|
||||
$paths,
|
||||
$this->fallbackDirsPsr0
|
||||
);
|
||||
} else {
|
||||
$this->fallbackDirsPsr0 = array_merge(
|
||||
$this->fallbackDirsPsr0,
|
||||
$paths
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$first = $prefix[0];
|
||||
if (!isset($this->prefixesPsr0[$first][$prefix])) {
|
||||
$this->prefixesPsr0[$first][$prefix] = $paths;
|
||||
|
||||
return;
|
||||
}
|
||||
if ($prepend) {
|
||||
$this->prefixesPsr0[$first][$prefix] = array_merge(
|
||||
$paths,
|
||||
$this->prefixesPsr0[$first][$prefix]
|
||||
);
|
||||
} else {
|
||||
$this->prefixesPsr0[$first][$prefix] = array_merge(
|
||||
$this->prefixesPsr0[$first][$prefix],
|
||||
$paths
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a set of PSR-4 directories for a given namespace, either
|
||||
* appending or prepending to the ones previously set for this namespace.
|
||||
*
|
||||
* @param string $prefix The prefix/namespace, with trailing '\\'
|
||||
* @param list<string>|string $paths The PSR-4 base directories
|
||||
* @param bool $prepend Whether to prepend the directories
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addPsr4($prefix, $paths, $prepend = false)
|
||||
{
|
||||
$paths = (array) $paths;
|
||||
if (!$prefix) {
|
||||
// Register directories for the root namespace.
|
||||
if ($prepend) {
|
||||
$this->fallbackDirsPsr4 = array_merge(
|
||||
$paths,
|
||||
$this->fallbackDirsPsr4
|
||||
);
|
||||
} else {
|
||||
$this->fallbackDirsPsr4 = array_merge(
|
||||
$this->fallbackDirsPsr4,
|
||||
$paths
|
||||
);
|
||||
}
|
||||
} elseif (!isset($this->prefixDirsPsr4[$prefix])) {
|
||||
// Register directories for a new namespace.
|
||||
$length = strlen($prefix);
|
||||
if ('\\' !== $prefix[$length - 1]) {
|
||||
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
|
||||
}
|
||||
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
|
||||
$this->prefixDirsPsr4[$prefix] = $paths;
|
||||
} elseif ($prepend) {
|
||||
// Prepend directories for an already registered namespace.
|
||||
$this->prefixDirsPsr4[$prefix] = array_merge(
|
||||
$paths,
|
||||
$this->prefixDirsPsr4[$prefix]
|
||||
);
|
||||
} else {
|
||||
// Append directories for an already registered namespace.
|
||||
$this->prefixDirsPsr4[$prefix] = array_merge(
|
||||
$this->prefixDirsPsr4[$prefix],
|
||||
$paths
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a set of PSR-0 directories for a given prefix,
|
||||
* replacing any others previously set for this prefix.
|
||||
*
|
||||
* @param string $prefix The prefix
|
||||
* @param list<string>|string $paths The PSR-0 base directories
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set($prefix, $paths)
|
||||
{
|
||||
if (!$prefix) {
|
||||
$this->fallbackDirsPsr0 = (array) $paths;
|
||||
} else {
|
||||
$this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a set of PSR-4 directories for a given namespace,
|
||||
* replacing any others previously set for this namespace.
|
||||
*
|
||||
* @param string $prefix The prefix/namespace, with trailing '\\'
|
||||
* @param list<string>|string $paths The PSR-4 base directories
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setPsr4($prefix, $paths)
|
||||
{
|
||||
if (!$prefix) {
|
||||
$this->fallbackDirsPsr4 = (array) $paths;
|
||||
} else {
|
||||
$length = strlen($prefix);
|
||||
if ('\\' !== $prefix[$length - 1]) {
|
||||
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
|
||||
}
|
||||
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
|
||||
$this->prefixDirsPsr4[$prefix] = (array) $paths;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns on searching the include path for class files.
|
||||
*
|
||||
* @param bool $useIncludePath
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUseIncludePath($useIncludePath)
|
||||
{
|
||||
$this->useIncludePath = $useIncludePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can be used to check if the autoloader uses the include path to check
|
||||
* for classes.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getUseIncludePath()
|
||||
{
|
||||
return $this->useIncludePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns off searching the prefix and fallback directories for classes
|
||||
* that have not been registered with the class map.
|
||||
*
|
||||
* @param bool $classMapAuthoritative
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setClassMapAuthoritative($classMapAuthoritative)
|
||||
{
|
||||
$this->classMapAuthoritative = $classMapAuthoritative;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should class lookup fail if not found in the current class map?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isClassMapAuthoritative()
|
||||
{
|
||||
return $this->classMapAuthoritative;
|
||||
}
|
||||
|
||||
/**
|
||||
* APCu prefix to use to cache found/not-found classes, if the extension is enabled.
|
||||
*
|
||||
* @param string|null $apcuPrefix
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setApcuPrefix($apcuPrefix)
|
||||
{
|
||||
$this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The APCu prefix in use, or null if APCu caching is not enabled.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getApcuPrefix()
|
||||
{
|
||||
return $this->apcuPrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers this instance as an autoloader.
|
||||
*
|
||||
* @param bool $prepend Whether to prepend the autoloader or not
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register($prepend = false)
|
||||
{
|
||||
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
|
||||
|
||||
if (null === $this->vendorDir) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($prepend) {
|
||||
self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders;
|
||||
} else {
|
||||
unset(self::$registeredLoaders[$this->vendorDir]);
|
||||
self::$registeredLoaders[$this->vendorDir] = $this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters this instance as an autoloader.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function unregister()
|
||||
{
|
||||
spl_autoload_unregister(array($this, 'loadClass'));
|
||||
|
||||
if (null !== $this->vendorDir) {
|
||||
unset(self::$registeredLoaders[$this->vendorDir]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the given class or interface.
|
||||
*
|
||||
* @param string $class The name of the class
|
||||
* @return true|null True if loaded, null otherwise
|
||||
*/
|
||||
public function loadClass($class)
|
||||
{
|
||||
if ($file = $this->findFile($class)) {
|
||||
$includeFile = self::$includeFile;
|
||||
$includeFile($file);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the path to the file where the class is defined.
|
||||
*
|
||||
* @param string $class The name of the class
|
||||
*
|
||||
* @return string|false The path if found, false otherwise
|
||||
*/
|
||||
public function findFile($class)
|
||||
{
|
||||
// class map lookup
|
||||
if (isset($this->classMap[$class])) {
|
||||
return $this->classMap[$class];
|
||||
}
|
||||
if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
|
||||
return false;
|
||||
}
|
||||
if (null !== $this->apcuPrefix) {
|
||||
$file = apcu_fetch($this->apcuPrefix.$class, $hit);
|
||||
if ($hit) {
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
|
||||
$file = $this->findFileWithExtension($class, '.php');
|
||||
|
||||
// Search for Hack files if we are running on HHVM
|
||||
if (false === $file && defined('HHVM_VERSION')) {
|
||||
$file = $this->findFileWithExtension($class, '.hh');
|
||||
}
|
||||
|
||||
if (null !== $this->apcuPrefix) {
|
||||
apcu_add($this->apcuPrefix.$class, $file);
|
||||
}
|
||||
|
||||
if (false === $file) {
|
||||
// Remember that this class does not exist.
|
||||
$this->missingClasses[$class] = true;
|
||||
}
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the currently registered loaders keyed by their corresponding vendor directories.
|
||||
*
|
||||
* @return array<string, self>
|
||||
*/
|
||||
public static function getRegisteredLoaders()
|
||||
{
|
||||
return self::$registeredLoaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $ext
|
||||
* @return string|false
|
||||
*/
|
||||
private function findFileWithExtension($class, $ext)
|
||||
{
|
||||
// PSR-4 lookup
|
||||
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
|
||||
|
||||
$first = $class[0];
|
||||
if (isset($this->prefixLengthsPsr4[$first])) {
|
||||
$subPath = $class;
|
||||
while (false !== $lastPos = strrpos($subPath, '\\')) {
|
||||
$subPath = substr($subPath, 0, $lastPos);
|
||||
$search = $subPath . '\\';
|
||||
if (isset($this->prefixDirsPsr4[$search])) {
|
||||
$pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
|
||||
foreach ($this->prefixDirsPsr4[$search] as $dir) {
|
||||
if (file_exists($file = $dir . $pathEnd)) {
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PSR-4 fallback dirs
|
||||
foreach ($this->fallbackDirsPsr4 as $dir) {
|
||||
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
|
||||
// PSR-0 lookup
|
||||
if (false !== $pos = strrpos($class, '\\')) {
|
||||
// namespaced class name
|
||||
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
|
||||
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
|
||||
} else {
|
||||
// PEAR-like class name
|
||||
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
|
||||
}
|
||||
|
||||
if (isset($this->prefixesPsr0[$first])) {
|
||||
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
|
||||
if (0 === strpos($class, $prefix)) {
|
||||
foreach ($dirs as $dir) {
|
||||
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PSR-0 fallback dirs
|
||||
foreach ($this->fallbackDirsPsr0 as $dir) {
|
||||
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
|
||||
// PSR-0 include paths.
|
||||
if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
|
||||
return $file;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private static function initializeIncludeClosure()
|
||||
{
|
||||
if (self::$includeFile !== null) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope isolated include.
|
||||
*
|
||||
* Prevents access to $this/self from included files.
|
||||
*
|
||||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
self::$includeFile = \Closure::bind(static function($file) {
|
||||
include $file;
|
||||
}, null, null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,396 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Composer.
|
||||
*
|
||||
* (c) Nils Adermann <naderman@naderman.de>
|
||||
* Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Composer;
|
||||
|
||||
use Composer\Autoload\ClassLoader;
|
||||
use Composer\Semver\VersionParser;
|
||||
|
||||
/**
|
||||
* This class is copied in every Composer installed project and available to all
|
||||
*
|
||||
* See also https://getcomposer.org/doc/07-runtime.md#installed-versions
|
||||
*
|
||||
* To require its presence, you can require `composer-runtime-api ^2.0`
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
class InstalledVersions
|
||||
{
|
||||
/**
|
||||
* @var string|null if set (by reflection by Composer), this should be set to the path where this class is being copied to
|
||||
* @internal
|
||||
*/
|
||||
private static $selfDir = null;
|
||||
|
||||
/**
|
||||
* @var mixed[]|null
|
||||
* @psalm-var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}|array{}|null
|
||||
*/
|
||||
private static $installed;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private static $installedIsLocalDir;
|
||||
|
||||
/**
|
||||
* @var bool|null
|
||||
*/
|
||||
private static $canGetVendors;
|
||||
|
||||
/**
|
||||
* @var array[]
|
||||
* @psalm-var array<string, array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
|
||||
*/
|
||||
private static $installedByVendor = array();
|
||||
|
||||
/**
|
||||
* Returns a list of all package names which are present, either by being installed, replaced or provided
|
||||
*
|
||||
* @return string[]
|
||||
* @psalm-return list<string>
|
||||
*/
|
||||
public static function getInstalledPackages()
|
||||
{
|
||||
$packages = array();
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
$packages[] = array_keys($installed['versions']);
|
||||
}
|
||||
|
||||
if (1 === \count($packages)) {
|
||||
return $packages[0];
|
||||
}
|
||||
|
||||
return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all package names with a specific type e.g. 'library'
|
||||
*
|
||||
* @param string $type
|
||||
* @return string[]
|
||||
* @psalm-return list<string>
|
||||
*/
|
||||
public static function getInstalledPackagesByType($type)
|
||||
{
|
||||
$packagesByType = array();
|
||||
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
foreach ($installed['versions'] as $name => $package) {
|
||||
if (isset($package['type']) && $package['type'] === $type) {
|
||||
$packagesByType[] = $name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $packagesByType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given package is installed
|
||||
*
|
||||
* This also returns true if the package name is provided or replaced by another package
|
||||
*
|
||||
* @param string $packageName
|
||||
* @param bool $includeDevRequirements
|
||||
* @return bool
|
||||
*/
|
||||
public static function isInstalled($packageName, $includeDevRequirements = true)
|
||||
{
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
if (isset($installed['versions'][$packageName])) {
|
||||
return $includeDevRequirements || !isset($installed['versions'][$packageName]['dev_requirement']) || $installed['versions'][$packageName]['dev_requirement'] === false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given package satisfies a version constraint
|
||||
*
|
||||
* e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call:
|
||||
*
|
||||
* Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3')
|
||||
*
|
||||
* @param VersionParser $parser Install composer/semver to have access to this class and functionality
|
||||
* @param string $packageName
|
||||
* @param string|null $constraint A version constraint to check for, if you pass one you have to make sure composer/semver is required by your package
|
||||
* @return bool
|
||||
*/
|
||||
public static function satisfies(VersionParser $parser, $packageName, $constraint)
|
||||
{
|
||||
$constraint = $parser->parseConstraints((string) $constraint);
|
||||
$provided = $parser->parseConstraints(self::getVersionRanges($packageName));
|
||||
|
||||
return $provided->matches($constraint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a version constraint representing all the range(s) which are installed for a given package
|
||||
*
|
||||
* It is easier to use this via isInstalled() with the $constraint argument if you need to check
|
||||
* whether a given version of a package is installed, and not just whether it exists
|
||||
*
|
||||
* @param string $packageName
|
||||
* @return string Version constraint usable with composer/semver
|
||||
*/
|
||||
public static function getVersionRanges($packageName)
|
||||
{
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
if (!isset($installed['versions'][$packageName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$ranges = array();
|
||||
if (isset($installed['versions'][$packageName]['pretty_version'])) {
|
||||
$ranges[] = $installed['versions'][$packageName]['pretty_version'];
|
||||
}
|
||||
if (array_key_exists('aliases', $installed['versions'][$packageName])) {
|
||||
$ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
|
||||
}
|
||||
if (array_key_exists('replaced', $installed['versions'][$packageName])) {
|
||||
$ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
|
||||
}
|
||||
if (array_key_exists('provided', $installed['versions'][$packageName])) {
|
||||
$ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
|
||||
}
|
||||
|
||||
return implode(' || ', $ranges);
|
||||
}
|
||||
|
||||
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $packageName
|
||||
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present
|
||||
*/
|
||||
public static function getVersion($packageName)
|
||||
{
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
if (!isset($installed['versions'][$packageName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($installed['versions'][$packageName]['version'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $installed['versions'][$packageName]['version'];
|
||||
}
|
||||
|
||||
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $packageName
|
||||
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present
|
||||
*/
|
||||
public static function getPrettyVersion($packageName)
|
||||
{
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
if (!isset($installed['versions'][$packageName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($installed['versions'][$packageName]['pretty_version'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $installed['versions'][$packageName]['pretty_version'];
|
||||
}
|
||||
|
||||
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $packageName
|
||||
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference
|
||||
*/
|
||||
public static function getReference($packageName)
|
||||
{
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
if (!isset($installed['versions'][$packageName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($installed['versions'][$packageName]['reference'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $installed['versions'][$packageName]['reference'];
|
||||
}
|
||||
|
||||
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $packageName
|
||||
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as install path. Packages of type metapackages also have a null install path.
|
||||
*/
|
||||
public static function getInstallPath($packageName)
|
||||
{
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
if (!isset($installed['versions'][$packageName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null;
|
||||
}
|
||||
|
||||
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @psalm-return array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}
|
||||
*/
|
||||
public static function getRootPackage()
|
||||
{
|
||||
$installed = self::getInstalled();
|
||||
|
||||
return $installed[0]['root'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the raw installed.php data for custom implementations
|
||||
*
|
||||
* @deprecated Use getAllRawData() instead which returns all datasets for all autoloaders present in the process. getRawData only returns the first dataset loaded, which may not be what you expect.
|
||||
* @return array[]
|
||||
* @psalm-return array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}
|
||||
*/
|
||||
public static function getRawData()
|
||||
{
|
||||
@trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED);
|
||||
|
||||
if (null === self::$installed) {
|
||||
// only require the installed.php file if this file is loaded from its dumped location,
|
||||
// and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
|
||||
if (substr(__DIR__, -8, 1) !== 'C') {
|
||||
self::$installed = include __DIR__ . '/installed.php';
|
||||
} else {
|
||||
self::$installed = array();
|
||||
}
|
||||
}
|
||||
|
||||
return self::$installed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the raw data of all installed.php which are currently loaded for custom implementations
|
||||
*
|
||||
* @return array[]
|
||||
* @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
|
||||
*/
|
||||
public static function getAllRawData()
|
||||
{
|
||||
return self::getInstalled();
|
||||
}
|
||||
|
||||
/**
|
||||
* Lets you reload the static array from another file
|
||||
*
|
||||
* This is only useful for complex integrations in which a project needs to use
|
||||
* this class but then also needs to execute another project's autoloader in process,
|
||||
* and wants to ensure both projects have access to their version of installed.php.
|
||||
*
|
||||
* A typical case would be PHPUnit, where it would need to make sure it reads all
|
||||
* the data it needs from this class, then call reload() with
|
||||
* `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure
|
||||
* the project in which it runs can then also use this class safely, without
|
||||
* interference between PHPUnit's dependencies and the project's dependencies.
|
||||
*
|
||||
* @param array[] $data A vendor/composer/installed.php data set
|
||||
* @return void
|
||||
*
|
||||
* @psalm-param array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $data
|
||||
*/
|
||||
public static function reload($data)
|
||||
{
|
||||
self::$installed = $data;
|
||||
self::$installedByVendor = array();
|
||||
|
||||
// when using reload, we disable the duplicate protection to ensure that self::$installed data is
|
||||
// always returned, but we cannot know whether it comes from the installed.php in __DIR__ or not,
|
||||
// so we have to assume it does not, and that may result in duplicate data being returned when listing
|
||||
// all installed packages for example
|
||||
self::$installedIsLocalDir = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private static function getSelfDir()
|
||||
{
|
||||
if (self::$selfDir === null) {
|
||||
self::$selfDir = strtr(__DIR__, '\\', '/');
|
||||
}
|
||||
|
||||
return self::$selfDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array[]
|
||||
* @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
|
||||
*/
|
||||
private static function getInstalled()
|
||||
{
|
||||
if (null === self::$canGetVendors) {
|
||||
self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
|
||||
}
|
||||
|
||||
$installed = array();
|
||||
$copiedLocalDir = false;
|
||||
|
||||
if (self::$canGetVendors) {
|
||||
$selfDir = self::getSelfDir();
|
||||
foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
|
||||
$vendorDir = strtr($vendorDir, '\\', '/');
|
||||
if (isset(self::$installedByVendor[$vendorDir])) {
|
||||
$installed[] = self::$installedByVendor[$vendorDir];
|
||||
} elseif (is_file($vendorDir.'/composer/installed.php')) {
|
||||
/** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $required */
|
||||
$required = require $vendorDir.'/composer/installed.php';
|
||||
self::$installedByVendor[$vendorDir] = $required;
|
||||
$installed[] = $required;
|
||||
if (self::$installed === null && $vendorDir.'/composer' === $selfDir) {
|
||||
self::$installed = $required;
|
||||
self::$installedIsLocalDir = true;
|
||||
}
|
||||
}
|
||||
if (self::$installedIsLocalDir && $vendorDir.'/composer' === $selfDir) {
|
||||
$copiedLocalDir = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (null === self::$installed) {
|
||||
// only require the installed.php file if this file is loaded from its dumped location,
|
||||
// and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
|
||||
if (substr(__DIR__, -8, 1) !== 'C') {
|
||||
/** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $required */
|
||||
$required = require __DIR__ . '/installed.php';
|
||||
self::$installed = $required;
|
||||
} else {
|
||||
self::$installed = array();
|
||||
}
|
||||
}
|
||||
|
||||
if (self::$installed !== array() && !$copiedLocalDir) {
|
||||
$installed[] = self::$installed;
|
||||
}
|
||||
|
||||
return $installed;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
// autoload_classmap.php @generated by Composer
|
||||
|
||||
$vendorDir = dirname(__DIR__);
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
|
||||
);
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
// autoload_files.php @generated by Composer
|
||||
|
||||
$vendorDir = dirname(__DIR__);
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
'ae18b6324ebdb023b82dfb6b6ec743e6' => $vendorDir . '/rocklobsterinc/functions/src/functions.php',
|
||||
'a88a339f0bb39de956f257ef3d7d5643' => $vendorDir . '/rocklobsterinc/form-data-tree/src/functions.php',
|
||||
'8e20d93be991ae9cd79bba6774de3000' => $vendorDir . '/rocklobsterinc/swv/src/functions.php',
|
||||
);
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
// autoload_namespaces.php @generated by Composer
|
||||
|
||||
$vendorDir = dirname(__DIR__);
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
);
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
// autoload_psr4.php @generated by Composer
|
||||
|
||||
$vendorDir = dirname(__DIR__);
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
'RockLobsterInc\\Swv\\' => array($vendorDir . '/rocklobsterinc/swv/src'),
|
||||
'RockLobsterInc\\FormDataTree\\' => array($vendorDir . '/rocklobsterinc/form-data-tree/src'),
|
||||
);
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitf850b4a531317914b9045752ab7ba3f8
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
public static function loadClassLoader($class)
|
||||
{
|
||||
if ('Composer\Autoload\ClassLoader' === $class) {
|
||||
require __DIR__ . '/ClassLoader.php';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Composer\Autoload\ClassLoader
|
||||
*/
|
||||
public static function getLoader()
|
||||
{
|
||||
if (null !== self::$loader) {
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
require __DIR__ . '/platform_check.php';
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitf850b4a531317914b9045752ab7ba3f8', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitf850b4a531317914b9045752ab7ba3f8', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitf850b4a531317914b9045752ab7ba3f8::getInitializer($loader));
|
||||
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitf850b4a531317914b9045752ab7ba3f8::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
||||
require $file;
|
||||
}
|
||||
}, null, null);
|
||||
foreach ($filesToLoad as $fileIdentifier => $file) {
|
||||
$requireFile($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
// autoload_static.php @generated by Composer
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitf850b4a531317914b9045752ab7ba3f8
|
||||
{
|
||||
public static $files = array (
|
||||
'ae18b6324ebdb023b82dfb6b6ec743e6' => __DIR__ . '/..' . '/rocklobsterinc/functions/src/functions.php',
|
||||
'a88a339f0bb39de956f257ef3d7d5643' => __DIR__ . '/..' . '/rocklobsterinc/form-data-tree/src/functions.php',
|
||||
'8e20d93be991ae9cd79bba6774de3000' => __DIR__ . '/..' . '/rocklobsterinc/swv/src/functions.php',
|
||||
);
|
||||
|
||||
public static $prefixLengthsPsr4 = array (
|
||||
'R' =>
|
||||
array (
|
||||
'RockLobsterInc\\Swv\\' => 19,
|
||||
'RockLobsterInc\\FormDataTree\\' => 28,
|
||||
),
|
||||
);
|
||||
|
||||
public static $prefixDirsPsr4 = array (
|
||||
'RockLobsterInc\\Swv\\' =>
|
||||
array (
|
||||
0 => __DIR__ . '/..' . '/rocklobsterinc/swv/src',
|
||||
),
|
||||
'RockLobsterInc\\FormDataTree\\' =>
|
||||
array (
|
||||
0 => __DIR__ . '/..' . '/rocklobsterinc/form-data-tree/src',
|
||||
),
|
||||
);
|
||||
|
||||
public static $classMap = array (
|
||||
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
|
||||
);
|
||||
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitf850b4a531317914b9045752ab7ba3f8::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitf850b4a531317914b9045752ab7ba3f8::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitf850b4a531317914b9045752ab7ba3f8::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
{
|
||||
"packages": [
|
||||
{
|
||||
"name": "rocklobsterinc/form-data-tree",
|
||||
"version": "v2.0.7",
|
||||
"version_normalized": "2.0.7.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/rocklobster-in/form-data-tree-php.git",
|
||||
"reference": "607a95b456dd73b22fd2dfdd79b9477e2a662ae8"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/rocklobster-in/form-data-tree-php/zipball/607a95b456dd73b22fd2dfdd79b9477e2a662ae8",
|
||||
"reference": "607a95b456dd73b22fd2dfdd79b9477e2a662ae8",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.3",
|
||||
"rocklobsterinc/functions": "^v1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"php-parallel-lint/php-parallel-lint": "^1.4"
|
||||
},
|
||||
"time": "2026-08-14T07:18:32+00:00",
|
||||
"type": "library",
|
||||
"installation-source": "dist",
|
||||
"autoload": {
|
||||
"files": [
|
||||
"src/functions.php"
|
||||
],
|
||||
"psr-4": {
|
||||
"RockLobsterInc\\FormDataTree\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Takayuki Miyoshi",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"description": "FormDataTree for PHP",
|
||||
"support": {
|
||||
"issues": "https://github.com/rocklobster-in/form-data-tree-php/issues",
|
||||
"source": "https://github.com/rocklobster-in/form-data-tree-php/tree/v2.0.7"
|
||||
},
|
||||
"install-path": "../rocklobsterinc/form-data-tree"
|
||||
},
|
||||
{
|
||||
"name": "rocklobsterinc/functions",
|
||||
"version": "v1.0.6",
|
||||
"version_normalized": "1.0.6.0",
|
||||
"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"
|
||||
},
|
||||
"time": "2026-08-14T06:28:00+00:00",
|
||||
"type": "library",
|
||||
"installation-source": "dist",
|
||||
"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"
|
||||
},
|
||||
"install-path": "../rocklobsterinc/functions"
|
||||
},
|
||||
{
|
||||
"name": "rocklobsterinc/swv",
|
||||
"version": "v2.0.7",
|
||||
"version_normalized": "2.0.7.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/rocklobster-in/swv-php.git",
|
||||
"reference": "84382c12cc9630af2f6f0218c2aea2ffde32b605"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/rocklobster-in/swv-php/zipball/84382c12cc9630af2f6f0218c2aea2ffde32b605",
|
||||
"reference": "84382c12cc9630af2f6f0218c2aea2ffde32b605",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.3",
|
||||
"rocklobsterinc/form-data-tree": "^v2.0",
|
||||
"rocklobsterinc/functions": "^v1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"php-parallel-lint/php-parallel-lint": "^1.4",
|
||||
"phpunit/phpunit": "^12.5"
|
||||
},
|
||||
"time": "2026-08-14T08:23:16+00:00",
|
||||
"type": "library",
|
||||
"installation-source": "dist",
|
||||
"autoload": {
|
||||
"files": [
|
||||
"src/functions.php"
|
||||
],
|
||||
"psr-4": {
|
||||
"RockLobsterInc\\Swv\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Takayuki Miyoshi",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"description": "Schema-Woven Validation for PHP",
|
||||
"support": {
|
||||
"issues": "https://github.com/rocklobster-in/swv-php/issues",
|
||||
"source": "https://github.com/rocklobster-in/swv-php/tree/v2.0.7"
|
||||
},
|
||||
"install-path": "../rocklobsterinc/swv"
|
||||
}
|
||||
],
|
||||
"dev": true,
|
||||
"dev-package-names": []
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php return array(
|
||||
'root' => array(
|
||||
'name' => '__root__',
|
||||
'pretty_version' => 'dev-trunk',
|
||||
'version' => 'dev-trunk',
|
||||
'reference' => '2a5b1397127b6011e867143a5b9f979b527dab3a',
|
||||
'type' => 'library',
|
||||
'install_path' => __DIR__ . '/../../',
|
||||
'aliases' => array(),
|
||||
'dev' => true,
|
||||
),
|
||||
'versions' => array(
|
||||
'__root__' => array(
|
||||
'pretty_version' => 'dev-trunk',
|
||||
'version' => 'dev-trunk',
|
||||
'reference' => '2a5b1397127b6011e867143a5b9f979b527dab3a',
|
||||
'type' => 'library',
|
||||
'install_path' => __DIR__ . '/../../',
|
||||
'aliases' => array(),
|
||||
'dev_requirement' => false,
|
||||
),
|
||||
'rocklobsterinc/form-data-tree' => array(
|
||||
'pretty_version' => 'v2.0.7',
|
||||
'version' => '2.0.7.0',
|
||||
'reference' => '607a95b456dd73b22fd2dfdd79b9477e2a662ae8',
|
||||
'type' => 'library',
|
||||
'install_path' => __DIR__ . '/../rocklobsterinc/form-data-tree',
|
||||
'aliases' => array(),
|
||||
'dev_requirement' => false,
|
||||
),
|
||||
'rocklobsterinc/functions' => array(
|
||||
'pretty_version' => 'v1.0.6',
|
||||
'version' => '1.0.6.0',
|
||||
'reference' => '5390f54a2cbcadadc373ca069f46c7e02fe36fc6',
|
||||
'type' => 'library',
|
||||
'install_path' => __DIR__ . '/../rocklobsterinc/functions',
|
||||
'aliases' => array(),
|
||||
'dev_requirement' => false,
|
||||
),
|
||||
'rocklobsterinc/swv' => array(
|
||||
'pretty_version' => 'v2.0.7',
|
||||
'version' => '2.0.7.0',
|
||||
'reference' => '84382c12cc9630af2f6f0218c2aea2ffde32b605',
|
||||
'type' => 'library',
|
||||
'install_path' => __DIR__ . '/../rocklobsterinc/swv',
|
||||
'aliases' => array(),
|
||||
'dev_requirement' => false,
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
// platform_check.php @generated by Composer
|
||||
|
||||
$issues = array();
|
||||
|
||||
if (!(PHP_VERSION_ID >= 80300)) {
|
||||
$issues[] = 'Your Composer dependencies require a PHP version ">= 8.3.0". You are running ' . PHP_VERSION . '.';
|
||||
}
|
||||
|
||||
if ($issues) {
|
||||
if (!headers_sent()) {
|
||||
header('HTTP/1.1 500 Internal Server Error');
|
||||
}
|
||||
if (!ini_get('display_errors')) {
|
||||
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
|
||||
fwrite(STDERR, 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . implode(PHP_EOL, $issues) . PHP_EOL.PHP_EOL);
|
||||
} elseif (!headers_sent()) {
|
||||
echo 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . str_replace('You are running '.PHP_VERSION.'.', '', implode(PHP_EOL, $issues)) . PHP_EOL.PHP_EOL;
|
||||
}
|
||||
}
|
||||
throw new \RuntimeException(
|
||||
'Composer detected issues in your platform: ' . implode(' ', $issues)
|
||||
);
|
||||
}
|
||||
+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