-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract parameter processor into multiple classes
By extracting them into different processors, the code in the rendered becomes much more straightforward, and the design makes it possible to create processors easily. This change will allow users to customize that behavior if they want to. Signed-off-by: Henrique Moody <[email protected]>
- Loading branch information
1 parent
955a554
commit d25557c
Showing
17 changed files
with
409 additions
and
147 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,15 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Message\Parameter; | ||
|
||
interface Processor | ||
{ | ||
public function process(string $name, mixed $value, ?string $modifier = null): string; | ||
} |
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,30 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Message\Parameter; | ||
|
||
use function is_bool; | ||
use function is_scalar; | ||
|
||
final class Raw implements Processor | ||
{ | ||
public function __construct( | ||
private readonly Processor $nextProcessor, | ||
) { | ||
} | ||
|
||
public function process(string $name, mixed $value, ?string $modifier = null): string | ||
{ | ||
if ($modifier === 'raw' && is_scalar($value)) { | ||
return is_bool($value) ? (string) (int) $value : (string) $value; | ||
} | ||
|
||
return $this->nextProcessor->process($name, $value, $modifier); | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Message\Parameter; | ||
|
||
use function call_user_func; | ||
use function is_string; | ||
|
||
final class Trans implements Processor | ||
{ | ||
/** @var callable */ | ||
private $translator; | ||
|
||
public function __construct( | ||
callable $translator, | ||
private readonly Processor $nextProcessor, | ||
) { | ||
$this->translator = $translator; | ||
} | ||
|
||
public function process(string $name, mixed $value, ?string $modifier = null): string | ||
{ | ||
if ($modifier === 'trans' && is_string($value)) { | ||
return call_user_func($this->translator, $value); | ||
} | ||
|
||
return $this->nextProcessor->process($name, $value, $modifier); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,27 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Validation\Test\Message\Parameter; | ||
|
||
use Respect\Validation\Message\Parameter\Processor; | ||
|
||
use function json_encode; | ||
use function sprintf; | ||
|
||
final class TestingProcessor implements Processor | ||
{ | ||
public function process(string $name, mixed $value, ?string $modifier = null): string | ||
{ | ||
if ($modifier !== null) { | ||
return sprintf('%s(<%s:%s>)', $modifier, $name, $modifier); | ||
} | ||
|
||
return sprintf('<%s:%s>', $name, json_encode($value)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.