Skip to content

Commit 1751a88

Browse files
committed
Merge branch 'develop'
2 parents b5e8ae7 + 1718d1d commit 1751a88

File tree

6 files changed

+306
-100
lines changed

6 files changed

+306
-100
lines changed

Inp.php

Lines changed: 99 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99

1010
namespace MaplePHP\Validate;
1111

12+
use ErrorException;
1213
use Exception;
14+
use http\Exception\InvalidArgumentException;
15+
use MaplePHP\DTO\MB;
1316
use MaplePHP\Validate\Interfaces\InpInterface;
1417
use MaplePHP\DTO\Format\Str;
1518
use DateTime;
1619

1720
class Inp implements InpInterface
1821
{
19-
const WHITELIST_OPERATORS = [
22+
public const WHITELIST_OPERATORS = [
2023
'!=',
2124
'<',
2225
'<=',
@@ -42,22 +45,36 @@ class Inp implements InpInterface
4245

4346
/**
4447
* Start instance
45-
* @param mixed $value the input value
48+
* @param mixed $value the input value
49+
* @throws ErrorException
4650
*/
4751
public function __construct(mixed $value)
4852
{
4953
$this->value = $value;
5054
$this->dateTime = new DateTime("now");
5155
if(is_string($value) || is_numeric($value)) {
52-
$this->length = $this->getLength($value);
56+
$this->length = $this->getLength((string)$value);
5357
$this->getStr = new Str($this->value);
5458
}
5559
}
5660

61+
/**
62+
* Immutable: Validate against new value
63+
* @param mixed $value
64+
* @return InpInterface
65+
*/
66+
public function withValue(mixed $value): InpInterface
67+
{
68+
$inst = clone $this;
69+
$inst->value = $value;
70+
return $inst;
71+
}
72+
5773
/**
5874
* Start instance
59-
* @param string $value the input value
75+
* @param string $value the input value
6076
* @return self
77+
* @throws ErrorException
6178
*/
6279
public static function value(mixed $value): self
6380
{
@@ -66,12 +83,14 @@ public static function value(mixed $value): self
6683

6784
/**
6885
* Get value string length
69-
* @param string $value
86+
* @param string $value
7087
* @return int
88+
* @throws ErrorException
7189
*/
7290
public function getLength(string $value): int
7391
{
74-
return strlen($value);
92+
$mb = new MB($value);
93+
return (int)$mb->strlen();
7594
}
7695

7796
/**
@@ -181,6 +200,9 @@ public function findInString(string $match, ?int $pos = null): bool
181200
*/
182201
public function phone(): bool
183202
{
203+
if (is_null($this->getStr)) {
204+
return false;
205+
}
184206
$val = (string)$this->getStr->replace([" ", "-", "", "", "(", ")"], ["", "", "", "", "", ""]);
185207
$match = preg_match('/^[0-9]{7,14}+$/', $val);
186208
$strict = preg_match('/^\+[0-9]{1,2}[0-9]{6,13}$/', $val);
@@ -189,15 +211,19 @@ public function phone(): bool
189211

190212
/**
191213
* Check if is valid ZIP
192-
* @param int $arg1 start length
193-
* @param int|null $arg2 end length
214+
* @param int $arg1 start length
215+
* @param int|null $arg2 end length
194216
* @return bool
217+
* @throws ErrorException
195218
*/
196219
public function zip(int $arg1, int $arg2 = null): bool
197220
{
221+
if (is_null($this->getStr)) {
222+
return false;
223+
}
198224
$this->value = (string)$this->getStr->replace([" ", "-", "", ""], ["", "", "", ""]);
199225
$this->length = $this->getLength($this->value);
200-
return ($this->int() && $this->length($arg1, $arg2));
226+
return ($this->isInt() && $this->length($arg1, $arg2));
201227
}
202228

203229
/**
@@ -210,12 +236,6 @@ public function isFloat(): bool
210236
return (filter_var($this->value, FILTER_VALIDATE_FLOAT) !== false);
211237
}
212238

213-
// Deprecated
214-
public function float(): bool
215-
{
216-
return $this->isFloat();
217-
}
218-
219239
/**
220240
* Is value int
221241
* Will validate whether a string is a valid integer (User input is always a string)
@@ -226,12 +246,6 @@ public function isInt(): bool
226246
return (filter_var($this->value, FILTER_VALIDATE_INT) !== false);
227247
}
228248

229-
// Deprecated
230-
public function int(): bool
231-
{
232-
return $this->isInt();
233-
}
234-
235249
/**
236250
* Is value string
237251
* @return bool
@@ -241,8 +255,11 @@ public function isString(): bool
241255
return is_string($this->value);
242256
}
243257

244-
// Deprecated
245-
public function string(): bool
258+
/**
259+
* Is value string
260+
* @return bool
261+
*/
262+
public function isStr(): bool
246263
{
247264
return $this->isString();
248265
}
@@ -256,12 +273,6 @@ public function isArray(): bool
256273
return is_array($this->value);
257274
}
258275

259-
// Deprecated
260-
public function array(): bool
261-
{
262-
return $this->isArray();
263-
}
264-
265276
/**
266277
* Is value object
267278
* @return bool
@@ -271,12 +282,6 @@ public function isObject(): bool
271282
return is_object($this->value);
272283
}
273284

274-
// Deprecated
275-
public function object(): bool
276-
{
277-
return $this->isObject();
278-
}
279-
280285
/**
281286
* Is value bool
282287
* @return bool
@@ -286,12 +291,6 @@ public function isBool(): bool
286291
return (is_bool($this->value));
287292
}
288293

289-
// Deprecated
290-
public function bool(): bool
291-
{
292-
return $this->isBool();
293-
}
294-
295294
/**
296295
* Check if the value itself can be Interpreted as a bool value
297296
* E.g. If value === ([on, off], [yes, no], [1, 0] or [true, false])
@@ -305,10 +304,58 @@ public function isBoolVal(): bool
305304
return ($true || $false);
306305
}
307306

308-
// Deprecated
309-
public function boolVal(): bool
307+
/**
308+
* Is null
309+
* @return bool
310+
*/
311+
public function isNull(): bool
312+
{
313+
return is_null($this->value);
314+
}
315+
316+
/**
317+
* Is file
318+
* @return bool
319+
*/
320+
public function isFile(): bool
321+
{
322+
return is_file($this->value);
323+
}
324+
325+
/**
326+
* Is directory
327+
* @return bool
328+
*/
329+
public function isDir(): bool
330+
{
331+
return is_dir($this->value);
332+
}
333+
334+
/**
335+
* Is resource
336+
* @return bool
337+
*/
338+
public function isResource(): bool
339+
{
340+
return is_resource($this->value);
341+
}
342+
343+
/**
344+
* Is writable
345+
* @return bool
346+
*/
347+
public function isWritable(): bool
348+
{
349+
return is_writable($this->value);
350+
}
351+
352+
/**
353+
* Is readable
354+
* @return bool
355+
*/
356+
public function isReadable(): bool
310357
{
311-
return $this->isBoolVal();
358+
return is_readable($this->value);
312359
}
313360

314361
/**
@@ -440,7 +487,7 @@ public function validVersion(bool $strict = false): bool
440487
/**
441488
* Validate/compare if a version is equal/more/equalMore/less... e.g than withVersion
442489
* @param string $withVersion
443-
* @param string $operator '!='|'<'|'<='|'<>'|'='|'=='|'>'|'>='|'eq'|'ge'|'gt'|'le'|'lt'|'ne'
490+
* @param '!='|'<'|'<='|'<>'|'='|'=='|'>'|'>='|'eq'|'ge'|'gt'|'le'|'lt'|'ne' $operator
444491
* @return bool
445492
*/
446493
public function versionCompare(string $withVersion, string $operator = "=="): bool
@@ -586,10 +633,10 @@ public function dateRange(string $format = "Y-m-d H:i"): array|false
586633
*/
587634
public function age(int $arg1): bool
588635
{
589-
$now = $this->dateTime->format("Y");
636+
$now = (int)$this->dateTime->format("Y");
590637
$dateTime = new DateTime($this->value);
591-
$birth = $dateTime->format("Y");
592-
$age = (int)($now - $birth);
638+
$birth = (int)$dateTime->format("Y");
639+
$age = ($now - $birth);
593640
return ($age <= $arg1);
594641
}
595642

@@ -664,8 +711,9 @@ private function getHost(string $host): string
664711

665712
/**
666713
* Validate multiple. Will return true if "one" matches
667-
* @param array $arr
714+
* @param array $arr
668715
* @return bool
716+
* @throws ErrorException
669717
*/
670718
public function oneOf(array $arr): bool
671719
{
@@ -681,8 +729,9 @@ public function oneOf(array $arr): bool
681729

682730
/**
683731
* Validate multiple. Will return true if "all" matches
684-
* @param array $arr
732+
* @param array $arr
685733
* @return bool
734+
* @throws ErrorException
686735
*/
687736
public function allOf(array $arr): bool
688737
{

Luhn.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
class Luhn
1515
{
1616
private $number;
17-
private $length;
1817
private $string;
1918
private $part;
19+
//private $length;
2020

2121
/**
2222
* Start intsance and input Value
@@ -27,7 +27,7 @@ public function __construct($number)
2727

2828
$this->string = preg_replace('/[^A-Z\d]/', '', strtoupper($number));
2929
$this->number = preg_replace('/\D/', '', $number);
30-
$this->length = (is_string($this->number)) ? strlen($this->number) : 0;
30+
//$this->length = (is_string($this->number)) ? strlen($this->number) : 0;
3131
}
3232

3333
/**
@@ -215,11 +215,11 @@ final protected function luhn($number): float
215215
*/
216216
final protected function part()
217217
{
218-
$match = array();
218+
$match = [];
219219
$reg = '/^(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([\+\-\s]?)(\d{3})(\d)$/';
220220
preg_match($reg, $this->number, $match);
221221
if (count($match) !== 8) {
222-
return array();
222+
return [];
223223
}
224224

225225
$century = $match[1];
@@ -230,7 +230,7 @@ final protected function part()
230230
$num = $match[6];
231231
$check = $match[7];
232232

233-
if (!in_array($sep, array('-', '+'))) {
233+
if (!in_array($sep, ['-', '+'])) {
234234
if (empty($century) || date('Y') - intval(strval($century) . strval($year)) < 100) {
235235
$sep = '-';
236236
} else {
@@ -246,15 +246,15 @@ final protected function part()
246246
$century = substr((string)($baseYear - (($baseYear - $year) % 100)), 0, 2);
247247
}
248248

249-
return array(
249+
return [
250250
'century' => $century,
251251
'year' => $year,
252252
'month' => $month,
253253
'day' => $day,
254254
'sep' => $sep,
255255
'num' => $num,
256256
'check' => $check
257-
);
257+
];
258258
}
259259

260260
/**

0 commit comments

Comments
 (0)