Skip to content

Commit f438e97

Browse files
committed
Se agrega helper Encoding que permite aplicar utf8encode y utf8decode a string, array y object.
1 parent 510da1c commit f438e97

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

src/Encoding.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ final class Encoding
2424
*
2525
* Only converts strings that are actually UTF-8 encoded.
2626
*
27-
* @param string|array|object $input
28-
* @return string|array|object
27+
* @param mixed $input
28+
* @return mixed
2929
*/
30-
public static function utf8decode(string|array|object $input): string|array|object
30+
public static function utf8decode(mixed $input): mixed
3131
{
3232
if (is_string($input)) {
3333
if (empty($input) || !mb_detect_encoding($input, 'UTF-8', true)) {
@@ -44,9 +44,13 @@ public static function utf8decode(string|array|object $input): string|array|obje
4444
return array_map([self::class, 'utf8decode'], $input);
4545
}
4646

47-
foreach ($input as $key => $value) {
48-
$input->$key = self::utf8decode($value);
47+
if (is_object($input)) {
48+
foreach ($input as $key => $value) {
49+
$input->$key = self::utf8decode($value);
50+
}
51+
return $input;
4952
}
53+
5054
return $input;
5155
}
5256

@@ -55,10 +59,10 @@ public static function utf8decode(string|array|object $input): string|array|obje
5559
*
5660
* Only converts strings that are actually ISO-8859-1 encoded.
5761
*
58-
* @param string|array|object $input
59-
* @return string|array|object
62+
* @param mixed $input
63+
* @return mixed
6064
*/
61-
public static function utf8encode(string|array|object $input): string|array|object
65+
public static function utf8encode(mixed $input): mixed
6266
{
6367
if (is_string($input)) {
6468
if (empty($input) || !mb_detect_encoding($input, 'ISO-8859-1', true)) {
@@ -75,9 +79,13 @@ public static function utf8encode(string|array|object $input): string|array|obje
7579
return array_map([self::class, 'utf8encode'], $input);
7680
}
7781

78-
foreach ($input as $key => $value) {
79-
$input->$key = self::utf8encode($value);
82+
if (is_object($input)) {
83+
foreach ($input as $key => $value) {
84+
$input->$key = self::utf8encode($value);
85+
}
86+
return $input;
8087
}
88+
8189
return $input;
8290
}
8391
}

0 commit comments

Comments
 (0)