-
Notifications
You must be signed in to change notification settings - Fork 774
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The standards `CompositeStringifier` from "respect/stringifier" has lots of interesting stringifiers. However, this library is not 100% focused on engineers. Someone could type a string that matches a callable, and then you will overexpose the system. This commit makes sure that callables are not interpreted as callables.
- Loading branch information
1 parent
a3c197f
commit 0cdd57b
Showing
9 changed files
with
131 additions
and
14 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
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,92 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Message; | ||
|
||
use DateTimeInterface; | ||
use Respect\Stringifier\Quoter; | ||
use Respect\Stringifier\Quoters\StandardQuoter; | ||
use Respect\Stringifier\Stringifier; | ||
use Respect\Stringifier\Stringifiers\ArrayObjectStringifier; | ||
use Respect\Stringifier\Stringifiers\ArrayStringifier; | ||
use Respect\Stringifier\Stringifiers\BoolStringifier; | ||
use Respect\Stringifier\Stringifiers\CompositeStringifier; | ||
use Respect\Stringifier\Stringifiers\DateTimeStringifier; | ||
use Respect\Stringifier\Stringifiers\DeclaredStringifier; | ||
use Respect\Stringifier\Stringifiers\EnumerationStringifier; | ||
use Respect\Stringifier\Stringifiers\InfiniteNumberStringifier; | ||
use Respect\Stringifier\Stringifiers\IteratorObjectStringifier; | ||
use Respect\Stringifier\Stringifiers\JsonEncodableStringifier; | ||
use Respect\Stringifier\Stringifiers\JsonSerializableObjectStringifier; | ||
use Respect\Stringifier\Stringifiers\NotANumberStringifier; | ||
use Respect\Stringifier\Stringifiers\NullStringifier; | ||
use Respect\Stringifier\Stringifiers\ObjectStringifier; | ||
use Respect\Stringifier\Stringifiers\ObjectWithDebugInfoStringifier; | ||
use Respect\Stringifier\Stringifiers\ResourceStringifier; | ||
use Respect\Stringifier\Stringifiers\StringableObjectStringifier; | ||
use Respect\Stringifier\Stringifiers\ThrowableObjectStringifier; | ||
|
||
final class StandardStringifier implements Stringifier | ||
{ | ||
private const MAXIMUM_DEPTH = 3; | ||
private const MAXIMUM_NUMBER_OF_ITEMS = 5; | ||
private const MAXIMUM_NUMBER_OF_PROPERTIES = self::MAXIMUM_NUMBER_OF_ITEMS; | ||
private const MAXIMUM_LENGTH = 120; | ||
|
||
private readonly Stringifier $stringifier; | ||
|
||
public function __construct( | ||
private readonly Quoter $quoter = new StandardQuoter(self::MAXIMUM_LENGTH) | ||
) { | ||
$this->stringifier = $this->createStringifier($quoter); | ||
} | ||
|
||
public function stringify(mixed $raw, int $depth): string | ||
{ | ||
return $this->stringifier->stringify($raw, $depth) ?? $this->quoter->quote('unknown', $depth); | ||
} | ||
|
||
private function createStringifier(Quoter $quoter): Stringifier | ||
{ | ||
$jsonEncodableStringifier = new JsonEncodableStringifier(); | ||
|
||
$stringifier = new CompositeStringifier( | ||
new InfiniteNumberStringifier($quoter), | ||
new NotANumberStringifier($quoter), | ||
new ResourceStringifier($quoter), | ||
new BoolStringifier($quoter), | ||
new NullStringifier($quoter), | ||
new DeclaredStringifier($quoter), | ||
$jsonEncodableStringifier, | ||
); | ||
$arrayStringifier = new ArrayStringifier( | ||
$stringifier, | ||
$quoter, | ||
self::MAXIMUM_DEPTH, | ||
self::MAXIMUM_NUMBER_OF_ITEMS, | ||
); | ||
$stringifier->prependStringifier($arrayStringifier); | ||
$stringifier->prependStringifier(new ObjectStringifier( | ||
$stringifier, | ||
$quoter, | ||
self::MAXIMUM_DEPTH, | ||
self::MAXIMUM_NUMBER_OF_PROPERTIES | ||
)); | ||
$stringifier->prependStringifier(new EnumerationStringifier($quoter)); | ||
$stringifier->prependStringifier(new ObjectWithDebugInfoStringifier($arrayStringifier, $quoter)); | ||
$stringifier->prependStringifier(new ArrayObjectStringifier($arrayStringifier, $quoter)); | ||
$stringifier->prependStringifier(new JsonSerializableObjectStringifier($jsonEncodableStringifier, $quoter)); | ||
$stringifier->prependStringifier(new StringableObjectStringifier($jsonEncodableStringifier, $quoter)); | ||
$stringifier->prependStringifier(new ThrowableObjectStringifier($jsonEncodableStringifier, $quoter)); | ||
$stringifier->prependStringifier(new DateTimeStringifier($quoter, DateTimeInterface::ATOM)); | ||
$stringifier->prependStringifier(new IteratorObjectStringifier($stringifier, $quoter)); | ||
|
||
return $stringifier; | ||
} | ||
} |
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
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
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,28 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Message; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Respect\Validation\Test\TestCase; | ||
|
||
#[CoversClass(StandardStringifier::class)] | ||
final class StandardStringifierTest extends TestCase | ||
{ | ||
#[Test] | ||
public function itShouldReturnQuotedUnknownWhenTheTypeIsUnknown(): void | ||
{ | ||
$resource = tmpfile(); | ||
fclose($resource); | ||
|
||
$stringifier = new StandardStringifier(); | ||
self::assertEquals('`unknown`', $stringifier->stringify($resource, 0)); | ||
} | ||
} |