Passed
Push — master ( 85d16e...d12e69 )
by Evgeniy
02:40
created

DynamicContent::id()   A

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
    private string $id;
13
    private array $parameters;
14
15
    /**
16
     * @var callable
17
     */
18
    private $contentGenerator;
19
20
    /**
21
     * @param string $id The unique identifier of the dynamic content.
22
     * @param callable $contentGenerator PHP callable with the signature: `function (array $parameters = []): string;`.
23
     * @param array $parameters The parameters (name-value pairs) that will be passed in the $contentGenerator context.
24
     */
25 5
    public function __construct(string $id, callable $contentGenerator, array $parameters = [])
26
    {
27 5
        $this->id = $id;
28 5
        $this->contentGenerator = $contentGenerator;
29 5
        $this->parameters = $parameters;
30 5
    }
31
32
    /**
33
     * Returns the he unique identifier of the dynamic content.
34
     *
35
     * @return string The unique identifier of the dynamic content.
36
     */
37 5
    public function id(): string
38
    {
39 5
        return $this->id;
40
    }
41
42
    /**
43
     * Generates the dynamic content.
44
     *
45
     * @return string The generated dynamic content.
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