1: <?php declare(strict_types=1);
2:
3: namespace Salient\Contract\Core\Pipeline;
4:
5: use Salient\Contract\Core\ArrayMapperFlag;
6: use Salient\Contract\Core\Chainable;
7: use Salient\Contract\Core\Immutable;
8: use Closure;
9:
10: /**
11: * @template TInput
12: * @template TOutput
13: * @template TArgument
14: */
15: interface BasePipelineInterface extends Chainable, Immutable
16: {
17: /**
18: * Apply a closure to each payload before it is sent
19: *
20: * This method can only be called once per pipeline.
21: *
22: * @param Closure(TInput $payload, static $pipeline, TArgument $arg): (TInput|TOutput) $closure
23: * @return static
24: */
25: public function after(Closure $closure);
26:
27: /**
28: * Apply a closure to each payload before it is sent if after() hasn't
29: * already been called
30: *
31: * @param Closure(TInput $payload, static $pipeline, TArgument $arg): (TInput|TOutput) $closure
32: * @return static
33: */
34: public function afterIf(Closure $closure);
35:
36: /**
37: * Add a pipe to the pipeline
38: *
39: * A pipe must be one of the following:
40: *
41: * - an instance of a class that implements {@see PipeInterface}
42: * - the name of a class that implements {@see PipeInterface}
43: * - a closure
44: *
45: * Whichever form it takes, a pipe should do something with `$payload`
46: * before taking one of the following actions:
47: *
48: * - return the value of `$next($payload)`
49: * - return a value that will be discarded by
50: * {@see StreamPipelineInterface::unless()}, bypassing any remaining pipes
51: * and {@see BasePipelineInterface::then()}, if applicable
52: * - throw an exception
53: *
54: * @param (Closure(TInput $payload, Closure $next, static $pipeline, TArgument $arg): (TInput|TOutput))|(Closure(TOutput $payload, Closure $next, static $pipeline, TArgument $arg): TOutput)|PipeInterface<TInput,TOutput,TArgument>|class-string<PipeInterface<TInput,TOutput,TArgument>> $pipe
55: * @return static
56: */
57: public function through($pipe);
58:
59: /**
60: * Add a simple closure to the pipeline
61: *
62: * @param (Closure(TInput $payload, static $pipeline, TArgument $arg): (TInput|TOutput))|(Closure(TOutput $payload, static $pipeline, TArgument $arg): TOutput) $closure
63: * @return static
64: */
65: public function throughClosure(Closure $closure);
66:
67: /**
68: * Add an array key mapper to the pipeline
69: *
70: * @param array<array-key,array-key|array-key[]> $keyMap An array that maps
71: * input keys to one or more output keys.
72: * @param int-mask-of<ArrayMapperFlag::*> $flags
73: * @return static
74: */
75: public function throughKeyMap(array $keyMap, int $flags = ArrayMapperFlag::ADD_UNMAPPED);
76:
77: /**
78: * Apply a closure to each result
79: *
80: * This method can only be called once per pipeline, and only if
81: * {@see StreamPipelineInterface::collectThen()} is not also called.
82: *
83: * @param (Closure(TInput $result, static $pipeline, TArgument $arg): TOutput)|(Closure(TOutput $result, static $pipeline, TArgument $arg): TOutput) $closure
84: * @return static
85: */
86: public function then(Closure $closure);
87:
88: /**
89: * Apply a closure to each result if then() hasn't already been called
90: *
91: * @param (Closure(TInput $result, static $pipeline, TArgument $arg): TOutput)|(Closure(TOutput $result, static $pipeline, TArgument $arg): TOutput) $closure
92: * @return static
93: */
94: public function thenIf(Closure $closure);
95:
96: /**
97: * Pass each result to a closure
98: *
99: * Results not discarded by {@see StreamPipelineInterface::unless()} are
100: * passed to the closure before leaving the pipeline. The closure's return
101: * value is ignored.
102: *
103: * @param Closure(TOutput $result, static $pipeline, TArgument $arg): mixed $closure
104: * @return static
105: */
106: public function cc(Closure $closure);
107: }
108: