Custom validator created with some functions
This commit is contained in:
parent
e2c3372ab1
commit
2e33469987
1 changed files with 160 additions and 0 deletions
160
src/validator.php
Normal file
160
src/validator.php
Normal file
|
@ -0,0 +1,160 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
function fieldExistsInData($data, $key)
|
||||||
|
{
|
||||||
|
$keys = explode('.', $key);
|
||||||
|
foreach ($keys as $k) {
|
||||||
|
if (is_array($data) && array_key_exists($k, $data)) {
|
||||||
|
$data = $data[$k];
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDataValue($data, $key)
|
||||||
|
{
|
||||||
|
$keys = explode('.', $key);
|
||||||
|
|
||||||
|
foreach ($keys as $k) {
|
||||||
|
if (is_array($data) && array_key_exists($k, $data)) {
|
||||||
|
$data = $data[$k];
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validate($data, $rules)
|
||||||
|
{
|
||||||
|
$errors = [];
|
||||||
|
|
||||||
|
foreach ($rules as $field => $ruleString) {
|
||||||
|
$value = getDataValue($data, $field);
|
||||||
|
$ruleList = explode('|', $ruleString);
|
||||||
|
|
||||||
|
$hasSometimes = in_array('sometimes', $ruleList, true);
|
||||||
|
if ($hasSometimes && !fieldExistsInData($data, $field)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($ruleList as $ruleItem) {
|
||||||
|
if ($ruleItem === 'sometimes') {
|
||||||
|
continue; // already handled
|
||||||
|
}
|
||||||
|
|
||||||
|
$param = null;
|
||||||
|
if (strpos($ruleItem, ':') !== false) {
|
||||||
|
$exploded = explode(':', $ruleItem, 2);
|
||||||
|
$rule = $exploded[0];
|
||||||
|
$param = isset($exploded[1]) ? $exploded[1] : null;
|
||||||
|
} else {
|
||||||
|
$rule = $ruleItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
$validator = 'validate_' . $rule;
|
||||||
|
|
||||||
|
if (function_exists($validator)) {
|
||||||
|
$result = $validator($field, $value, $param, $data);
|
||||||
|
|
||||||
|
if ($result !== true) {
|
||||||
|
$errors[$field][] = $result;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$errors[$field][] = "Rule '$rule' is not supported.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ['valid' => !empty($errors) ? false : true, 'errors' => $errors];
|
||||||
|
}
|
||||||
|
|
||||||
|
function validate_array($field, $value, $param = null, $data = [])
|
||||||
|
{
|
||||||
|
if (!is_array($value)) {
|
||||||
|
return "The field '$field' must be an array.";
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validate_integer($field, $value, $param = null, $data = [])
|
||||||
|
{
|
||||||
|
if (!is_int($value)) {
|
||||||
|
return "The field '$field' must be an integer.";
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validate_max($field, $value, $param, $data = [])
|
||||||
|
{
|
||||||
|
if (is_string($value) && strlen($value) > $param) {
|
||||||
|
return "The field '$field' must have at most $param characters.";
|
||||||
|
}
|
||||||
|
if (is_integer($value) && $value > $param) {
|
||||||
|
return "The value of '$field' must be at most $param.";
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validate_min($field, $value, $param, $data = [])
|
||||||
|
{
|
||||||
|
if (is_string($value) && strlen($value) < $param) {
|
||||||
|
return "The field '$field' must have at least $param characters.";
|
||||||
|
}
|
||||||
|
if (is_numeric($value) && $value < $param) {
|
||||||
|
return "The value of '$field' must be at least $param.";
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validate_prohibited_with($field, $value, $param, $data = [])
|
||||||
|
{
|
||||||
|
$otherFields = explode(',', $param);
|
||||||
|
|
||||||
|
foreach ($otherFields as $other) {
|
||||||
|
$other = trim($other);
|
||||||
|
if (!empty($data[$other])) {
|
||||||
|
if (!empty($value)) {
|
||||||
|
return "The field '$field' is not allowed when '$other' is present.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validate_required($field, $value, $param = null, $data = [])
|
||||||
|
{
|
||||||
|
if ($value === null || $value === '') {
|
||||||
|
return "The field '$field' is required.";
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validate_required_with($field, $value, $param, $data = [])
|
||||||
|
{
|
||||||
|
$relatedFields = explode(',', $param);
|
||||||
|
|
||||||
|
foreach ($relatedFields as $related) {
|
||||||
|
$related = trim($related);
|
||||||
|
|
||||||
|
if (isset($data[$related]) && $data[$related] !== '') {
|
||||||
|
if ($value === null || $value === '') {
|
||||||
|
return "The field '$field' is required when '$related' is present.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validate_string($field, $value, $param = null, $data = [])
|
||||||
|
{
|
||||||
|
if (!is_string($value)) {
|
||||||
|
return "The field '$field' must be a string.";
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue