|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shoot\Shoot; |
|
5
|
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Throwable; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A view is the visual representation of a model. |
|
11
|
|
|
*/ |
|
12
|
|
|
final class View |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var callable */ |
|
15
|
|
|
private $callback; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
private $name; |
|
19
|
|
|
|
|
20
|
|
|
/** @var PresentationModel */ |
|
21
|
|
|
private $presentationModel; |
|
22
|
|
|
|
|
23
|
|
|
/** @var null|Throwable */ |
|
24
|
|
|
private $suppressedException; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $name The name of the view. |
|
28
|
|
|
* @param PresentationModel $presentationModel A presentation model holding the variables for this view. |
|
29
|
|
|
* @param callable $callback A callback to render this view. |
|
30
|
|
|
* |
|
31
|
|
|
* @throws InvalidArgumentException |
|
32
|
|
|
*/ |
|
33
|
16 |
|
public function __construct(string $name, PresentationModel $presentationModel, callable $callback) |
|
34
|
|
|
{ |
|
35
|
16 |
|
if ($name === '') { |
|
36
|
1 |
|
throw new InvalidArgumentException('The name of a view cannot be empty'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
15 |
|
$this->name = $name; |
|
40
|
15 |
|
$this->presentationModel = $presentationModel; |
|
41
|
15 |
|
$this->callback = $callback; |
|
42
|
15 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Renders the view. |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
7 |
|
public function render() |
|
50
|
|
|
{ |
|
51
|
7 |
|
call_user_func($this->callback, $this->presentationModel->getVariables()); |
|
52
|
5 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return string The name of the view. |
|
56
|
|
|
*/ |
|
57
|
3 |
|
public function getName(): string |
|
58
|
|
|
{ |
|
59
|
3 |
|
return $this->name; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return PresentationModel |
|
64
|
|
|
*/ |
|
65
|
9 |
|
public function getPresentationModel(): PresentationModel |
|
66
|
|
|
{ |
|
67
|
9 |
|
return $this->presentationModel; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param PresentationModel $presentationModel |
|
72
|
|
|
* |
|
73
|
|
|
* @return View |
|
74
|
|
|
*/ |
|
75
|
4 |
|
public function withPresentationModel(PresentationModel $presentationModel): self |
|
76
|
|
|
{ |
|
77
|
4 |
|
$new = clone $this; |
|
78
|
4 |
|
$new->presentationModel = $presentationModel; |
|
79
|
|
|
|
|
80
|
4 |
|
return $new; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return null|Throwable |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function getSuppressedException() |
|
87
|
|
|
{ |
|
88
|
1 |
|
return $this->suppressedException; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function hasSuppressedException(): bool |
|
95
|
|
|
{ |
|
96
|
2 |
|
return $this->suppressedException !== null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param null|Throwable $exception |
|
101
|
|
|
* |
|
102
|
|
|
* @return View |
|
103
|
|
|
*/ |
|
104
|
2 |
|
public function withSuppressedException(Throwable $exception = null): self |
|
105
|
|
|
{ |
|
106
|
2 |
|
$new = clone $this; |
|
107
|
2 |
|
$new->suppressedException = $exception; |
|
108
|
|
|
|
|
109
|
2 |
|
return $new; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|