DynamicContent::placeholder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\View\Cache;
6
7
/**
8
 * `DynamicContent` generates data for dynamic content that is used for cached content {@see CachedContent}.
9
 */
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
58
    {
59 5
        return "<![CDATA[YII-DYNAMIC-$this->id]]>";
60
    }
61
}
62