Skip to content

Commit ae1e420

Browse files
new release
1 parent 029a5a2 commit ae1e420

13 files changed

+950
-432
lines changed

composer.json

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
{
22
"name": "peterson/ultimate-validator",
3-
"description": "PHP Library for easy form validation",
4-
"keywords": ["php-form-validator", "form-validator", "form", "php-form", "php", "forms", "easy-validation", "easy-form"],
53
"type": "library",
4+
"description": "PHP Form Validator is a small no-depencies library for php form validation.",
5+
"keywords": ["php-form-validator", "form-validator", "form", "php-form", "php", "forms", "easy validation", "easy-form"],
6+
"homepage": "https://www.tamedevelopers/ultimate-validator",
67
"license": "MIT",
78
"authors": [
8-
{
9-
"name": "Fredrick Peterson",
10-
"email": "[email protected]"
11-
}
9+
{"name": "Fredrick Peterson", "email": "[email protected]"},
10+
{"name": "Tame Developers", "email": "[email protected]"}
1211
],
1312
"require": {
14-
"php": ">=5.3.3"
13+
"php": ">=5.5"
1514
},
1615
"autoload": {
1716
"psr-4": {
1817
"UltimateValidator\\": "src/"
18+
},
19+
"classmap": ["src/*"]
20+
},
21+
"autoload-dev": {
22+
"psr-4": {
23+
"UltimateValidator\\": "test/"
1924
}
2025
},
2126
"extra": {
2227
"branch-alias": {
23-
"dev-master": "1.0-dev"
28+
"dev-main": "1.0-dev"
2429
}
2530
},
2631
"minimum-stability": "dev"

src/Methods/CheckDatatype.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of ultimate-validator.
7+
*
8+
* (c) Tame Developers Inc.
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace UltimateValidator;
15+
16+
/**
17+
* CheckDatatype
18+
*
19+
* @package Ultimate\Validator
20+
* @author Tame Developers <[email protected]>
21+
* @copyright 2021-2023 Tame Developers
22+
*/
23+
class CheckDatatype {
24+
25+
/**
26+
* Private instance of parent validator
27+
*
28+
* @var object\object
29+
*/
30+
private static $object;
31+
32+
33+
/**
34+
* Check if form data is set
35+
* And if data type is correct
36+
*
37+
* @param array $data_flags\[data_type|variable|operator|value]
38+
*
39+
* !isset if param is not set
40+
* false if expected data type is not correct
41+
* true|data on success
42+
*
43+
* @return array|string|boolean\check
44+
*/
45+
public static function check(?array $data_flags)
46+
{
47+
// parent object
48+
self::$object = UltimateMethods::$object;
49+
50+
// if input parameter is isset -- proceed to error validating
51+
$type = true;
52+
53+
// check if in array
54+
55+
if(UltimateMethods::checkIfParamIsset($data_flags['variable'])){
56+
switch($data_flags['data_type']){
57+
58+
// email validation
59+
case (in_array($data_flags['data_type'], ['email', 'e'])):
60+
$type = filter_input(self::$object->type, $data_flags['variable'], FILTER_VALIDATE_EMAIL);
61+
break;
62+
63+
// integer validation
64+
case (in_array($data_flags['data_type'], ['int', 'i'])):
65+
$type = filter_input(self::$object->type, $data_flags['variable'], FILTER_VALIDATE_INT);
66+
break;
67+
68+
// float validation
69+
case (in_array($data_flags['data_type'], ['float', 'f'])):
70+
$type = filter_input(self::$object->type, $data_flags['variable'], FILTER_VALIDATE_FLOAT);
71+
break;
72+
73+
// url validation
74+
case (in_array($data_flags['data_type'], ['url', 'u'])):
75+
$type = filter_input(self::$object->type, $data_flags['variable'], FILTER_VALIDATE_URL);
76+
break;
77+
78+
// array validation
79+
case (in_array($data_flags['data_type'], ['array', 'a'])):
80+
if(is_string(self::$object->param[$data_flags['variable']])){
81+
$array = json_decode(self::$object->param[$data_flags['variable']], TRUE);
82+
}else{
83+
$array = self::$object->param[$data_flags['variable']];
84+
}
85+
$type = isset(self::$object->param[$data_flags['variable']]) && is_array($array) && count($array) > 0 ? true : false;
86+
break;
87+
88+
// bool|boolean validation
89+
case (in_array($data_flags['data_type'], ['bool', 'b'])):
90+
$type = filter_input(self::$object->type, $data_flags['variable'], FILTER_VALIDATE_BOOLEAN);
91+
break;
92+
93+
// string validation
94+
default:
95+
$type = htmlspecialchars(filter_input(self::$object->type, $data_flags['variable']), ENT_HTML5);
96+
// mostly for value of 0
97+
if(empty($type) && $type != '0') {
98+
$type = false;
99+
}
100+
break;
101+
}
102+
}
103+
104+
// if data passes is not in form elements
105+
elseif(!in_array($data_flags['variable'], array_keys(self::$object->param))){
106+
$type = '!found';
107+
}
108+
109+
// else if not isset
110+
else{
111+
$type = '!isset';
112+
}
113+
114+
return $type;
115+
}
116+
117+
}

src/Methods/CheckOperator.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of ultimate-validator.
7+
*
8+
* (c) Tame Developers Inc.
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace UltimateValidator;
15+
16+
/**
17+
* CheckOperator
18+
*
19+
* @package Ultimate\Validator
20+
* @author Tame Developers <[email protected]>
21+
* @copyright 2021-2023 Tame Developers
22+
*/
23+
class CheckOperator {
24+
25+
26+
/**
27+
* Checking for flag type error
28+
* Returns true on error found and false is no error is found
29+
*
30+
* @param array\flag $flag Keypairs.
31+
* @param object\object $object of parent class ($this).
32+
*
33+
* @return boolean\ false|true
34+
*/
35+
public static function check($dataType = [], UltimateValidator $object)
36+
{
37+
$object->operator = false;
38+
$flagOperator = $dataType['operator'];
39+
$flagValue = $dataType['value'];
40+
41+
if(UltimateMethods::checkIfParamIsset($dataType['variable'])){
42+
// equal to operator
43+
if($flagOperator == '=')
44+
{
45+
$dataString = $object->param[$dataType['variable']];
46+
if($dataString == $flagValue){
47+
$object->operator = true;
48+
}
49+
}
50+
51+
// strictly equal to operator
52+
elseif($flagOperator == '==')
53+
{
54+
$dataString = $object->param[$dataType['variable']];
55+
if($dataString === $flagValue){
56+
$object->operator = true;
57+
}
58+
}
59+
60+
// not equal to operator
61+
elseif($flagOperator == '!=')
62+
{
63+
$dataString = $object->param[$dataType['variable']];
64+
if($dataString != $flagValue){
65+
$object->operator = true;
66+
}
67+
}
68+
69+
// strictly not equal to operator
70+
elseif($flagOperator == '!==')
71+
{
72+
$dataString = $object->param[$dataType['variable']];
73+
if($dataString !== $flagValue){
74+
$object->operator = true;
75+
}
76+
}
77+
78+
// greater than operator
79+
elseif($flagOperator == '>')
80+
{
81+
$dataString = $object->param[$dataType['variable']];
82+
// if str_len | sl
83+
if(in_array($dataType['data_type'], ['str_len', 'sl'])){
84+
$dataString = strlen($dataString);
85+
if($dataString > (float) $flagValue){
86+
$object->operator = true;
87+
}
88+
}else{
89+
$dataString = (float) $dataString;
90+
if($dataString > (float) $flagValue){
91+
$object->operator = true;
92+
}
93+
}
94+
}
95+
96+
// greater than or equal to operator
97+
elseif($flagOperator == '>=')
98+
{
99+
$dataString = $object->param[$dataType['variable']];
100+
// if str_len | sl
101+
if(in_array($dataType['data_type'], ['str_len', 'sl'])){
102+
$dataString = strlen($dataString);
103+
if($dataString >= (float) $flagValue){
104+
$object->operator = true;
105+
}
106+
}else{
107+
$dataString = (float) $dataString;
108+
if($dataString >= (float) $flagValue){
109+
$object->operator = true;
110+
}
111+
}
112+
}
113+
114+
// less than operator
115+
elseif($flagOperator == '<')
116+
{
117+
$dataString = $object->param[$dataType['variable']];
118+
// if str_len | sl
119+
if(in_array($dataType['data_type'], ['str_len', 'sl'])){
120+
$dataString = strlen($dataString);
121+
if($dataString < (float) $flagValue){
122+
$object->operator = true;
123+
}
124+
}else{
125+
$dataString = (float) $dataString;
126+
if($dataString < (float) $flagValue){
127+
$object->operator = true;
128+
}
129+
}
130+
}
131+
132+
// less than or equal to operator
133+
elseif($flagOperator == '<=')
134+
{
135+
$dataString = $object->param[$dataType['variable']];
136+
// if str_len | sl
137+
if(in_array($dataType['data_type'], ['str_len', 'sl'])){
138+
$dataString = strlen($dataString);
139+
if($dataString <= (float) $flagValue){
140+
$object->operator = true;
141+
}
142+
}else{
143+
$dataString = (float) $dataString;
144+
if($dataString <= (float) $flagValue){
145+
$object->operator = true;
146+
}
147+
}
148+
}
149+
150+
}
151+
152+
return $object->operator;
153+
}
154+
155+
156+
}

src/Methods/CreateDatatype.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of ultimate-validator.
7+
*
8+
* (c) Tame Developers Inc.
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace UltimateValidator;
15+
16+
/**
17+
* CreateDatatype
18+
*
19+
* @package Ultimate\Validator
20+
* @author Tame Developers <[email protected]>
21+
* @copyright 2021-2023 Tame Developers
22+
*/
23+
class CreateDatatype {
24+
25+
/**
26+
* Each form submit strings
27+
* [string:first_name:>:15 => 'First name is required']
28+
*
29+
* @param string $key string like :string::name .
30+
*
31+
* @return array datas [data_type|variable|operator|value]
32+
*/
33+
public static function create(?string $key)
34+
{
35+
// explode data
36+
$data = explode(":", $key);
37+
38+
// explode indicator
39+
// count how many occurence in string
40+
$find_occur = substr_count($key, ':');
41+
42+
// error
43+
if(count($data) > 4 || $find_occur > 3 || !isset($data[1])){
44+
return "indicator";
45+
}
46+
47+
$data['data_type'] = $data[0];
48+
$data['variable'] = $data[1];
49+
50+
//create operator
51+
if(isset($data[2])){
52+
$data['operator'] = $data[2];
53+
}
54+
if(isset($data[3])){
55+
$data['value'] = $data[3];
56+
}
57+
58+
// unset un-used keys `Key string values from array`
59+
unset($data[0]);
60+
unset($data[1]);
61+
unset($data[2]);
62+
unset($data[3]);
63+
64+
return $data;
65+
}
66+
67+
}

0 commit comments

Comments
 (0)