Completed
Push — master ( d2e043...09ed12 )
by Changwan
12:25
created

Buffer::resolveLayout()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\View\Phiew;
3
4
class Buffer
5
{
6
    /** @var string */
7
    protected $buffer = '';
8
9
    /** @var array */
10
    protected $attributes = [];
11
12
    /** @var array */
13
    protected $contents = [];
14
15
    /** @var \Wandu\View\Phiew\Template */
16
    protected $layout;
17
    
18
    /** @var array */
19
    protected $layoutAttributes;
20
21
    /** @var string */
22
    protected $pushName;
23
    
24
    /** @var string */
25
    protected $sectionName;
26
27 4
    public function __construct(array $attributes = [], array $contents = [])
28
    {
29 4
        $this->attributes = $attributes;
30 4
        $this->contents = $contents;
31 4
    }
32
33
    /**
34
     * @return string
35
     */
36 3
    public function __toString()
37
    {
38 3
        return $this->buffer;
39
    }
40
41
    /**
42
     * @param string $contents
43
     */
44 3
    public function write(string $contents)
45
    {
46 3
        $this->buffer .= $contents;
47 3
    }
48
49
    /**
50
     * @param \Wandu\View\Phiew\Template $template
51
     * @param array $attributes
52
     * @return string
53
     */
54 1
    public function resolveRequire(Template $template, array $attributes = [])
55
    {
56 1
        return $template->execute($attributes + $this->attributes);
57
    }
58
    
59
    /**
60
     * @param \Wandu\View\Phiew\Template $layout
61
     * @param array $attributes
62
     */
63 1
    public function setLayout(Template $layout, array $attributes = [])
64
    {
65 1
        $this->layout = $layout;
66 1
        $this->layoutAttributes = $attributes + $this->attributes;
67 1
    }
68
    
69 3
    public function resolveLayout()
70
    {
71 3
        if ($this->layout) {
72 1
            $this->buffer .= $this->layout->execute($this->layoutAttributes, $this->contents);
73 1
            $this->layout = null;
74
        }
75 3
    }
76
77
    /**
78
     * @param string $name
79
     */
80 1
    public function startPush(string $name)
81
    {
82 1
        $this->pushName = $name;
83 1
    }
84
85
    /**
86
     * @param string $contents
87
     */
88 1
    public function resolvePush(string $contents)
89
    {
90 1
        if (!$this->pushName) {
91
            throw new PhiewException('push is not started.', PhiewException::CODE_WRONG_SYNTAX);
92
        }
93
94 1
        $name = $this->pushName;
95 1
        $this->pushName = null;
96
97 1
        if (!isset($this->contents[$name])) {
98 1
            $this->contents[$name] = '';
99
        }
100 1
        $this->contents[$name] .= $contents;
101 1
    }
102
103
    /**
104
     * @param string $name
105
     */
106 1
    public function startSection(string $name)
107
    {
108 1
        $this->sectionName = $name;
109 1
    }
110
111
    /**
112
     * @param string $contents
113
     */
114 1
    public function resolveSection(string $contents)
115
    {
116 1
        if (!$this->sectionName) {
117
            throw new PhiewException('section is not started.', PhiewException::CODE_WRONG_SYNTAX);
118
        }
119 1
        $name = $this->sectionName;
120 1
        $this->sectionName = null;
121
122 1
        $this->contents[$name] = $contents;
123 1
    }
124
125
    /**
126
     * @param string $name
127
     * @return string
128
     */
129 1
    public function getContent(string $name): string
130
    {
131 1
        return $this->contents[$name] ?? '';
132
    }
133
}
134