Skip to content

Commit 3ab29e9

Browse files
committed
Add more validations and patch for null args
1 parent e4c888c commit 3ab29e9

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

Inp.php

+34-3
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function phone(): bool
215215
* @return bool
216216
* @throws ErrorException
217217
*/
218-
public function zip(int $arg1, int $arg2 = null): bool
218+
public function zip(int $arg1, ?int $arg2 = null): bool
219219
{
220220
if (is_null($this->getStr)) {
221221
return false;
@@ -290,6 +290,37 @@ public function isBool(): bool
290290
return (is_bool($this->value));
291291
}
292292

293+
/**
294+
* Is a valid json string
295+
* @return bool
296+
*/
297+
public function isJson(): bool
298+
{
299+
json_decode($this->value);
300+
return json_last_error() === JSON_ERROR_NONE;
301+
}
302+
303+
/**
304+
* Validate a string as html, check that it contains doctype, html, head and body
305+
* @return bool
306+
*/
307+
public function isFullHtml(): bool
308+
{
309+
libxml_use_internal_errors(true);
310+
$dom = new \DOMDocument();
311+
if (!is_string($this->value) || !$dom->loadHTML($this->value, LIBXML_NOERROR | LIBXML_NOWARNING)) {
312+
return false; // Invalid HTML syntax
313+
}
314+
if (!$dom->doctype || strtolower($dom->doctype->name) !== "html") {
315+
return false;
316+
}
317+
$htmlTag = $dom->getElementsByTagName("html")->length > 0;
318+
$headTag = $dom->getElementsByTagName("head")->length > 0;
319+
$bodyTag = $dom->getElementsByTagName("body")->length > 0;
320+
return $htmlTag && $headTag && $bodyTag;
321+
}
322+
323+
293324
/**
294325
* Check if the value itself can be Interpreted as a bool value
295326
* E.g. If value === ([on, off], [yes, no], [1, 0] or [true, false])
@@ -430,7 +461,7 @@ public function max(float $int): bool
430461
* @param int|null $arg2 end length
431462
* @return bool
432463
*/
433-
public function length(int $arg1, int $arg2 = null): bool
464+
public function length(int $arg1, ?int $arg2 = null): bool
434465
{
435466
if ($this->length >= $arg1 && (($arg2 === null) || $this->length <= $arg2)) {
436467
return true;
@@ -636,7 +667,7 @@ public function age(int $arg1): bool
636667
$dateTime = new DateTime($this->value);
637668
$birth = (int)$dateTime->format("Y");
638669
$age = ($now - $birth);
639-
return ($age <= $arg1);
670+
return ($age >= $arg1);
640671
}
641672

642673
/**

composer.json

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

tests/unitary-validate-inp.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
"equal" => [true],
142142
], "Expect date to be true");
143143

144-
$this->add(Inp::value("1988-08-21")->age(36), [
144+
$this->add(Inp::value("1988-08-21")->age(18), [
145145
"equal" => [true],
146146
], "Expect age to be true");
147147

0 commit comments

Comments
 (0)