Skip to content

Commit 52bbc6e

Browse files
committed
Create NanStringifier
1 parent 3393615 commit 52bbc6e

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-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_nan;
18+
use Respect\Stringifier\Quoter;
19+
use Respect\Stringifier\Stringifier;
20+
21+
/**
22+
* Converts a NaN value into a string.
23+
*
24+
* @author Henrique Moody <[email protected]>
25+
*/
26+
final class NanStringifier 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_nan($raw)) {
53+
return null;
54+
}
55+
56+
return $this->quoter->quote('NaN', $depth);
57+
}
58+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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\NanStringifier;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* @covers \Respect\Stringifier\Stringifiers\NanStringifier
22+
*
23+
* @author Henrique Moody <[email protected]>
24+
*/
25+
final class NanStringifierTest extends TestCase
26+
{
27+
/**
28+
* @test
29+
*/
30+
public function shouldNotConvertToStringWhenRawValueIsNotAFloat(): void
31+
{
32+
$raw = 'string';
33+
$depth = 0;
34+
35+
$quoterMock = $this->createMock(Quoter::class);
36+
$quoterMock
37+
->expects($this->never())
38+
->method('quote');
39+
40+
$nanStringifier = new NanStringifier($quoterMock);
41+
42+
self::assertNull($nanStringifier->stringify($raw, $depth));
43+
}
44+
45+
/**
46+
* @test
47+
*/
48+
public function shouldNotConvertToStringWhenRawValueIsANumber(): void
49+
{
50+
$raw = 1.00000000002;
51+
$depth = 0;
52+
53+
$quoterMock = $this->createMock(Quoter::class);
54+
$quoterMock
55+
->expects($this->never())
56+
->method('quote');
57+
58+
$nanStringifier = new NanStringifier($quoterMock);
59+
60+
self::assertNull($nanStringifier->stringify($raw, $depth));
61+
}
62+
63+
/**
64+
* @test
65+
*/
66+
public function shouldConvertToStringWhenRawValueIsNotANumber(): void
67+
{
68+
$raw = acos(8);
69+
$depth = 0;
70+
71+
$expected = 'NaN';
72+
73+
$quoterMock = $this->createMock(Quoter::class);
74+
$quoterMock
75+
->expects($this->once())
76+
->method('quote')
77+
->with($expected, $depth)
78+
->willReturn($expected);
79+
80+
$nanStringifier = new NanStringifier($quoterMock);
81+
82+
self::assertSame($expected, $nanStringifier->stringify($raw, $depth));
83+
}
84+
}

0 commit comments

Comments
 (0)