Skip to content

Commit 5738496

Browse files
committed
Create stringify() function
1 parent e62e07b commit 5738496

15 files changed

+242
-1
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
"autoload": {
2323
"psr-4": {
2424
"Respect\\Stringifier\\": "src/"
25-
}
25+
},
26+
"files": [
27+
"src/stringify.php"
28+
]
2629
},
2730
"scripts": {
2831
"docheader": "vendor/bin/docheader check src/ tests/",

src/stringify.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Respect/Stringifier.
5+
*
6+
* (c) Henrique Moody <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the "LICENSE.md"
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Respect\Stringifier;
15+
16+
use Respect\Stringifier\Stringifiers\ClusterStringifier;
17+
18+
function stringify($value): string
19+
{
20+
static $stringifier;
21+
22+
if (null === $stringifier) {
23+
$stringifier = ClusterStringifier::createDefault();
24+
}
25+
26+
return $stringifier->stringify($value, 0) ?? '#ERROR#';
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--FILE--
2+
<?php
3+
require 'vendor/autoload.php';
4+
5+
use function Respect\Stringifier\stringify;
6+
7+
echo stringify([
8+
1,
9+
null,
10+
[
11+
1.000,
12+
[
13+
tmpfile(),
14+
[
15+
1
16+
]
17+
]
18+
],
19+
false,
20+
new stdClass(),
21+
42
22+
]);
23+
?>
24+
--EXPECT--
25+
{ 1, NULL, { 1.0, { [resource] (stream), ... } }, FALSE, [object] (stdClass: { }), ... }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--FILE--
2+
<?php
3+
require 'vendor/autoload.php';
4+
5+
use function Respect\Stringifier\stringify;
6+
7+
echo implode(
8+
PHP_EOL,
9+
[
10+
stringify(true),
11+
stringify(false),
12+
]
13+
);
14+
?>
15+
--EXPECT--
16+
`TRUE`
17+
`FALSE`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--FILE--
2+
<?php
3+
require 'vendor/autoload.php';
4+
5+
use function Respect\Stringifier\stringify;
6+
7+
echo implode(
8+
PHP_EOL,
9+
[
10+
stringify(1.0),
11+
stringify(.3),
12+
stringify(INF),
13+
stringify(-1*INF),
14+
stringify(acos(8)),
15+
]
16+
);
17+
?>
18+
--EXPECT--
19+
1.0
20+
0.3
21+
`INF`
22+
`-INF`
23+
`NaN`
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--FILE--
2+
<?php
3+
require 'vendor/autoload.php';
4+
5+
use function Respect\Stringifier\stringify;
6+
7+
echo stringify(1);
8+
?>
9+
--EXPECT--
10+
1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--FILE--
2+
<?php
3+
require 'vendor/autoload.php';
4+
5+
use function Respect\Stringifier\stringify;
6+
7+
echo stringify(NULL);
8+
?>
9+
--EXPECT--
10+
`NULL`
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--FILE--
2+
<?php
3+
require 'vendor/autoload.php';
4+
5+
use function Respect\Stringifier\stringify;
6+
7+
$dateTime = DateTime::createFromFormat('Y-m-d\TH:i:sP', '2017-12-31T23:59:59+00:00');
8+
9+
echo stringify($dateTime);
10+
?>
11+
--EXPECT--
12+
"2017-12-31T23:59:59+00:00"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--FILE--
2+
<?php
3+
require 'vendor/autoload.php';
4+
5+
use function Respect\Stringifier\stringify;
6+
7+
final class MyObject implements JsonSerializable
8+
{
9+
public function jsonSerialize(): array
10+
{
11+
return [1, 2, 3];
12+
}
13+
}
14+
15+
echo stringify(new MyObject());
16+
?>
17+
--EXPECT--
18+
`[json-serializable] (MyObject: { 1, 2, 3 })`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--FILE--
2+
<?php
3+
require 'vendor/autoload.php';
4+
5+
use function Respect\Stringifier\stringify;
6+
7+
final class MyObject
8+
{
9+
public function __toString(): string
10+
{
11+
return __METHOD__;
12+
}
13+
}
14+
15+
echo stringify(new MyObject());
16+
?>
17+
--EXPECT--
18+
"MyObject::__toString"

0 commit comments

Comments
 (0)