Skip to content

Commit f6b7418

Browse files
committed
Imporve quality
1 parent cb75205 commit f6b7418

File tree

10 files changed

+331
-83
lines changed

10 files changed

+331
-83
lines changed

DynamicDataAbstract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
abstract class DynamicDataAbstract
1313
{
14-
private $data;
14+
protected $data = null;
1515

1616
abstract public function get();
1717

Format/Arr.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
final class Arr extends FormatAbstract implements FormatInterface
1414
{
1515
/**
16-
* Input is mixed data type in the interface becouse I do not know the type before
16+
* Input is mixed data type in the interface because I do not know the type before
1717
* The class constructor MUST handle the input validation
18-
* @param array $value
18+
* @param string $value
1919
*/
20-
public function __construct(array $value)
20+
public function __construct(mixed $value)
2121
{
22-
$this->value = $value;
22+
if (!is_array($value)) {
23+
throw new \InvalidArgumentException("Is expecting a string or a convertable string value.", 1);
24+
}
25+
parent::__construct((array)$value);
2326
}
2427

2528
/**
@@ -35,10 +38,9 @@ public static function value(mixed $value): FormatInterface
3538

3639
/**
3740
* Unset array
38-
* @param keys Keys that you want to unset (@unset("username", "password", "email", ....))
39-
* @return self
41+
* @return $this
4042
*/
41-
public function unset()
43+
public function unset(): self
4244
{
4345
$args = func_get_args();
4446
foreach ($args as $v) {
@@ -51,7 +53,7 @@ public function unset()
5153
* Get array keys
5254
* @return self
5355
*/
54-
public function arrayKeys()
56+
public function arrayKeys(): self
5557
{
5658
$this->value = array_keys($this->value);
5759
return $this;
@@ -60,8 +62,7 @@ public function arrayKeys()
6062
/**
6163
* Will explode an array item value and then merge it into array in same hierky
6264
* @param string $separator
63-
* @param array $array
64-
* @return array
65+
* @return self
6566
*/
6667
public function arrayItemExpMerge(string $separator): self
6768
{
@@ -89,15 +90,20 @@ public function pop(?Str &$poppedValue = null): self
8990
}
9091

9192
/**
92-
* Extract all array items with arrat key prefix ("prefix_"name)
93+
* Extract all array items with array key prefix ("prefix_"name)
9394
* @param string $search wildcard prefix
9495
* @return self
9596
*/
9697
public function wildcardSearch(string $search): self
9798
{
98-
$search = str_replace('\*', '.*?', preg_quote($search, '/'));
99-
$result = preg_grep('/^' . $search . '$/i', array_keys($this->value));
100-
$this->value = array_intersect_key($this->value, array_flip($result));
99+
$regex = "/^" . str_replace(['\*', '\?'], ['.*', '.'], preg_quote($search, '/')) . "$/";
100+
$matches = [];
101+
foreach ($this->value as $element) {
102+
if (preg_match($regex, $element)) {
103+
$matches[] = $element;
104+
}
105+
}
106+
$this->value = $matches;
101107
return $this;
102108
}
103109

Format/Clock.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* @Package: MaplePHP Format string
5+
* @Author: Daniel Ronkainen
6+
* @Licence: Apache-2.0 license, Copyright © Daniel Ronkainen
7+
Don't delete this comment, its part of the license.
8+
*/
9+
10+
namespace MaplePHP\DTO\Format;
11+
12+
use Exception;
13+
use InvalidArgumentException;
14+
15+
final class Clock extends FormatAbstract implements FormatInterface
16+
{
17+
/**
18+
* Input is mixed data type in the interface because I do not know the type before
19+
* The class constructor MUST handle the input validation
20+
* @param string $value
21+
* @throws Exception
22+
*/
23+
public function __construct(mixed $value)
24+
{
25+
if (is_array($value) || is_object($value)) {
26+
throw new InvalidArgumentException("Is expecting a string or a convertable string value.", 1);
27+
}
28+
29+
parent::__construct(new \DateTime($value));
30+
}
31+
32+
/**
33+
* Format date data
34+
* @param string $format
35+
* @return object
36+
*/
37+
public function format(string $format = 'Y-m-d H:i:s'): string
38+
{
39+
return $this->value->format($format);
40+
}
41+
42+
/**
43+
* Get DTO value
44+
* @return mixed
45+
*/
46+
public function get(): mixed
47+
{
48+
return $this->value->format('Y-m-d H:i:s');
49+
}
50+
51+
/**
52+
* Init format by adding data to modify/format/traverse
53+
* @param mixed $value
54+
* @return self
55+
* @throws Exception
56+
*/
57+
public static function value(mixed $value): FormatInterface
58+
{
59+
return new Clock((string)$value);
60+
}
61+
62+
/**
63+
* Get Value
64+
* @return string
65+
*/
66+
public function __toString(): string
67+
{
68+
return (string)$this->get();
69+
}
70+
}

Format/Collection.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/**
4+
* @Package: MaplePHP Format string
5+
* @Author: Daniel Ronkainen
6+
* @Licence: Apache-2.0 license, Copyright © Daniel Ronkainen
7+
Don't delete this comment, its part of the license.
8+
*/
9+
10+
namespace MaplePHP\DTO\Format;
11+
12+
use InvalidArgumentException;
13+
14+
final class Collection extends FormatAbstract implements FormatInterface
15+
{
16+
//protected $value;
17+
18+
/**
19+
* Input is mixed data type in the interface because I do not know the type before
20+
* The class constructor MUST handle the input validation
21+
* @param string $value
22+
*/
23+
public function __construct(mixed $value)
24+
{
25+
parent::__construct(new \ArrayObject($value));
26+
}
27+
28+
public function __call($func, $argv)
29+
{
30+
if(method_exists($this->value, $func)) {
31+
$val = $this->value->{$func}(...$argv);
32+
$this->value = $this->value->getArrayCopy();
33+
34+
} else {
35+
$copy = $this->value->getArrayCopy();
36+
$this->value = call_user_func_array($func, array_merge(array($copy), $argv));
37+
38+
}
39+
40+
return $this;
41+
}
42+
43+
/**
44+
* Get/return result
45+
* @param mixed $value
46+
* @param bool $append
47+
* @return self
48+
*/
49+
public function merge(mixed $value, bool $append = true): self
50+
{
51+
if($append) {
52+
$this->value = array_merge($this->value->getArrayCopy(), (array)$value);
53+
} else {
54+
$this->value = array_merge((array)$value, $this->value->getArrayCopy());
55+
}
56+
return $this;
57+
}
58+
59+
/**
60+
* Get/return result
61+
* @param mixed $value
62+
* @return self
63+
*/
64+
public function prepend(mixed $value): self
65+
{
66+
$value = [$value];
67+
$this->value = array_merge($value, $this->value->getArrayCopy());
68+
return $this;
69+
}
70+
/**
71+
* Init format by adding data to modify/format/traverse
72+
* @param mixed $value
73+
* @return self
74+
*/
75+
public static function value(mixed $value): FormatInterface
76+
{
77+
return new Collection($value);
78+
}
79+
80+
}

Format/Encode.php

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
final class Encode extends FormatAbstract implements FormatInterface
66
{
77
//protected $value;
8-
protected $jsonEncode = true;
9-
protected $specialChar = true;
10-
protected $specialCharFlag = ENT_NOQUOTES;
11-
protected $urlencode = false;
8+
protected bool $jsonEncode = true;
9+
protected bool $specialChar = true;
10+
protected int $specialCharFlag = ENT_NOQUOTES;
11+
protected bool $urlencode = false;
12+
protected bool $sanitizeIdentifiers = false;
1213

1314
/**
14-
* Input is mixed data type in the interface becouse I do not know the type before the class
15-
* The class constructor MUST handle the input validation
15+
* Input is mixed data type in the interface because I do not know the type before
16+
* the Class constructor MUST handle the input validation
1617
* @param array|string $value
1718
*/
1819
public function __construct(array|string $value)
1920
{
20-
$this->value = $value;
21+
parent::__construct($value);
2122
}
2223

2324
/**
@@ -27,13 +28,24 @@ public function __construct(array|string $value)
2728
*/
2829
public static function value(mixed $value): FormatInterface
2930
{
30-
$inst = new static($value);
31-
return $inst;
31+
return new self($value);
32+
}
33+
34+
/**
35+
* Remove any character that is not a letter, number, underscore, or dash
36+
* Can be used to sanitize SQL identifiers that should be enclosed in backticks
37+
* @param bool $sanitizeIdentifiers
38+
* @return self
39+
*/
40+
public function sanitizeIdentifiers(bool $sanitizeIdentifiers): self
41+
{
42+
$this->sanitizeIdentifiers = $sanitizeIdentifiers;
43+
return $this;
3244
}
3345

3446
/**
3547
* Url encode flag
36-
* @param bool $urlencode
48+
* @param bool $encode
3749
* @return self
3850
*/
3951
public function urlEncode(bool $encode): self
@@ -44,11 +56,11 @@ public function urlEncode(bool $encode): self
4456

4557
/**
4658
* Special Char encode
47-
* @param bool $urlencode
48-
* @param int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401
59+
* @param bool $encode
60+
* @param int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401
4961
* @return self
5062
*/
51-
public function specialChar(bool $encode, $flag = ENT_NOQUOTES): self
63+
public function specialChar(bool $encode, int $flag = ENT_NOQUOTES): self
5264
{
5365
$this->specialChar = $encode;
5466
$this->specialCharFlag = $flag;
@@ -57,17 +69,19 @@ public function specialChar(bool $encode, $flag = ENT_NOQUOTES): self
5769

5870
/**
5971
* Encode values
60-
* @param callable|null $callback Access encode value with callable and build upon
6172
* @return string|array
6273
*/
63-
public function encode(?callable $callback = null): string|array
74+
public function encode(): string|array
6475
{
65-
// Allways url decode first
76+
// Always url decode first
6677
$this->value = $this->urldecode(function($value) {
6778
$uri = Str::value((string)$value);
6879
if ($this->urlencode) {
6980
$uri->rawurlencode();
7081
}
82+
if ($this->sanitizeIdentifiers) {
83+
$uri->sanitizeIdentifiers();
84+
}
7185
if ($this->specialChar) {
7286
$uri->encode($this->specialCharFlag);
7387
}
@@ -78,14 +92,13 @@ public function encode(?callable $callback = null): string|array
7892
}
7993

8094
/**
81-
* urldecode
95+
* Url decode
8296
* @param callable|null $callback Access encode value with callable and build upon
8397
* @return string|array
8498
*/
8599
public function urldecode(?callable $callback = null): string|array
86100
{
87101
if (is_array($this->value)) {
88-
89102
$this->value = Arr::value($this->value)->walk(function ($value) use ($callback) {
90103
$value = Str::value((string)$value)->rawurldecode()->get();
91104
if (!is_null($callback)) {
@@ -94,6 +107,7 @@ public function urldecode(?callable $callback = null): string|array
94107
return $value;
95108

96109
})->get();
110+
97111
} else {
98112
$this->value = Str::value($this->value)->rawurldecode()->get();
99113
if (!is_null($callback)) {
@@ -105,10 +119,11 @@ public function urldecode(?callable $callback = null): string|array
105119

106120
/**
107121
* XXS Protect the result
108-
* @return self
122+
* @param callable|null $callback
123+
* @return array|string
109124
*/
110-
public function xss(?callable $callback = null): self
125+
public function xss(?callable $callback = null): array|string
111126
{
112-
return $this->specialChar(true)->encode($callback);
127+
return $this->specialChar(true)->encode();
113128
}
114129
}

0 commit comments

Comments
 (0)