Skip to content

Commit 0bb381d

Browse files
author
Wazabii
committed
Improvements
1 parent 3d32909 commit 0bb381d

File tree

2 files changed

+87
-62
lines changed

2 files changed

+87
-62
lines changed

Format/Encode.php

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ final class Encode extends FormatAbstract implements FormatInterface
66
{
77
//protected $value;
88
protected $jsonEncode = true;
9+
protected $specialChar = true;
10+
protected $specialCharFlag = ENT_NOQUOTES;
911
protected $urlencode = false;
1012

1113
/**
@@ -34,39 +36,71 @@ public static function value(mixed $value): FormatInterface
3436
* @param bool $urlencode
3537
* @return self
3638
*/
37-
public function urlEncode(bool $urlencode = true): self
39+
public function urlEncode(bool $encode): self
3840
{
39-
$this->urlencode = $urlencode;
41+
$this->urlencode = $encode;
42+
return $this;
43+
}
44+
45+
/**
46+
* Special Char encode
47+
* @param bool $urlencode
48+
* @param int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401
49+
* @return self
50+
*/
51+
public function specialChar(bool $encode, $flag = ENT_NOQUOTES): self
52+
{
53+
$this->specialChar = $encode;
54+
$this->specialCharFlag = $flag;
4055
return $this;
4156
}
4257

4358
/**
4459
* Encode values
4560
* @param callable|null $callback Access encode value with callable and build upon
46-
* @param int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401
47-
* @return self
61+
* @return string|array
4862
*/
49-
public function encode(?callable $callback = null, int $flag = ENT_QUOTES): self
63+
public function encode(?callable $callback = null): string|array
64+
{
65+
// Allways url decode first
66+
$this->value = $this->urldecode(function($value) {
67+
$uri = Str::value((string)$value);
68+
if ($this->urlencode) {
69+
$uri->rawurlencode();
70+
}
71+
if ($this->specialChar) {
72+
$uri->encode($this->specialCharFlag);
73+
}
74+
return $uri->get();
75+
});
76+
77+
return $this->value;
78+
}
79+
80+
/**
81+
* urldecode
82+
* @param callable|null $callback Access encode value with callable and build upon
83+
* @return string|array
84+
*/
85+
public function urldecode(?callable $callback = null): string|array
5086
{
5187
if (is_array($this->value)) {
52-
$this->value = Arr::value($this->value)->walk(function ($value) use ($callback, $flag) {
88+
89+
$this->value = Arr::value($this->value)->walk(function ($value) use ($callback) {
90+
$value = Str::value((string)$value)->rawurldecode()->get();
5391
if (!is_null($callback)) {
5492
$value = $callback($value);
5593
}
56-
$uri = Str::value((string)$value)->encode($flag);
57-
if ($this->urlencode) {
58-
$uri->rawurlencode();
59-
}
60-
return $uri->get();
94+
return $value;
95+
6196
})->get();
6297
} else {
98+
$this->value = Str::value($this->value)->rawurldecode()->get();
6399
if (!is_null($callback)) {
64100
$this->value = $callback($this->value);
65101
}
66-
$this->value = Str::value($this->value)->encode($flag)->get();
67102
}
68-
69-
return $this;
103+
return $this->value;
70104
}
71105

72106
/**
@@ -75,6 +109,6 @@ public function encode(?callable $callback = null, int $flag = ENT_QUOTES): self
75109
*/
76110
public function xss(?callable $callback = null): self
77111
{
78-
return $this->encode($callback);
112+
return $this->specialChar(true)->encode($callback);
79113
}
80114
}

Traverse.php

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -47,34 +47,6 @@ public function get(?string $fallback = null): mixed
4747
return (!is_null($this->row) ? $this->row : $fallback);
4848
}
4949

50-
/**
51-
* Traverse factory
52-
* If you want
53-
* @return self
54-
*/
55-
public function __call($method, $args)
56-
{
57-
$this->row = ($this->{$method} ?? null);
58-
$this->raw = $this->row;
59-
60-
if (count($args) > 0) {
61-
$name = ucfirst($args[0]);
62-
$className = "MaplePHP\\DTO\\Format\\{$name}";
63-
if (!class_exists($className)) {
64-
throw new \Exception("The DTO Format class do not exist!", 1);
65-
}
66-
$reflect = new \ReflectionClass($className);
67-
$instance = $reflect->newInstanceWithoutConstructor();
68-
return $instance->value($this->row);
69-
}
70-
71-
if (is_array($this->row) || is_object($this->row)) {
72-
return $this::value($this->row, $this->raw);
73-
}
74-
75-
return self::value($this->row);
76-
}
77-
7850
/**
7951
* Get raw
8052
* @return mixed
@@ -84,24 +56,16 @@ public function getRaw()
8456
return $this->raw;
8557
}
8658

59+
8760
/**
88-
* Callable factory
89-
* @psalm-suppress InvalidFunctionCall Psalm do not understand that $call is callable
90-
* @return mixed
61+
* Json decode value
62+
* @return self
9163
*/
92-
/*
93-
public function fetchFactory(): mixed
64+
public function jsonDecode(): self
9465
{
95-
return function ($arr, $row, $_unusedKey, $index) {
96-
$data = array_values($this->raw);
97-
$call = (isset($data[$index])) ? $data[$index] : null;
98-
if (is_callable($call)) {
99-
return $call($arr, $row);
100-
}
101-
return false;
102-
};
66+
$this->row = json_decode($this->row);
67+
return $this::value($this->row);
10368
}
104-
*/
10569

10670
/**
10771
* Access incremental array
@@ -113,13 +77,13 @@ public function fetch(?callable $callback = null)
11377
$index = 0;
11478
$new = array();
11579

116-
if (is_null($this->raw)) {
117-
$this->raw = $this->data;
80+
if (is_null($this->row)) {
81+
$this->row = $this->data;
11882
}
11983

120-
foreach ($this->raw as $key => $row) {
84+
foreach ($this->row as $key => $row) {
12185
if (!is_null($callback)) {
122-
if (($get = $callback($this::value($this->raw), $row, $key, $index)) !== false) {
86+
if (($get = $callback($this::value($this->row), $row, $key, $index)) !== false) {
12387
$new[$key] = $get;
12488
}
12589
} else {
@@ -142,7 +106,6 @@ public function fetch(?callable $callback = null)
142106
return $this;
143107
}
144108

145-
146109
/**
147110
* Chech if current traverse data is equal to val
148111
* @param string $isVal
@@ -196,4 +159,32 @@ public function sprint(string $add)
196159
}
197160
return $this;
198161
}
162+
163+
/**
164+
* Traverse factory
165+
* If you want
166+
* @return self
167+
*/
168+
public function __call($method, $args)
169+
{
170+
$this->row = ($this->{$method} ?? null);
171+
$this->raw = $this->row;
172+
173+
if (count($args) > 0) {
174+
$name = ucfirst($args[0]);
175+
$className = "MaplePHP\\DTO\\Format\\{$name}";
176+
if (!class_exists($className)) {
177+
throw new \Exception("The DTO Format class do not exist!", 1);
178+
}
179+
$reflect = new \ReflectionClass($className);
180+
$instance = $reflect->newInstanceWithoutConstructor();
181+
return $instance->value($this->row);
182+
}
183+
184+
if (is_array($this->row) || is_object($this->row)) {
185+
return $this::value($this->row, $this->raw);
186+
}
187+
188+
return self::value($this->row);
189+
}
199190
}

0 commit comments

Comments
 (0)