Select Git revision
projectlist.class.php
validateparam.class.php 6.89 KiB
<?php
class validateparam
{
/**
* Summary of _aValidationDefs
* @var array
*/
protected array $_aValidationDefs = [];
protected bool $_flag_debug = false;
/**
* Write debug line if _flag_debug is set to true
* @param string $s message to show
* @return void
*/
protected function _wd(string $s)
{
if ($this->_flag_debug) {
echo "DEBUG: $s<br>\n";
}
}
/**
* Include the default validation rules defined in validateparam.settings.php
*/
public function __construct(){
// IMPORTANT:
// This style is not so nice like <variable> = include <file>;
// keep the include line unchanged - it is detected by installer
// CLI client project (amcli)
include 'validateparam.settings.php';
}
/**
* shared validaten checks for floor and integer
*
* @param array $aOpt Validation rules
* @param mixed $value value to verify
* @return string with found error
*/
protected function _checkCount(array $aOpt, mixed $value)
{
$sError = "";
if ($aOpt['min'] ?? false) {
if ($value < $aOpt['min']) {
$sError .= "Value is too small; minimum is $aOpt[min]. ";
}
}
if ($aOpt['max'] ?? false) {
if ($value > $aOpt['max']) {
$sError .= "Value is too big; maximum is $aOpt[max]. ";
}
}
if (isset($aOpt['oneof']) && is_array($aOpt['oneof'])) {
if (array_search($value, $aOpt['oneof']) == false) {
$sError .= "Value is invalid. Value doesn't match one of these values " . print_r($aOpt['oneof']);
}
}
return $sError;
}
/**
* Validate a single value. It returns a string with an errormessage.
* No error means ok.
*