Skip to content

Commit 9e18cf0

Browse files
committed
Create InfiniteStringifier
1 parent 81e2f24 commit 9e18cf0

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 is_float;
17+
use function is_infinite;
18+
use Respect\Stringifier\Quoter;
19+
use Respect\Stringifier\Stringifier;
20+
21+
/**
22+
* Converts an infinite float value into a string.
23+
*
24+
* @author Henrique Moody <[email protected]>
25+
*/
26+
final class InfiniteStringifier implements Stringifier
27+
{
28+
/**
29+
* @var Quoter
30+
*/
31+
private $quoter;
32+
33+
/**
34+
* Initializes the stringifier.
35+
*
36+
* @param Quoter $quoter
37+
*/
38+
public function __construct(Quoter $quoter)
39+
{
40+
$this->quoter = $quoter;
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function stringify($raw, int $depth): ?string
47+
{
48+
if (!is_float($raw)) {
49+
return null;
50+
}
51+
52+
if (!is_infinite($raw)) {
53+
return null;
54+
}
55+
56+
return $this->quoter->quote(($raw > 0 ? '' : '-').'INF', $depth);
57+
}
58+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 Respect\Stringifier\Quoter;
17+
use Respect\Stringifier\Stringifiers\InfiniteStringifier;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* @covers \Respect\Stringifier\Stringifiers\InfiniteStringifier
22+
*
23+
* @author Henrique Moody <[email protected]>
24+
*/
25+
final class InfiniteStringifierTest extends TestCase
26+
{
27+
/**
28+
* @test
29+
*/
30+
public function shouldNotConvertToStringWhenRawValueIsNotFloat(): void
31+
{
32+
$raw = 1;
33+
$depth = 0;
34+
35+
$quoterMock = $this->createMock(Quoter::class);
36+
$quoterMock
37+
->expects($this->never())
38+
->method('quote');
39+
40+
$infiniteStringifier = new InfiniteStringifier($quoterMock);
41+
42+
self::assertNull($infiniteStringifier->stringify($raw, $depth));
43+
}
44+
45+
/**
46+
* @test
47+
*/
48+
public function shouldNotConvertToStringWhenRawValueIsFiniteFloat(): void
49+
{
50+
$raw = 1.0;
51+
$depth = 0;
52+
53+
$quoterMock = $this->createMock(Quoter::class);
54+
$quoterMock
55+
->expects($this->never())
56+
->method('quote');
57+
58+
$infiniteStringifier = new InfiniteStringifier($quoterMock);
59+
60+
self::assertNull($infiniteStringifier->stringify($raw, $depth));
61+
}
62+
63+
/**
64+
* @test
65+
*/
66+
public function shouldConvertToStringWhenRawValueIsInfinitePositive(): void
67+
{
68+
$raw = INF;
69+
$depth = 0;
70+
71+
$expected = '1.0';
72+
73+
$quoterMock = $this->createMock(Quoter::class);
74+
$quoterMock
75+
->expects($this->once())
76+
->method('quote')
77+
->with($raw, $depth)
78+
->willReturn($expected);
79+
80+
$infiniteStringifier = new InfiniteStringifier($quoterMock);
81+
82+
self::assertSame($expected, $infiniteStringifier->stringify($raw, $depth));
83+
}
84+
85+
/**
86+
* @test
87+
*/
88+
public function shouldConvertToStringWhenRawValueIsInfiniteNegative(): void
89+
{
90+
$raw = -1 * INF;
91+
$depth = 0;
92+
93+
$expected = '-1.0';
94+
95+
$quoterMock = $this->createMock(Quoter::class);
96+
$quoterMock
97+
->expects($this->once())
98+
->method('quote')
99+
->with($raw, $depth)
100+
->willReturn($expected);
101+
102+
$infiniteStringifier = new InfiniteStringifier($quoterMock);
103+
104+
self::assertSame($expected, $infiniteStringifier->stringify($raw, $depth));
105+
}
106+
}

0 commit comments

Comments
 (0)