Skip to content

Commit ec93f9c

Browse files
committed
Code quality improvements
1 parent f7b44a3 commit ec93f9c

File tree

5 files changed

+300
-98
lines changed

5 files changed

+300
-98
lines changed

Inp.php

Lines changed: 94 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
use ErrorException;
1313
use Exception;
14+
use http\Exception\InvalidArgumentException;
1415
use MaplePHP\DTO\MB;
1516
use MaplePHP\Validate\Interfaces\InpInterface;
1617
use MaplePHP\DTO\Format\Str;
1718
use DateTime;
1819

1920
class Inp implements InpInterface
2021
{
21-
const WHITELIST_OPERATORS = [
22+
public const WHITELIST_OPERATORS = [
2223
'!=',
2324
'<',
2425
'<=',
@@ -44,22 +45,36 @@ class Inp implements InpInterface
4445

4546
/**
4647
* Start instance
47-
* @param mixed $value the input value
48+
* @param mixed $value the input value
49+
* @throws ErrorException
4850
*/
4951
public function __construct(mixed $value)
5052
{
5153
$this->value = $value;
5254
$this->dateTime = new DateTime("now");
5355
if(is_string($value) || is_numeric($value)) {
54-
$this->length = $this->getLength($value);
56+
$this->length = $this->getLength((string)$value);
5557
$this->getStr = new Str($this->value);
5658
}
5759
}
5860

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+
5973
/**
6074
* Start instance
61-
* @param string $value the input value
75+
* @param string $value the input value
6276
* @return self
77+
* @throws ErrorException
6378
*/
6479
public static function value(mixed $value): self
6580
{
@@ -75,7 +90,7 @@ public static function value(mixed $value): self
7590
public function getLength(string $value): int
7691
{
7792
$mb = new MB($value);
78-
return $mb->strlen();
93+
return (int)$mb->strlen();
7994
}
8095

8196
/**
@@ -185,6 +200,9 @@ public function findInString(string $match, ?int $pos = null): bool
185200
*/
186201
public function phone(): bool
187202
{
203+
if (is_null($this->getStr)) {
204+
return false;
205+
}
188206
$val = (string)$this->getStr->replace([" ", "-", "", "", "(", ")"], ["", "", "", "", "", ""]);
189207
$match = preg_match('/^[0-9]{7,14}+$/', $val);
190208
$strict = preg_match('/^\+[0-9]{1,2}[0-9]{6,13}$/', $val);
@@ -193,15 +211,19 @@ public function phone(): bool
193211

194212
/**
195213
* Check if is valid ZIP
196-
* @param int $arg1 start length
197-
* @param int|null $arg2 end length
214+
* @param int $arg1 start length
215+
* @param int|null $arg2 end length
198216
* @return bool
217+
* @throws ErrorException
199218
*/
200219
public function zip(int $arg1, int $arg2 = null): bool
201220
{
221+
if (is_null($this->getStr)) {
222+
return false;
223+
}
202224
$this->value = (string)$this->getStr->replace([" ", "-", "", ""], ["", "", "", ""]);
203225
$this->length = $this->getLength($this->value);
204-
return ($this->int() && $this->length($arg1, $arg2));
226+
return ($this->isInt() && $this->length($arg1, $arg2));
205227
}
206228

207229
/**
@@ -214,12 +236,6 @@ public function isFloat(): bool
214236
return (filter_var($this->value, FILTER_VALIDATE_FLOAT) !== false);
215237
}
216238

217-
// Deprecated
218-
public function float(): bool
219-
{
220-
return $this->isFloat();
221-
}
222-
223239
/**
224240
* Is value int
225241
* Will validate whether a string is a valid integer (User input is always a string)
@@ -230,12 +246,6 @@ public function isInt(): bool
230246
return (filter_var($this->value, FILTER_VALIDATE_INT) !== false);
231247
}
232248

233-
// Deprecated
234-
public function int(): bool
235-
{
236-
return $this->isInt();
237-
}
238-
239249
/**
240250
* Is value string
241251
* @return bool
@@ -245,8 +255,11 @@ public function isString(): bool
245255
return is_string($this->value);
246256
}
247257

248-
// Deprecated
249-
public function string(): bool
258+
/**
259+
* Is value string
260+
* @return bool
261+
*/
262+
public function isStr(): bool
250263
{
251264
return $this->isString();
252265
}
@@ -260,12 +273,6 @@ public function isArray(): bool
260273
return is_array($this->value);
261274
}
262275

263-
// Deprecated
264-
public function array(): bool
265-
{
266-
return $this->isArray();
267-
}
268-
269276
/**
270277
* Is value object
271278
* @return bool
@@ -275,12 +282,6 @@ public function isObject(): bool
275282
return is_object($this->value);
276283
}
277284

278-
// Deprecated
279-
public function object(): bool
280-
{
281-
return $this->isObject();
282-
}
283-
284285
/**
285286
* Is value bool
286287
* @return bool
@@ -290,12 +291,6 @@ public function isBool(): bool
290291
return (is_bool($this->value));
291292
}
292293

293-
// Deprecated
294-
public function bool(): bool
295-
{
296-
return $this->isBool();
297-
}
298-
299294
/**
300295
* Check if the value itself can be Interpreted as a bool value
301296
* E.g. If value === ([on, off], [yes, no], [1, 0] or [true, false])
@@ -309,10 +304,58 @@ public function isBoolVal(): bool
309304
return ($true || $false);
310305
}
311306

312-
// Deprecated
313-
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
314321
{
315-
return $this->isBoolVal();
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
357+
{
358+
return is_readable($this->value);
316359
}
317360

318361
/**
@@ -444,7 +487,7 @@ public function validVersion(bool $strict = false): bool
444487
/**
445488
* Validate/compare if a version is equal/more/equalMore/less... e.g than withVersion
446489
* @param string $withVersion
447-
* @param string $operator '!='|'<'|'<='|'<>'|'='|'=='|'>'|'>='|'eq'|'ge'|'gt'|'le'|'lt'|'ne'
490+
* @param '!='|'<'|'<='|'<>'|'='|'=='|'>'|'>='|'eq'|'ge'|'gt'|'le'|'lt'|'ne' $operator
448491
* @return bool
449492
*/
450493
public function versionCompare(string $withVersion, string $operator = "=="): bool
@@ -590,10 +633,10 @@ public function dateRange(string $format = "Y-m-d H:i"): array|false
590633
*/
591634
public function age(int $arg1): bool
592635
{
593-
$now = $this->dateTime->format("Y");
636+
$now = (int)$this->dateTime->format("Y");
594637
$dateTime = new DateTime($this->value);
595-
$birth = $dateTime->format("Y");
596-
$age = (int)($now - $birth);
638+
$birth = (int)$dateTime->format("Y");
639+
$age = ($now - $birth);
597640
return ($age <= $arg1);
598641
}
599642

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

669712
/**
670713
* Validate multiple. Will return true if "one" matches
671-
* @param array $arr
714+
* @param array $arr
672715
* @return bool
716+
* @throws ErrorException
673717
*/
674718
public function oneOf(array $arr): bool
675719
{
@@ -685,8 +729,9 @@ public function oneOf(array $arr): bool
685729

686730
/**
687731
* Validate multiple. Will return true if "all" matches
688-
* @param array $arr
732+
* @param array $arr
689733
* @return bool
734+
* @throws ErrorException
690735
*/
691736
public function allOf(array $arr): bool
692737
{

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)