-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInp.php
executable file
·829 lines (750 loc) · 20.5 KB
/
Inp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
<?php
/**
* @Package: MaplePHP - Input validation library
* @Author: Daniel Ronkainen
* @Licence: Apache-2.0 license, Copyright © Daniel Ronkainen
Don't delete this comment, its part of the license.
*/
namespace MaplePHP\Validate;
use ErrorException;
use Exception;
use MaplePHP\DTO\MB;
use MaplePHP\Validate\Interfaces\InpInterface;
use MaplePHP\DTO\Format\Str;
use DateTime;
class Inp implements InpInterface
{
public const WHITELIST_OPERATORS = [
'!=',
'<',
'<=',
'<>',
'=',
'==',
'>',
'>=',
'eq',
'ge',
'gt',
'le',
'lt',
'ne'
];
private mixed $value;
private int $length = 0;
private DateTime $dateTime;
private ?Luhn $luhn = null;
private ?Str $getStr = null;
/**
* Start instance
* @param mixed $value the input value
* @throws ErrorException
*/
public function __construct(mixed $value)
{
$this->value = $value;
$this->dateTime = new DateTime("now");
if(is_string($value) || is_numeric($value)) {
$this->length = $this->getLength((string)$value);
$this->getStr = new Str($this->value);
}
}
/**
* Immutable: Validate against new value
* @param mixed $value
* @return InpInterface
*/
public function withValue(mixed $value): InpInterface
{
$inst = clone $this;
$inst->value = $value;
return $inst;
}
/**
* Start instance
* @param string $value the input value
* @return self
* @throws ErrorException
*/
public static function value(mixed $value): self
{
return new self($value);
}
/**
* Get value string length
* @param string $value
* @return int
* @throws ErrorException
*/
public function getLength(string $value): int
{
$mb = new MB($value);
return (int)$mb->strlen();
}
/**
* Access luhn validation class
* @return Luhn
*/
public function luhn(): Luhn
{
if (is_null($this->luhn)) {
$this->luhn = new Luhn($this->value);
}
return $this->luhn;
}
/**
* Will check if value if empty (e.g. "", 0, NULL) = false
* @return bool
*/
public function required(): bool
{
if ($this->length(1) && !empty($this->value)) {
return true;
}
return false;
}
/**
* Will only check if there is a value (e.g. 0) = true
* @return bool
*/
public function hasValue(): bool
{
return $this->length(1);
}
/**
* Validate Swedish personal numbers
* @return bool
*/
public function socialNumber(): bool
{
return $this->luhn()->personnummer();
}
/**
* Validate Swedish personal numbers
* @return bool
*/
public function personalNumber(): bool
{
return $this->socialNumber();
}
/**
* Validate Swedish org numbers
* @return bool
*/
public function orgNumber(): bool
{
return $this->luhn()->orgNumber();
}
/**
* Validate credit card numbers (THIS needs to be tested)
* @return bool
*/
public function creditCard(): bool
{
return $this->luhn()->creditcard();
}
/**
* Validate Swedish vat number
* @return bool
*/
public function vatNumber(): bool
{
return $this->luhn()->vatNumber();
}
/**
* Validate email
* Loosely check if is email. By loosely I mean it will not check if valid DNS. You can check this
* manually with the method @dns but in most cases this will not be necessary.
* @return bool
*/
public function email(): bool
{
return (filter_var($this->value, FILTER_VALIDATE_EMAIL) !== false);
}
/**
* Find in string
* @param string $match keyword to match against
* @param int|null $pos match start position if you want
* @return bool
*/
public function findInString(string $match, ?int $pos = null): bool
{
return ((is_null($pos) && str_contains($this->value, $match)) ||
(strpos($this->value, $match) === $pos));
}
/**
* Check if is a phone number
* @return bool
*/
public function phone(): bool
{
if (is_null($this->getStr)) {
return false;
}
$val = (string)$this->getStr->replace([" ", "-", "—", "–", "(", ")"], ["", "", "", "", "", ""]);
$match = preg_match('/^[0-9]{7,14}+$/', $val);
$strict = preg_match('/^\+[0-9]{1,2}[0-9]{6,13}$/', $val);
return ($strict || $match);
}
/**
* Check if is valid ZIP
* @param int $arg1 start length
* @param int|null $arg2 end length
* @return bool
* @throws ErrorException
*/
public function zip(int $arg1, ?int $arg2 = null): bool
{
if (is_null($this->getStr)) {
return false;
}
$this->value = (string)$this->getStr->replace([" ", "-", "—", "–"], ["", "", "", ""]);
$this->length = $this->getLength($this->value);
return ($this->isInt() && $this->length($arg1, $arg2));
}
/**
* Is value float
* Will validate whether a string is a valid float (User input is always a string)
* @return bool
*/
public function isFloat(): bool
{
return (filter_var($this->value, FILTER_VALIDATE_FLOAT) !== false);
}
/**
* Is value int
* Will validate whether a string is a valid integer (User input is always a string)
* @return bool
*/
public function isInt(): bool
{
return (filter_var($this->value, FILTER_VALIDATE_INT) !== false);
}
/**
* Is value string
* @return bool
*/
public function isString(): bool
{
return is_string($this->value);
}
/**
* Is value string
* @return bool
*/
public function isStr(): bool
{
return $this->isString();
}
/**
* Is value array
* @return bool
*/
public function isArray(): bool
{
return is_array($this->value);
}
/**
* Is value object
* @return bool
*/
public function isObject(): bool
{
return is_object($this->value);
}
/**
* Is value bool
* @return bool
*/
public function isBool(): bool
{
return (is_bool($this->value));
}
/**
* Is a valid json string
* @return bool
*/
public function isJson(): bool
{
json_decode($this->value);
return json_last_error() === JSON_ERROR_NONE;
}
/**
* Validate a string as html, check that it contains doctype, html, head and body
* @return bool
*/
public function isFullHtml(): bool
{
libxml_use_internal_errors(true);
$dom = new \DOMDocument();
if (!is_string($this->value) || !$dom->loadHTML($this->value, LIBXML_NOERROR | LIBXML_NOWARNING)) {
return false; // Invalid HTML syntax
}
if (!$dom->doctype || strtolower($dom->doctype->name) !== "html") {
return false;
}
$htmlTag = $dom->getElementsByTagName("html")->length > 0;
$headTag = $dom->getElementsByTagName("head")->length > 0;
$bodyTag = $dom->getElementsByTagName("body")->length > 0;
return $htmlTag && $headTag && $bodyTag;
}
/**
* Check if the value itself can be Interpreted as a bool value
* E.g. If value === ([on, off], [yes, no], [1, 0] or [true, false])
* @return bool
*/
public function isBoolVal(): bool
{
$val = strtolower(trim((string)$this->value));
$true = ($val === "on" || $val === "yes" || $val === "1" || $val === "true");
$false = ($val === "off" || $val === "no" || $val === "0" || $val === "false");
return ($true || $false);
}
/**
* Is null
* @return bool
*/
public function isNull(): bool
{
return is_null($this->value);
}
/**
* Is file
* @return bool
*/
public function isFile(): bool
{
return is_file($this->value);
}
/**
* Is directory
* @return bool
*/
public function isDir(): bool
{
return is_dir($this->value);
}
/**
* Is resource
* @return bool
*/
public function isResource(): bool
{
return is_resource($this->value);
}
/**
* Is writable
* @return bool
*/
public function isWritable(): bool
{
return is_writable($this->value);
}
/**
* Is readable
* @return bool
*/
public function isReadable(): bool
{
return is_readable($this->value);
}
/**
* Value is number
* @return bool
*/
public function number(): bool
{
return (is_numeric($this->value));
}
public function numeric(): bool
{
return $this->number();
}
public function numericVal(): bool
{
return $this->number();
}
/**
* Value is number positive 20
* @return bool
*/
public function positive(): bool
{
return ((float)$this->value >= 0);
}
/**
* Value is number negative -20
* @return bool
*/
public function negative(): bool
{
return ((float)$this->value < 0);
}
/**
* Value is minimum float|int value
* @param float $int
* @return bool
*/
public function min(float $int): bool
{
return ((float)$this->value >= $int);
}
/**
* Value is minimum float|int value (Same as "@min()" but can be used to add another error message)
* @param float $int
* @return bool
*/
public function minAlt(float $int): bool
{
return $this->min($int);
}
/**
* Value is maximum float|int value
* @param float $int
* @return bool
*/
public function max(float $int): bool
{
return ((float)$this->value <= $int);
}
/**
* Value string length is more than start ($arg1) or between start ($arg1) and end ($arg2)
* @param int $arg1 start length
* @param int|null $arg2 end length
* @return bool
*/
public function length(int $arg1, ?int $arg2 = null): bool
{
if ($this->length >= $arg1 && (($arg2 === null) || $this->length <= $arg2)) {
return true;
}
return false;
}
/**
* Value string length is equal to ($arg1)
* @param int $arg1 length
* @return bool
*/
public function equalLength(int $arg1): bool
{
if ($this->length === $arg1) {
return true;
}
return false;
}
/**
* IF value equals to param
* @param $str
* @return bool
*/
public function equal($str): bool
{
return ($this->value === $str);
}
/**
* IF value is less than to parameter
* @param $num
* @return bool
*/
public function lessThan($num): bool
{
return ($this->value < (float)$num);
}
/**
* IF value is more than to parameter
* @param $num
* @return bool
*/
public function moreThan($num): bool
{
return ($this->value > (float)$num);
}
/**
* Checks if a string contains a given substring
*
* @param string $needle
* @return bool
*/
public function contains(string $needle): bool
{
return str_contains($this->value, $needle);
}
/**
* Checks if a string starts with a given substring
*
* @param string $needle
* @return bool
*/
public function startsWith(string $needle): bool
{
return str_starts_with($this->value, $needle);
}
/**
* Checks if a string ends with a given substring
*
* @param string $needle
* @return bool
*/
public function endsWith(string $needle): bool
{
return str_ends_with($this->value, $needle);
}
/**
* IF value equals to param
* @param $str
* @return bool
*/
public function notEqual($str): bool
{
return ($this->value !== $str);
}
/**
* Check is a valid version number
* @param bool $strict (validate as a semantic Versioning, e.g. 1.0.0)
* @return bool
*/
public function validVersion(bool $strict = false): bool
{
$strictMatch = (!$strict || preg_match("/^(\d?\d)\.(\d?\d)\.(\d?\d)$/", (string)$this->value));
$compare = version_compare((string)$this->value, '0.0.1', '>=');
return ($strictMatch && $compare !== false && $compare >= 0);
}
/**
* Validate/compare if a version is equal/more/equalMore/less... e.g than withVersion
* @param string $withVersion
* @param '!='|'<'|'<='|'<>'|'='|'=='|'>'|'>='|'eq'|'ge'|'gt'|'le'|'lt'|'ne' $operator
* @return bool
*/
public function versionCompare(string $withVersion, string $operator = "=="): bool
{
if (in_array($operator, self::WHITELIST_OPERATORS)) {
return version_compare((string)$this->value, $withVersion, $operator);
}
return false;
}
/**
* Lossy password - Will return false if a character inputted is not allowed
* [a-zA-Z\d$@$!%*?&] - Matches "any" letter (uppercase or lowercase), digit, or special character
* from the allowed set of special characters
* @param integer $length Minimum length
* @return bool
*/
public function lossyPassword(int $length = 1): bool
{
return ((int)preg_match('/^[a-zA-Z\d$@$!%*?&]{' . $length . ',}$/', $this->value) > 0);
}
/**
* Strict password
* (?=.*[a-z]) - at least one lowercase letter
* (?=.*[A-Z]) - at least one uppercase letter
* (?=.*\d) - at least one digit
* (?=.*[$@$!%*?&]) - at least one special character from the set: $, @, #, !, %, *, ?, &
* [A-Za-z\d$@$!%*?&]{1,} - matches 1 or more characters consisting of letters, digits,
* and the allowed special characters
* I do tho recommend that you validate the length with @length(8, 60) method!
* @param integer $length Minimum length
* @return bool
*/
public function strictPassword(int $length = 1): bool
{
$pattern = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{' . $length . ',}$/';
return ((int)preg_match($pattern, $this->value) > 0);
}
/**
* Is value is string and character between a-z or A-Z
* @param $matchStr
* @return bool
*/
public function pregMatch($matchStr): bool
{
return ((int)preg_match("/^[" . $matchStr . "]+$/", $this->value) > 0);
}
/**
* Is value is string and character between a-z or A-Z
* @return bool
*/
public function atoZ(): bool
{
return ((int)preg_match("/^[a-zA-Z]+$/", $this->value) > 0);
}
/**
* Is value is string and character between a-z (LOWERCASE)
* @return bool
*/
public function lowerAtoZ(): bool
{
return ((int)preg_match("/^[a-z]+$/", $this->value) > 0);
}
/**
* Is value is string and character between A-Z (UPPERCASE)
* @return bool
*/
public function upperAtoZ(): bool
{
return ((int)preg_match("/^[A-Z]+$/", $this->value) > 0);
}
/**
* Is Hex color code string
* @return bool
*/
public function hex(): bool
{
return ((int)preg_match('/^#([0-9A-F]{3}){1,2}$/i', $this->value) > 0);
}
/**
* Check if is a date
* @param string $format validate after this date format (default Y-m-d)
* @return bool
*/
public function date(string $format = "Y-m-d"): bool
{
return (DateTime::createFromFormat($format, $this->value) !== false);
}
/**
* Check if is a date and time
* @param string $format validate after this date format (default Y-m-d H:i)
* @return bool
*/
public function dateTime(string $format = "Y-m-d H:i"): bool
{
return $this->date($format);
}
/**
* Check if is a date and time
* @param string $format validate after this date format (default Y-m-d H:i)
* @return bool
*/
public function time(string $format = "H:i"): bool
{
return $this->date($format);
}
/**
* Check if is a date and a "valid range"
* @param string $format validate after this date format (default Y-m-d H:i)
* @return array|false E.g. array(T1, T2); T1 = start and T2 = end
*/
public function dateRange(string $format = "Y-m-d H:i"): array|false
{
$exp = explode(" - ", $this->value);
if (count($exp) === 2) {
$time1 = trim($exp[0]);
$time2 = trim($exp[1]);
$val1 = DateTime::createFromFormat($format, $time1);
$val2 = DateTime::createFromFormat($format, $time2);
return (($val1 && $val2 && ($val1->getTimestamp() <= $val2->getTimestamp())) ?
["t1" => $time1, "t2" => $time2] : false);
}
return false;
}
/**
* Check "minimum" age (value format should be validated date "Y-m-d")
* @param int $arg1 18: user should be 18 or older
* @return bool
* @throws Exception
*/
public function age(int $arg1): bool
{
$now = (int)$this->dateTime->format("Y");
$dateTime = new DateTime($this->value);
$birth = (int)$dateTime->format("Y");
$age = ($now - $birth);
return ($age >= $arg1);
}
/**
* Check if is valid domain
* @param bool $strict stricter = true
* @return bool
*/
public function domain(bool $strict = true): bool
{
$strict = ($strict) ? FILTER_FLAG_HOSTNAME : 0;
return (filter_var((string)$this->value, FILTER_VALIDATE_DOMAIN, $strict) !== false);
}
/**
* Check if is valid URL (http|https is required)
* @return bool
*/
public function url(): bool
{
return (filter_var($this->value, FILTER_VALIDATE_URL) !== false);
}
/**
* Check if "Host|domain" has an valid DNS (will check A, AAAA and MX)
* @psalm-suppress UndefinedConstant
* @noinspection PhpComposerExtensionStubsInspection
* @return bool
*/
public function dns(): bool
{
$AResult = true;
$host = $this->getHost($this->value);
$MXResult = checkdnsrr($host); // Argument 2 is MX by default
if (!$MXResult) {
$AResult = checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA');
}
return ($MXResult || $AResult);
}
/**
* Match DNS record by search for TYPE and matching VALUE
* @param int $type (DNS_A, DNS_CNAME, DNS_HINFO, DNS_CAA, DNS_MX, DNS_NS, DNS_PTR, DNS_SOA,
* DNS_TXT, DNS_AAAA, DNS_SRV, DNS_NAPTR, DNS_A6, DNS_ALL or DNS_ANY)
* @noinspection PhpComposerExtensionStubsInspection
* @return array|false
*/
public function matchDNS(int $type): array|false
{
$host = $this->getHost($this->value);
$result = dns_get_record($host, $type);
if (is_array($result) && count($result) > 0) {
return $result;
}
return false;
}
/**
* Get hosts (used for DNS checks)
* @noinspection PhpComposerExtensionStubsInspection
* @param string $host
* @return string
*/
private function getHost(string $host): string
{
if (!defined('INTL_IDNA_VARIANT_2003')) {
define('INTL_IDNA_VARIANT_2003', 0);
}
$variant = (defined('INTL_IDNA_VARIANT_UTS46')) ? INTL_IDNA_VARIANT_UTS46 : INTL_IDNA_VARIANT_2003;
return rtrim(idn_to_ascii($host, IDNA_DEFAULT, $variant), '.') . '.';
}
/**
* Validate multiple. Will return true if "one" matches
* @param array $arr
* @return bool
* @throws ErrorException
*/
public function oneOf(array $arr): bool
{
$valid = false;
foreach ($arr as $method => $args) {
$inst = new self($this->value);
if(call_user_func_array([$inst, $method], $args)) {
$valid = true;
}
}
return $valid;
}
/**
* Validate multiple. Will return true if "all" matches
* @param array $arr
* @return bool
* @throws ErrorException
*/
public function allOf(array $arr): bool
{
foreach ($arr as $method => $args) {
$inst = new self($this->value);
if(!call_user_func_array([$inst, $method], $args)) {
return false;
}
}
return true;
}
}