Skip to content

Commit b5e8ae7

Browse files
committed
Merge branch 'develop'
2 parents 0f52b43 + 8175733 commit b5e8ae7

File tree

3 files changed

+201
-64
lines changed

3 files changed

+201
-64
lines changed

Inp.php

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ public function equalLength(int $arg1): bool
412412
*/
413413
public function equal($str): bool
414414
{
415-
return ((string)$this->value === (string)$str);
415+
return ($this->value === $str);
416416
}
417417

418418
/**
@@ -422,18 +422,19 @@ public function equal($str): bool
422422
*/
423423
public function notEqual($str): bool
424424
{
425-
return ((string)$this->value !== (string)$str);
425+
return ($this->value !== $str);
426426
}
427427

428428
/**
429429
* Check is a valid version number
430-
* @param bool $strict
430+
* @param bool $strict (validate as a semantic Versioning, e.g. 1.0.0)
431431
* @return bool
432432
*/
433433
public function validVersion(bool $strict = false): bool
434434
{
435435
$strictMatch = (!$strict || preg_match("/^(\d?\d)\.(\d?\d)\.(\d?\d)$/", (string)$this->value));
436-
return ($strictMatch && version_compare((string)$this->value, '0.0.1', '>=') >= 0);
436+
$compare = version_compare((string)$this->value, '0.0.1', '>=');
437+
return ($strictMatch && $compare !== false && $compare >= 0);
437438
}
438439

439440
/**
@@ -442,10 +443,10 @@ public function validVersion(bool $strict = false): bool
442443
* @param string $operator '!='|'<'|'<='|'<>'|'='|'=='|'>'|'>='|'eq'|'ge'|'gt'|'le'|'lt'|'ne'
443444
* @return bool
444445
*/
445-
public function versionCompare(string $withVersion, string $operator = ">="): bool
446+
public function versionCompare(string $withVersion, string $operator = "=="): bool
446447
{
447448
if (in_array($operator, self::WHITELIST_OPERATORS)) {
448-
return (version_compare((string)$this->value, $withVersion, $operator) >= 0);
449+
return version_compare((string)$this->value, $withVersion, $operator);
449450
}
450451
return false;
451452
}
@@ -530,30 +531,30 @@ public function hex(): bool
530531
/**
531532
* Check if is a date
532533
* @param string $format validate after this date format (default Y-m-d)
533-
* @return DateTime|false
534+
* @return bool
534535
*/
535-
public function date(string $format = "Y-m-d"): DateTime|false
536+
public function date(string $format = "Y-m-d"): bool
536537
{
537-
return DateTime::createFromFormat($format, $this->value);
538+
return (DateTime::createFromFormat($format, $this->value) !== false);
538539
}
539540

540541

541542
/**
542543
* Check if is a date and time
543544
* @param string $format validate after this date format (default Y-m-d H:i)
544-
* @return DateTime|false
545+
* @return bool
545546
*/
546-
public function dateTime(string $format = "Y-m-d H:i"): DateTime|false
547+
public function dateTime(string $format = "Y-m-d H:i"): bool
547548
{
548549
return $this->date($format);
549550
}
550551

551552
/**
552553
* Check if is a date and time
553554
* @param string $format validate after this date format (default Y-m-d H:i)
554-
* @return DateTime|false
555+
* @return bool
555556
*/
556-
public function time(string $format = "H:i"): DateTime|false
557+
public function time(string $format = "H:i"): bool
557558
{
558559
return $this->date($format);
559560
}
@@ -579,7 +580,7 @@ public function dateRange(string $format = "Y-m-d H:i"): array|false
579580

580581
/**
581582
* Check "minimum" age (value format should be validated date "Y-m-d")
582-
* @param int $arg1 18 == user should be at least 18 years old
583+
* @param int $arg1 18: user should be 18 or older
583584
* @return bool
584585
* @throws Exception
585586
*/
@@ -589,7 +590,7 @@ public function age(int $arg1): bool
589590
$dateTime = new DateTime($this->value);
590591
$birth = $dateTime->format("Y");
591592
$age = (int)($now - $birth);
592-
return ($age >= $arg1);
593+
return ($age <= $arg1);
593594
}
594595

595596
/**
@@ -669,15 +670,10 @@ private function getHost(string $host): string
669670
public function oneOf(array $arr): bool
670671
{
671672
$valid = false;
672-
foreach ($arr as $val) {
673-
if (is_array($val)) {
674-
if (call_user_func_array(['self', 'length'], $val)) {
675-
$valid = true;
676-
}
677-
} else {
678-
if ($this->{$val}()) {
679-
$valid = true;
680-
}
673+
foreach ($arr as $method => $args) {
674+
$inst = new self($this->value);
675+
if(call_user_func_array([$inst, $method], $args)) {
676+
$valid = true;
681677
}
682678
}
683679
return $valid;
@@ -690,30 +686,12 @@ public function oneOf(array $arr): bool
690686
*/
691687
public function allOf(array $arr): bool
692688
{
693-
$valid = true;
694-
foreach ($arr as $val) {
695-
if (is_array($val)) {
696-
if (!call_user_func_array(['self', 'length'], $val)) {
697-
$valid = false;
698-
}
699-
} else {
700-
if (!$this->{$val}()) {
701-
$valid = false;
702-
}
689+
foreach ($arr as $method => $args) {
690+
$inst = new self($this->value);
691+
if(!call_user_func_array([$inst, $method], $args)) {
692+
return false;
703693
}
704694
}
705-
return $valid;
695+
return true;
706696
}
707-
708-
public function continue(array $arr1, array $arr2): bool
709-
{
710-
if ($this->allOf($arr1)) {
711-
if (!$this->required()) {
712-
return true;
713-
}
714-
return $this->allOf($arr2);
715-
}
716-
return false;
717-
}
718-
719697
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "maplephp/validate",
33
"type": "library",
4-
"version": "v1.0.5",
4+
"version": "v1.0.6",
55
"description": "User-friendly input validation library.",
66
"keywords": [
77
"validation",

tests/unitary-test.php

Lines changed: 175 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* when used in MaplePHP framework you can skip the "bash code" at top and the "autoload file"!
66
*/
77
use MaplePHP\Unitary\Unit;
8+
use MaplePHP\Validate\Inp;
9+
10+
811

912

1013
// If you add true to Unit it will run in quite mode
@@ -14,29 +17,185 @@
1417
// Add a title to your tests (not required)
1518
$unit->addTitle("Testing MaplePHP validation library!");
1619

17-
$unit->add("Validating values", function($inst) {
20+
$unit->add("Validating values", callback: function($inst) {
21+
22+
$strVal = Inp::value("TestStringValue");
23+
$testStrValidates = ["isString", "required", "hasValue"];
24+
25+
foreach ($testStrValidates as $validate) {
26+
$inst->add($strVal->{$validate}(), [
27+
"equal" => [true],
28+
], "Expect {$validate} to be true");
29+
}
30+
31+
$inst->add(Inp::value("8808218329")->socialNumber(), [
32+
"equal" => [false],
33+
], "Expect socialNumber to be false");
34+
35+
36+
$inst->add(Inp::value("4030000010001234")->creditCard(), [
37+
"equal" => [true],
38+
], "Expect creditCard to be true");
39+
40+
$inst->add(Inp::value("john.doe-gmail.com")->email(), [
41+
"equal" => [false],
42+
], "Expect creditCard to be false");
43+
44+
$inst->add(Inp::value("Hello world!")->findInString("world"), [
45+
"equal" => [true],
46+
], "Expect findInString to be true");
47+
48+
$inst->add(Inp::value("+46 (0) 702-83 27 12")->phone(), [
49+
"equal" => [true],
50+
], "Expect phone to be true");
51+
52+
$inst->add(Inp::value("252522")->zip(5), [
53+
"equal" => [true],
54+
], "Expect zip to be true");
55+
56+
$testDataTypeValidations = ['isString', 'isInt', 'isFloat', 'isArray', 'isObject', 'isBool'];
57+
$inst->add(Inp::value("Is string")->isString(), [
58+
"equal" => [true],
59+
], "Expect isString to be true");
60+
61+
$inst->add(Inp::value(122)->isInt(), [
62+
"equal" => [true],
63+
], "Expect isInt to be true");
64+
65+
$inst->add(Inp::value(22.12)->isFloat(), [
66+
"equal" => [true],
67+
], "Expect isFloat to be true");
68+
69+
$inst->add(Inp::value([1, 2, 3])->isArray(), [
70+
"equal" => [true],
71+
], "Expect isArray to be true");
72+
73+
$inst->add(Inp::value(new stdClass())->isObject(), [
74+
"equal" => [true],
75+
], "Expect isObject to be true");
76+
77+
$inst->add(Inp::value(false)->isBool(), [
78+
"equal" => [true],
79+
], "Expect isBool to be true");
80+
81+
$inst->add(Inp::value("222.33")->number(), [
82+
"equal" => [true],
83+
], "Expect number to be true");
84+
85+
$inst->add(Inp::value(100)->positive(), [
86+
"equal" => [true],
87+
], "Expect positive to be true");
88+
89+
$inst->add(Inp::value(-100)->negative(), [
90+
"equal" => [true],
91+
], "Expect negative to be true");
92+
93+
$inst->add(Inp::value(10)->min(10), [
94+
"equal" => [true],
95+
], "Expect min to be true");
96+
97+
$inst->add(Inp::value(10)->max(10), [
98+
"equal" => [true],
99+
], "Expect max to be true");
18100

19-
$inst->add("TestValue", [
101+
$inst->add(Inp::value("Lorem ipsum")->length(1, 11), [
102+
"equal" => [true],
103+
], "Expect length to be true");
104+
105+
$inst->add(Inp::value("22222")->equalLength(5), [
106+
"equal" => [true],
107+
], "Expect equalLength to be true");
108+
109+
$inst->add(Inp::value("hello")->equal("hello"), [
110+
"equal" => [true],
111+
], "Expect equal to be true");
112+
113+
$inst->add(Inp::value("world")->notEqual("hello"), [
114+
"equal" => [true],
115+
], "Expect notEqual to be true");
116+
117+
$inst->add(Inp::value("1.2.3")->validVersion(true), [
118+
"equal" => [true],
119+
], "Expect validVersion to be true");
120+
121+
$inst->add(Inp::value("1.2.0")->versionCompare("1.2.0"), [
122+
"equal" => [true],
123+
], "Expect versionCompare to be true");
124+
125+
$inst->add(Inp::value("MyStrongPass")->lossyPassword(), [
126+
"equal" => [true],
127+
], "Expect lossyPassword to be true");
128+
129+
$inst->add(Inp::value("My@StrongPass12")->strictPassword(), [
130+
"equal" => [true],
131+
], "Expect strictPassword to be true");
132+
133+
$inst->add(Inp::value("HelloWorld")->atoZ(), [
134+
"equal" => [true],
135+
], "Expect atoZ to be true");
136+
137+
$inst->add(Inp::value("welloworld")->lowerAtoZ(), [
138+
"equal" => [true],
139+
], "Expect lowerAtoZ to be true");
140+
141+
$inst->add(Inp::value("HELLOWORLD")->upperAtoZ(), [
142+
"equal" => [true],
143+
], "Expect upperAtoZ to be true");
144+
145+
$inst->add(Inp::value("#F1F1F1")->hex(), [
146+
"equal" => [true],
147+
], "Expect hex to be true");
148+
149+
$inst->add(Inp::value("1922-03-01")->date(), [
150+
"equal" => [true],
151+
], "Expect date to be true");
152+
153+
$inst->add(Inp::value("1988-08-21")->age(36), [
154+
"equal" => [true],
155+
], "Expect age to be true");
156+
157+
$inst->add(Inp::value("example.se")->domain(), [
158+
"equal" => [true],
159+
], "Expect domain to be true");
160+
161+
$inst->add(Inp::value("https://example.se")->url(), [
162+
"equal" => [true],
163+
], "Expect url to be true");
164+
165+
$inst->add(Inp::value("examplethatwillfail.se")->dns(), [
166+
"equal" => [false],
167+
], "Expect dns to be false");
168+
169+
$inst->add(Inp::value("Lorem ipsum")->oneOf([
170+
"length" => [120, 200],
20171
"isString" => []
21-
], "Is not a string");
172+
]), [
173+
"equal" => [true],
174+
], "Expect oneOf to be true");
22175

23-
$inst->add("600", [
24-
"isInt" => []
25-
], "Is not int");
176+
$inst->add(Inp::value("Lorem ipsum")->allOf([
177+
"length" => [1, 200],
178+
"isString" => []
179+
]), [
180+
"equal" => [true],
181+
], "Expect allOf to be true");
26182

27-
$inst->add("600.33", [
28-
"isFloat" => []
29-
], "Is not float");
183+
$inst->add(Inp::value("required")->required(), [
184+
"equal" => [true],
185+
], "Expect required to be true");
30186

31-
$inst->add(true, [
32-
"isBool" => []
33-
], "Is not bool");
187+
$inst->add(Inp::value("required")->required(), [
188+
"equal" => [true],
189+
], "Expect required to be true");
34190

35-
$inst->add("yes", [
36-
"isBoolVal" => []
37-
], "Is not bool");
191+
$inst->add(Inp::value("required")->required(), [
192+
"equal" => [true],
193+
], "Expect required to be true");
194+
195+
$inst->add(Inp::value("required")->required(), [
196+
"equal" => [true],
197+
], "Expect required to be true");
38198

39199
});
40200

41201
$unit->execute();
42-

0 commit comments

Comments
 (0)