Total Complexity | 4 |
Total Lines | 50 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
10 | final class DynamicContent |
||
11 | { |
||
12 | /** |
||
13 | * @var callable |
||
14 | */ |
||
15 | private $contentGenerator; |
||
16 | |||
17 | /** |
||
18 | * @param string $id The unique identifier of the dynamic content. |
||
19 | * @param callable $contentGenerator PHP callable with the signature: `function (array $parameters = []): string;`. |
||
20 | * @param array $parameters The parameters (name-value pairs) that will be passed in the $contentGenerator context. |
||
21 | */ |
||
22 | 5 | public function __construct( |
|
23 | private string $id, |
||
24 | callable $contentGenerator, |
||
25 | private array $parameters = [] |
||
26 | ) { |
||
27 | 5 | $this->contentGenerator = $contentGenerator; |
|
28 | } |
||
29 | |||
30 | /** |
||
31 | * Returns a unique identifier of the dynamic content. |
||
32 | * |
||
33 | * @return string The unique identifier of the dynamic content. |
||
34 | */ |
||
35 | 5 | public function id(): string |
|
36 | { |
||
37 | 5 | return $this->id; |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * Generates the dynamic content. |
||
42 | * |
||
43 | * @return string The generated dynamic content. |
||
44 | * |
||
45 | * @psalm-suppress MixedInferredReturnType, MixedReturnStatement |
||
46 | */ |
||
47 | 5 | public function content(): string |
|
48 | { |
||
49 | 5 | return ($this->contentGenerator)($this->parameters); |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * Returns the placeholder of the dynamic content. |
||
54 | * |
||
55 | * @return string The placeholder of the dynamic content. |
||
56 | */ |
||
57 | 5 | public function placeholder(): string |
|
60 | } |
||
61 | } |
||
62 |