Skip to content

Commit 9e25a31

Browse files
committed
Create ResourceStringifier
1 parent 52bbc6e commit 9e25a31

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 get_resource_type;
17+
use function is_resource;
18+
use function sprintf;
19+
use Respect\Stringifier\Quoter;
20+
use Respect\Stringifier\Stringifier;
21+
22+
/**
23+
* Converts a resource value into a string.
24+
*
25+
* @author Henrique Moody <[email protected]>
26+
*/
27+
final class ResourceStringifier implements Stringifier
28+
{
29+
/**
30+
* @var Quoter
31+
*/
32+
private $quoter;
33+
34+
/**
35+
* Initializes the stringifier.
36+
*
37+
* @param Quoter $quoter
38+
*/
39+
public function __construct(Quoter $quoter)
40+
{
41+
$this->quoter = $quoter;
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function stringify($raw, int $depth): ?string
48+
{
49+
if (!is_resource($raw)) {
50+
return null;
51+
}
52+
53+
return $this->quoter->quote(
54+
sprintf(
55+
'[resource] (%s)',
56+
get_resource_type($raw)
57+
),
58+
$depth
59+
);
60+
}
61+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 tmpfile;
17+
use Respect\Stringifier\Quoter;
18+
use Respect\Stringifier\Stringifiers\ResourceStringifier;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* @covers \Respect\Stringifier\Stringifiers\ResourceStringifier
23+
*
24+
* @author Henrique Moody <[email protected]>
25+
*/
26+
final class ResourceStringifierTest extends TestCase
27+
{
28+
/**
29+
* @test
30+
*/
31+
public function shouldNotConvertToStringWhenRawValueIsNotAResource(): void
32+
{
33+
$raw = true;
34+
$depth = 0;
35+
36+
$quoterMock = $this->createMock(Quoter::class);
37+
$quoterMock
38+
->expects($this->never())
39+
->method('quote');
40+
41+
$resourceStringifier = new ResourceStringifier($quoterMock);
42+
43+
self::assertNull($resourceStringifier->stringify($raw, $depth));
44+
}
45+
46+
/**
47+
* @test
48+
*/
49+
public function shouldConvertToStringWhenRawValueIsNotAResource(): void
50+
{
51+
$raw = tmpfile();
52+
$depth = 0;
53+
54+
$expected = '[resource] (stream)';
55+
56+
$quoterMock = $this->createMock(Quoter::class);
57+
$quoterMock
58+
->expects($this->once())
59+
->method('quote')
60+
->with($expected, $depth)
61+
->willReturn($expected);
62+
63+
$resourceStringifier = new ResourceStringifier($quoterMock);
64+
65+
self::assertSame($expected, $resourceStringifier->stringify($raw, $depth));
66+
}
67+
}

0 commit comments

Comments
 (0)