Skip to content

Commit 7621191

Browse files
author
Wazabii
committed
Structural improvement
1 parent 1b3fba3 commit 7621191

File tree

9 files changed

+166
-46
lines changed

9 files changed

+166
-46
lines changed

Format/Arr.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,22 @@
1212

1313
final class Arr extends FormatAbstract implements FormatInterface
1414
{
15+
/**
16+
* Input is mixed data type in the interface becouse I do not know the type before the class
17+
* The class constructor MUST handle the input validation
18+
* @param array $value
19+
*/
20+
public function __construct(array $value)
21+
{
22+
$this->value = $value;
23+
}
24+
1525
/**
1626
* Init format by adding data to modify/format/traverse
17-
* @param array $arr
27+
* @param mixed $value
1828
* @return self
1929
*/
20-
public static function value($value): FormatInterface
30+
public static function value(mixed $value): FormatInterface
2131
{
2232
$inst = new static($value);
2333
return $inst;

Format/DateTime.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class DateTime extends MainDateTime implements FormatInterface
4141
private $translations = array();
4242

4343

44+
/**
45+
* Input is mixed data type in the interface becouse I do not know the type before the class
46+
* The class constructor MUST handle the input validation
47+
* @param string $datetime Default value is NOW
48+
*/
4449
public function __construct(string $datetime = "now", ?DateTimeZone $timezone = null)
4550
{
4651
parent::__construct($datetime, $timezone);
@@ -68,10 +73,10 @@ public function __unserialize(array $data): void
6873

6974
/**
7075
* Init
71-
* @param string $value
76+
* @param mixed $value
7277
* @return self
7378
*/
74-
public static function value(string $value): self
79+
public static function value(mixed $value): self
7580
{
7681
$inst = new self($value);
7782
return $inst;

Format/Dom.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,55 @@
1111

1212
use PHPFuse\Output\Dom\Document;
1313
use PHPFuse\DTO\Format\FormatInterface;
14+
use InvalidArgumentException;
1415

1516
final class Dom extends FormatAbstract
1617
{
17-
protected $value;
18+
//protected $value;
1819
protected $dom;
1920
protected $str;
2021

21-
public static function value($value)
22+
/**
23+
* Input is mixed data type in the interface becouse I do not know the type before the class
24+
* The class constructor MUST handle the input validation
25+
* @param string $value
26+
*/
27+
public function __construct(mixed $value)
28+
{
29+
if (is_array($value) || is_object($value)) {
30+
throw new InvalidArgumentException("Is expecting a string or a convertable string value.", 1);
31+
}
32+
$this->value = (string)$value;
33+
}
34+
35+
/**
36+
* Static access
37+
* @param mixed $value
38+
* @return self
39+
*/
40+
public static function value(mixed $value): self
2241
{
2342
$inst = new static($value);
2443
$inst->dom = Document::dom("DTO");
2544
return $inst;
2645
}
2746

47+
/**
48+
* Get DOM if is modified
49+
* E.G. Will create an interface for the Document and Element class
50+
* @return object
51+
*/
52+
public function getDom(): object
53+
{
54+
return $this->dom;
55+
}
2856

29-
public function create(string $tag)
57+
/**
58+
* Create elemt with some default values
59+
* @param string $tag
60+
* @return object
61+
*/
62+
public function create(string $tag): object
3063
{
3164
if (is_array($this->value)) {
3265
$arr = $this->value;

Format/Encode.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,56 @@
44

55
final class Encode extends FormatAbstract implements FormatInterface
66
{
7-
protected $value;
7+
//protected $value;
88
protected $jsonEncode = true;
99
protected $urlencode = false;
1010

11+
/**
12+
* Input is mixed data type in the interface becouse I do not know the type before the class
13+
* The class constructor MUST handle the input validation
14+
* @param array|string $value
15+
*/
16+
public function __construct(array|string $value)
17+
{
18+
$this->value = $value;
19+
}
1120

1221
/**
1322
* Init format by adding data to modify/format/traverse
14-
* @param array $arr
23+
* @param mixed $value
1524
* @return self
1625
*/
17-
public static function value($value): FormatInterface
26+
public static function value(mixed $value): FormatInterface
1827
{
1928
$inst = new static($value);
2029
return $inst;
2130
}
2231

32+
/**
33+
* Url encode flag
34+
* @param bool $urlencode
35+
* @return self
36+
*/
2337
public function urlEncode(bool $urlencode = true): self
2438
{
2539
$this->urlencode = $urlencode;
2640
return $this;
2741
}
2842

2943
/**
30-
* XXS Protect the result
44+
* Encode values
45+
* @param callable|null $callback Access encode value with callable and build upon
46+
* @param int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401
3147
* @return self
3248
*/
33-
public function encode(?callable $callback = null): self
49+
public function encode(?callable $callback = null, int $flag = ENT_QUOTES): self
3450
{
3551
if (is_array($this->value)) {
36-
$this->value = Arr::value($this->value)->walk(function ($value) use ($callback) {
52+
$this->value = Arr::value($this->value)->walk(function ($value) use ($callback, $flag) {
3753
if (!is_null($callback)) {
3854
$value = $callback($value);
3955
}
40-
$uri = Str::value((string)$value)->encode();
56+
$uri = Str::value((string)$value)->encode($flag);
4157
if ($this->urlencode) {
4258
$uri->rawurlencode();
4359
}
@@ -47,7 +63,7 @@ public function encode(?callable $callback = null): self
4763
if (!is_null($callback)) {
4864
$this->value = $callback($this->value);
4965
}
50-
$this->value = Str::value($this->value)->encode()->get();
66+
$this->value = Str::value($this->value)->encode($flag)->get();
5167
}
5268

5369
return $this;

Format/FormatInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ interface FormatInterface
1414
{
1515
/**
1616
* Init format by adding data to modify/format/traverse
17-
* @param string $value
17+
* @param mixed $value
1818
* @return self
1919
*/
20-
public static function value(string $value);
20+
public static function value(mixed $value);
2121

2222
public function __toString();
2323

Format/Local.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ final class Local
2323

2424
/**
2525
* Init format by adding data to modify/format/traverse
26-
* @param array $arr
26+
* @param mixed $data
2727
* @return self
2828
*/
29-
public static function value(array|string $data)
29+
public static function value(mixed $data)
3030
{
3131

3232
if (is_string($data)) {
@@ -87,7 +87,7 @@ public function getValue(string $key): ?string
8787
return ($this->value[$key][$this::$prefix] ?? null);
8888
}
8989

90-
public function get(string|array $key, ?string $fallback = null, ?array $sprint = null): ?string
90+
public function get(string|array $key, string $fallback = "", ?array $sprint = null): ?string
9191
{
9292
if (is_null($this::$prefix)) {
9393
throw new Exception("Lang prefix is null.", 1);

Format/Num.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
2-
32
/**
3+
* DEPRECATED
44
* @Package: PHPFuse Format numbers
55
* @Author: Daniel Ronkainen
66
* @Licence: The MIT License (MIT), Copyright © Daniel Ronkainen
@@ -17,10 +17,10 @@ final class Num extends FormatAbstract implements FormatInterface
1717

1818
/**
1919
* Init format by adding data to modify/format/traverse
20-
* @param array $arr
20+
* @param mixed $value
2121
* @return self
2222
*/
23-
public static function value($value): FormatInterface
23+
public static function value(mixed $value): FormatInterface
2424
{
2525
$inst = new static($value);
2626
return $inst;

Format/Str.php

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,39 @@
99

1010
namespace PHPFuse\DTO\Format;
1111

12+
use InvalidArgumentException;
13+
1214
final class Str extends FormatAbstract implements FormatInterface
1315
{
14-
protected $value;
16+
//protected $value;
1517

18+
/**
19+
* Input is mixed data type in the interface becouse I do not know the type before the class
20+
* The class constructor MUST handle the input validation
21+
* @param string $value
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+
$this->value = (string)$value;
29+
}
1630

1731
/**
1832
* Init format by adding data to modify/format/traverse
19-
* @param array $arr
33+
* @param mixed $value
2034
* @return self
2135
*/
22-
public static function value($value): FormatInterface
36+
public static function value(mixed $value): FormatInterface
2337
{
24-
$inst = new static($value);
38+
$inst = new static((string)$value);
2539
return $inst;
2640
}
2741

2842

2943
/**
30-
* Gte value as string
44+
* Get value as string
3145
* @return string
3246
*/
3347
public function strVal(): string
@@ -44,9 +58,9 @@ public function strVal(): string
4458
public function excerpt($length = 40, $ending = "..."): self
4559
{
4660
$this->stripTags()->entityDecode();
47-
$this->value = str_replace(array("'", '"', ""), array("", "", ""), $this->value);
48-
$strlen = strlen($this->value);
49-
$this->value = trim(mb_substr($this->value, 0, $length));
61+
$this->value = str_replace(array("'", '"', ""), array("", "", ""), $this->strVal());
62+
$strlen = strlen($this->strVal());
63+
$this->value = trim(mb_substr($this->strVal(), 0, $length));
5064
if ($strlen > $length) {
5165
$this->value .= $ending;
5266
}
@@ -86,31 +100,38 @@ public function stripTags(string $whitelist = ""): self
86100

87101
/**
88102
* Cleans GET/POST data (XSS protection)
103+
* In most cases I want to encode dubble and single quotes but when it comes to
104+
* escaping value in DB the i rather escape quoutes the mysql way
105+
* @param int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401|null
89106
* @return self
90107
*/
91-
public function specialchars(): self
108+
public function specialchars(int $flag = ENT_QUOTES): self
92109
{
93-
$this->value = htmlspecialchars($this->strVal(), ENT_QUOTES, 'UTF-8');
110+
$this->value = htmlspecialchars($this->strVal(), $flag, 'UTF-8');
94111
return $this;
95112
}
96113

97114
/**
98115
* Cleans GET/POST data (XSS protection)
116+
* In most cases I want to encode dubble and single quotes but when it comes to
117+
* escaping value in DB the i rather escape quoutes the mysql way
118+
* @param int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401|null
99119
* @return self
100120
*/
101-
public function encode(): self
121+
public function encode(int $flag = ENT_QUOTES): self
102122
{
103-
$this->specialchars();
123+
$this->specialchars($flag);
104124
return $this;
105125
}
106126

107127
/**
108128
* Decode html special characters
129+
* @param ?int $flag ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401|null
109130
* @return self
110131
*/
111-
public function decode(): self
132+
public function decode(?int $flag = ENT_QUOTES): self
112133
{
113-
$this->value = htmlspecialchars_decode($this->strVal(), ENT_QUOTES);
134+
$this->value = htmlspecialchars_decode($this->strVal(), $flag);
114135
return $this;
115136
}
116137

@@ -302,6 +323,9 @@ public function rawurlencode(?array $find = null, ?array $replace = null): self
302323
public function replace($find, $replace): self
303324
{
304325
$this->value = str_replace($find, $replace, $this->strVal());
326+
if(!is_string($this->value)) {
327+
throw new InvalidArgumentException("The value has to be an string value!", 1);
328+
}
305329
return $this;
306330
}
307331

@@ -315,11 +339,10 @@ public function toggleUrlencode(?array $find = null, ?array $replace = null): se
315339
{
316340
return $this->urldecode()->rawurlencode($find, $replace);
317341
}
318-
319-
320-
342+
321343
/**
322344
* Explode return array instance
345+
* @param non-empty-string $delimiter
323346
* @return Arr
324347
*/
325348
public function explode(string $delimiter): Arr
@@ -385,4 +408,34 @@ public function xxs(): self
385408
}
386409
return $this;
387410
}
411+
412+
/**
413+
* To bool value
414+
* @return bool
415+
*/
416+
public function toBool(): bool
417+
{
418+
if(is_numeric($this->value)) {
419+
return ((float)$this->value > 0);
420+
}
421+
return ($this->value !== "false" && strlen($this->value));
422+
}
423+
424+
/**
425+
* To int value
426+
* @return int
427+
*/
428+
public function toInt(): int
429+
{
430+
return (int)$this->value;
431+
}
432+
433+
/**
434+
* To float value
435+
* @return float
436+
*/
437+
public function toFloat(): float
438+
{
439+
return (float)$this->value;
440+
}
388441
}

0 commit comments

Comments
 (0)