1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Http\Server; |
4: | |
5: | use Salient\Core\Concern\ImmutableTrait; |
6: | use Salient\Http\Message\Response; |
7: | use LogicException; |
8: | |
9: | /** |
10: | * @api |
11: | * |
12: | * @template TReturn = mixed |
13: | */ |
14: | class ServerResponse extends Response |
15: | { |
16: | use ImmutableTrait; |
17: | |
18: | protected bool $HasReturnValue = false; |
19: | /** @var TReturn */ |
20: | protected $ReturnValue; |
21: | |
22: | /** |
23: | * Check if the response has a return value |
24: | */ |
25: | public function hasReturnValue(): bool |
26: | { |
27: | return $this->HasReturnValue; |
28: | } |
29: | |
30: | /** |
31: | * Get the return value applied to the response |
32: | * |
33: | * @return TReturn |
34: | * @throws LogicException if the response does not have a return value. |
35: | */ |
36: | public function getReturnValue() |
37: | { |
38: | if (!$this->HasReturnValue) { |
39: | throw new LogicException('No return value'); |
40: | } |
41: | return $this->ReturnValue; |
42: | } |
43: | |
44: | /** |
45: | * Get an instance with the given return value |
46: | * |
47: | * @template T |
48: | * |
49: | * @param T $value |
50: | * @return static<T> |
51: | */ |
52: | public function withReturnValue($value = null) |
53: | { |
54: | /** @var static<T> */ |
55: | // @phpstan-ignore varTag.nativeType |
56: | $response = $this; |
57: | // @phpstan-ignore salient.property.type, return.type |
58: | return $response |
59: | ->with('HasReturnValue', true) |
60: | ->with('ReturnValue', $value); |
61: | } |
62: | } |
63: |