Skip to content

Commit f2fa601

Browse files
committed
Create CodeQuoter
1 parent 95887e3 commit f2fa601

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/Quoters/CodeQuoter.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Quoters;
15+
16+
use Respect\Stringifier\Quoter;
17+
18+
/**
19+
* Add "`" quotes around a string depending on its level.
20+
*
21+
* @author Henrique Moody <[email protected]>
22+
*/
23+
final class CodeQuoter implements Quoter
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function quote(string $string, int $depth): string
29+
{
30+
if (0 === $depth) {
31+
return sprintf('`%s`', $string);
32+
}
33+
34+
return $string;
35+
}
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Quoters;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Respect\Stringifier\Quoters\CodeQuoter;
18+
19+
/**
20+
* @covers \Respect\Stringifier\Quoters\CodeQuoter
21+
*
22+
* @author Henrique Moody <[email protected]>
23+
*/
24+
final class CodeQuoterTest extends TestCase
25+
{
26+
/**
27+
* @test
28+
*/
29+
public function shouldQuoteStringWhenInZeroDepth(): void
30+
{
31+
$quoter = new CodeQuoter();
32+
33+
$expectedValue = '`code`';
34+
$actualValue = $quoter->quote('code', 0);
35+
36+
self::assertSame($expectedValue, $actualValue);
37+
}
38+
39+
/**
40+
* @test
41+
*/
42+
public function shouldNotQuoteStringDepthIsBiggerThanZero(): void
43+
{
44+
$quoter = new CodeQuoter();
45+
46+
$expectedValue = 'code';
47+
$actualValue = $quoter->quote('code', 1);
48+
49+
self::assertSame($expectedValue, $actualValue);
50+
}
51+
}

0 commit comments

Comments
 (0)