mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 07:15:45 +01:00
The `InterpolationRenderer` was violating the open-closed principle, because every time we would want to add a new modifier, we would need to change its implementation. This commit changes that behaviour by creating a `Modifier` interface. The classes implementing that interface are using a chain of responsibility to pass the data to the next one. Using a chain of responsibility makes a lot of sense, since it's only possible to have one modifier at a time.
36 lines
783 B
PHP
36 lines
783 B
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Message\Modifier;
|
|
|
|
use Respect\Validation\Message\Modifier;
|
|
|
|
use function is_bool;
|
|
use function is_scalar;
|
|
|
|
final readonly class RawModifier implements Modifier
|
|
{
|
|
public function __construct(
|
|
private Modifier $nextModifier,
|
|
) {
|
|
}
|
|
|
|
public function modify(mixed $value, string|null $pipe): string
|
|
{
|
|
if ($pipe !== 'raw') {
|
|
return $this->nextModifier->modify($value, $pipe);
|
|
}
|
|
|
|
if (!is_scalar($value)) {
|
|
return $this->nextModifier->modify($value, null);
|
|
}
|
|
|
|
return is_bool($value) ? (string) (int) $value : (string) $value;
|
|
}
|
|
}
|