-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a stringifier for "quoted" values
The `StandardQuoter` adds backticks around strings, which indicates that it's not a simple string but a code. With this stringifier, we can add quotes to placeholders directly into templates.
- Loading branch information
1 parent
b8f4949
commit 84a0136
Showing
7 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Message\Placeholder; | ||
|
||
final class Quoted | ||
{ | ||
public function __construct( | ||
private readonly string $value | ||
) { | ||
} | ||
|
||
public function getValue(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
namespace Respect\Validation\Message\Stringifier; | ||
|
||
use Respect\Stringifier\Quoter; | ||
use Respect\Stringifier\Stringifier; | ||
use Respect\Validation\Message\Placeholder\Quoted; | ||
|
||
final class QuotedStringifier implements Stringifier | ||
{ | ||
public function __construct( | ||
private readonly Quoter $quoter | ||
) { | ||
} | ||
|
||
public function stringify(mixed $raw, int $depth): ?string | ||
{ | ||
if (!$raw instanceof Quoted) { | ||
return null; | ||
} | ||
|
||
return $this->quoter->quote($raw->getValue(), $depth); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Message\Stringifier; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Respect\Stringifier\Quoters\StandardQuoter; | ||
use Respect\Validation\Message\Placeholder\Quoted; | ||
use Respect\Validation\Test\TestCase; | ||
|
||
#[CoversClass(QuotedStringifier::class)] | ||
final class QuotedStringifierTest extends TestCase | ||
{ | ||
#[Test] | ||
#[DataProvider('providerForAnyValues')] | ||
public function itShouldNotStringifyWhenValueIsNotAnInstanceOfQuoted(mixed $value): void | ||
{ | ||
$quoter = new StandardQuoter(1); | ||
$stringifier = new QuotedStringifier($quoter); | ||
|
||
self::assertNull($stringifier->stringify($value, 0)); | ||
} | ||
|
||
#[Test] | ||
#[DataProvider('providerForStringTypes')] | ||
public function itShouldStringifyWhenValueIsAnInstanceOfQuoted(string $value): void | ||
{ | ||
$quoted = new Quoted($value); | ||
$quoter = new StandardQuoter(1); | ||
$stringifier = new QuotedStringifier($quoter); | ||
|
||
$expected = $quoter->quote($quoted->getValue(), 0); | ||
$actual = $stringifier->stringify($quoted, 0); | ||
|
||
self::assertSame($expected, $actual); | ||
} | ||
} |