Passed
Pull Request — master (#159)
by Evgeniy
07:24
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;
6
7
use function crc32;
8
use function dechex;
9
10
/**
11
 * DynamicContent generates data for dynamic content that is used for cached content {@see CacheContentTest}.
12
 */
13
final class DynamicContent
14
{
15
    private string $id;
16
    private array $parameters;
17
18
    /**
19
     * @var callable
20
     */
21
    private $contentGenerator;
22
23
    /**
24
     * @param string $id The unique identifier of the dynamic content.
25
     * @param callable $contentGenerator PHP callable with the signature: `function (array $parameters = []): string;`.
26
     * @param array $parameters The parameters (name-value pairs) that will be passed in the $contentGenerator context.
27
     */
28 5
    public function __construct(string $id, callable $contentGenerator, array $parameters = [])
29
    {
30 5
        $this->id = $id;
31 5
        $this->contentGenerator = $contentGenerator;
32 5
        $this->parameters = $parameters;
33 5
    }
34
35
    /**
36
     * Returns the he unique identifier of the dynamic content.
37
     *
38
     * @return string The unique identifier of the dynamic content.
39
     */
40 5
    public function id(): string
41
    {
42 5
        return $this->id;
43
    }
44
45
    /**
46
     * Generates the dynamic content.
47
     *
48
     * @return string The generated dynamic content.
49
     */
50 5
    public function content(): string
51
    {
52 5
        return ($this->contentGenerator)($this->parameters);
53
    }
54
55
    /**
56
     * Returns the placeholder of the dynamic content.
57
     *
58
     * @return string The placeholder of the dynamic content.
59
     */
60 5
    public function placeholder(): string
61
    {
62 5
        return "<![CDATA[YII-DYNAMIC-$this->id]]>";
63
    }
64
}
65