Skip to content

Commit 945f29c

Browse files
committed
new: add new method toString convert any value to string
1 parent 7bab2e5 commit 945f29c

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

src/Helper/DataHelper.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
namespace Toolkit\Stdlib\Helper;
44

55
use function filter_var;
6+
use function gettype;
7+
use function is_array;
8+
use function is_bool;
9+
use function is_object;
610
use function is_scalar;
11+
use function json_encode;
12+
use function method_exists;
713
use const FILTER_NULL_ON_FAILURE;
814
use const FILTER_VALIDATE_BOOLEAN;
915

@@ -46,4 +52,37 @@ public static function toBool($val, $nullAsFalse = false): bool
4652
{
4753
return self::boolean($val, $nullAsFalse);
4854
}
55+
56+
/**
57+
* @param mixed $val
58+
*
59+
* @return string
60+
*/
61+
public static function toString($val): string
62+
{
63+
// print_r($value)
64+
// var_export($value)
65+
if (is_scalar($val)) {
66+
if (is_bool($val)) {
67+
return $val ? 'bool(TRUE)' : 'bool(FALSE)';
68+
}
69+
70+
return (string)$val;
71+
}
72+
73+
if (is_array($val)) {
74+
return json_encode($val);
75+
}
76+
77+
if (is_object($val)) {
78+
if (method_exists($val, '__toString')) {
79+
return (string)$val;
80+
}
81+
82+
return PhpHelper::dumpVars($val);
83+
}
84+
85+
$typeName = gettype($val);
86+
return '<' . $typeName . '>';
87+
}
4988
}

src/Helper/JsonHelper.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ class JsonHelper
3939
// ----------- encode -----------
4040

4141
/**
42-
* @param $data
42+
* @param mixed $data
43+
* @param int $flags
44+
* @param int $depth
4345
*
4446
* @return string
4547
*/
46-
public static function enc($data): string
48+
public static function enc($data, int $flags = 0, int $depth = 512): string
4749
{
48-
return json_encode($data);
50+
return json_encode($data, $flags, $depth);
4951
}
5052

5153
/**

test/Helper/DataHelperTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,24 @@ public function testToBool(): void
2222
self::assertFalse(DataHelper::toBool('off'));
2323
self::assertFalse(DataHelper::toBool('0'));
2424
}
25+
26+
public function testToString(): void
27+
{
28+
self::assertEquals('1.2', DataHelper::toString(1.2));
29+
self::assertEquals('12', DataHelper::toString(12));
30+
self::assertEquals('abc', DataHelper::toString('abc'));
31+
self::assertEquals('bool(TRUE)', DataHelper::toString(true));
32+
self::assertEquals('bool(FALSE)', DataHelper::toString(false));
33+
self::assertEquals('<NULL>', DataHelper::toString(null));
34+
self::assertEquals('["ab",23]', DataHelper::toString(['ab', 23]));
35+
36+
$objStr = <<<OBJ
37+
object(stdClass)#73 (2) {
38+
["0"]=> string(2) "ab"
39+
["1"]=> int(23)
40+
}
41+
42+
OBJ;
43+
self::assertEquals($objStr, DataHelper::toString((object)['ab', 23]));
44+
}
2545
}

0 commit comments

Comments
 (0)