|
| 1 | +<?php // phpcs:disable Squiz.Commenting.FunctionComment.ParamNameNoMatch, Squiz.Commenting.FunctionComment.IncorrectTypeHint |
| 2 | +declare(strict_types = 1); |
| 3 | + |
| 4 | +namespace TheWebSolver\Codegarage\Lib\Interfaces; |
| 5 | + |
| 6 | +use Closure; |
| 7 | +use Throwable; |
| 8 | +use TheWebSolver\Codegarage\Lib\InvalidPipe; |
| 9 | +use TheWebSolver\Codegarage\Lib\InvalidPipeline; |
| 10 | +use TheWebSolver\Codegarage\Lib\PipeInterface as Handler; |
| 11 | + |
| 12 | +interface ChainOfResponsibility { |
| 13 | + /** |
| 14 | + * Provides additional arguments that can be used by all registered pipes. |
| 15 | + */ |
| 16 | + public function use( mixed ...$globalArgsForEachPipe ): static; |
| 17 | + |
| 18 | + /** |
| 19 | + * Provides subject to be transformed by all registered pipes. |
| 20 | + */ |
| 21 | + public function send( mixed $subject ): static; |
| 22 | + |
| 23 | + /** |
| 24 | + * Registers a single pipe to transform the subject. |
| 25 | + * |
| 26 | + * Pipes registered using this method before `ChainOfResponsibility::through()` method |
| 27 | + * must be deferred and must transform the subject only after pipes registered using |
| 28 | + * `ChainOfResponsibility::through()` method has transformed the subject, if any. |
| 29 | + * |
| 30 | + * @param class-string<Handler>|Handler|Closure(mixed $subject, Closure $next, mixed ...$args): mixed $handler |
| 31 | + */ |
| 32 | + public function pipe( string|Closure|Handler $handler ): static; |
| 33 | + |
| 34 | + /** |
| 35 | + * Registers pipes to transform the subject. |
| 36 | + * |
| 37 | + * Subject must be transformed in the same order pipes are registered. |
| 38 | + * |
| 39 | + * @param array<class-string<Handler>|Handler|Closure(mixed $subject, Closure $next, mixed ...$args): mixed> $pipes |
| 40 | + */ |
| 41 | + public function through( array $pipes ): static; |
| 42 | + |
| 43 | + /** |
| 44 | + * Catches any exception thrown during transformation of subject and returns the fallback value. |
| 45 | + * |
| 46 | + * @param Closure(Throwable $exception, mixed ...$args): mixed $fallback |
| 47 | + */ |
| 48 | + public function sealWith( Closure $fallback ): static; |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns the transformed subject after passing through all registered pipes and current pipe handle. |
| 52 | + * |
| 53 | + * @param Closure(mixed $subject, mixed ...$args): mixed $handle |
| 54 | + * @throws InvalidPipe When pipe type could not be resolved. |
| 55 | + * @throws InvalidPipeline When a pipe abrupt the pipeline by throwing an exception & sealWith not used. |
| 56 | + */ |
| 57 | + public function then( Closure $handle ): mixed; |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns the transformed subject after passing through all registered pipes. |
| 61 | + * |
| 62 | + * @throws InvalidPipe When pipe type could not be resolved. |
| 63 | + * @throws InvalidPipeline When a pipe abrupt the pipeline by throwing an exception & sealWith not used. |
| 64 | + */ |
| 65 | + public function thenReturn(): mixed; |
| 66 | +} |
0 commit comments