Skip to content

Commit bfec8cb

Browse files
committed
Create PhpParsableStringifier
1 parent f2fa601 commit bfec8cb

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Stringifiers;
15+
16+
use function var_export;
17+
use Respect\Stringifier\Stringifier;
18+
19+
/**
20+
* Converts any value into PHP parsable string representation.
21+
*
22+
* @author Henrique Moody <[email protected]>
23+
*/
24+
final class PhpParsableStringifier implements Stringifier
25+
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function stringify($raw, int $depth): ?string
30+
{
31+
$string = var_export($raw, true);
32+
if (null !== $raw && 'NULL' === $string) {
33+
return null;
34+
}
35+
36+
return $string;
37+
}
38+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\Test\Stringifiers;
15+
16+
use function implode;
17+
use const PHP_EOL;
18+
use Respect\Stringifier\Stringifiers\PhpParsableStringifier;
19+
use PHPUnit\Framework\TestCase;
20+
use stdClass;
21+
22+
/**
23+
* @covers \Respect\Stringifier\Stringifiers\PhpParsableStringifier
24+
*
25+
* @author Henrique Moody <[email protected]>
26+
*/
27+
final class PhpParsableStringifierTest extends TestCase
28+
{
29+
/**
30+
* @test
31+
*
32+
* @dataProvider validValuesProvider
33+
*
34+
* @param mixed $raw
35+
* @param string $expected
36+
*/
37+
public function shouldConvertValueToPhpParsable($raw, string $expected): void
38+
{
39+
$stringifier = new PhpParsableStringifier();
40+
41+
self::assertSame($expected, $stringifier->stringify($raw, 0));
42+
}
43+
44+
public function validValuesProvider(): array
45+
{
46+
return [
47+
[true, 'true'],
48+
[false, 'false'],
49+
[42, '42'],
50+
[1.2, '1.2'],
51+
[.2, '0.2'],
52+
['string', '\'string\''],
53+
[null, 'NULL'],
54+
[[1], implode(PHP_EOL, ['array (', ' 0 => 1,', ')'])],
55+
[new stdClass(), implode(PHP_EOL, ['stdClass::__set_state(array(', '))'])],
56+
];
57+
}
58+
59+
/**
60+
* @test
61+
*/
62+
public function shouldReturnNullWhenNotPossibleToConvert(): void
63+
{
64+
$stringifier = new PhpParsableStringifier();
65+
66+
self::assertNull($stringifier->stringify(tmpfile(), 0));
67+
}
68+
}

0 commit comments

Comments
 (0)