<?php
namespace CodeIgniter\Validation;
use Config\Database;
class Rules
{
public function differs(string $str = null, string $field, array $data): bool
{
return array_key_exists($field, $data) ? ($str !== $data[$field]) : false;
}
public function equals(string $str = null, string $val): bool
{
return $str === $val;
}
public function exact_length(string $str = null, string $val, array $data): bool
{
$val = explode(',', $val);
foreach ($val as $tmp)
{
if (is_numeric($tmp) && (int)$tmp === mb_strlen($str))
{
return true;
}
}
return false;
}
public function greater_than(string $str = null, string $min, array $data): bool
{
return is_numeric($str) ? ($str > $min) : false;
}
public function greater_than_equal_to(string $str = null, string $min, array $data): bool
{
return is_numeric($str) ? ($str >= $min) : false;
}
public function in_list(string $value = null, string $list, array $data): bool
{
$list = explode(',', $list);
$list = array_map(function ($value) {
return trim($value);
}, $list);
return in_array($value, $list, true);
}
public function is_unique(string $str = null, string $field, array $data): bool
{
list($field, $ignoreField, $ignoreValue) = array_pad(explode(',', $field), 3, null);
sscanf($field, '%[^.].%[^.]', $table, $field);
$db = Database::connect($data['DBGroup'] ?? null);
$row = $db->table($table)
->select('1')
->where($field, $str)
->limit(1);
if (! empty($ignoreField) && ! empty($ignoreValue))
{
$row = $row->where("{$ignoreField} !=", $ignoreValue);
}
return (bool) ($row->get()
->getRow() === null);
}
public function less_than(string $str = null, string $max): bool
{
return is_numeric($str) ? ($str < $max) : false;
}
public function less_than_equal_to(string $str = null, string $max): bool
{
return is_numeric($str) ? ($str <= $max) : false;
}
public function matches(string $str = null, string $field, array $data): bool
{
return array_key_exists($field, $data) ? ($str === $data[$field]) : false;
}
public function max_length(string $str = null, string $val, array $data): bool
{
return ($val >= mb_strlen($str));
}
public function min_length(string $str = null, string $val, array $data): bool
{
return ($val <= mb_strlen($str));
}
public function not_equals(string $str = null, string $val): bool
{
return $str !== $val;
}
public function required($str = null): bool
{
if (is_object($str))
{
return true;
}
return is_array($str) ? ! empty($str) : (trim($str) !== '');
}
public function required_with($str = null, string $fields, array $data): bool
{
$fields = explode(',', $fields);
$present = $this->required($str ?? '');
if ($present)
{
return true;
}
$requiredFields = [];
foreach ($fields as $field)
{
if (array_key_exists($field, $data))
{
$requiredFields[] = $field;
}
}
$requiredFields = array_filter($requiredFields, function ($item) use ($data) {
return ! empty($data[$item]);
});
return empty($requiredFields);
}
public function required_without($str = null, string $fields, array $data): bool
{
$fields = explode(',', $fields);
$present = $this->required($str ?? '');
if ($present)
{
return true;
}
foreach ($fields as $field)
{
if (! array_key_exists($field, $data))
{
return false;
}
}
return true;
}
}