Completed
Push — master ( 9ad54b...895d73 )
by Federico
03:21
created

Block::includeBlock()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 9
eloc 14
c 1
b 0
f 1
nc 7
nop 0
dl 0
loc 25
rs 4.909
1
<?php
2
3
namespace Jade\Nodes;
4
5
class Block extends Node
6
{
7
    public $isBlock = true;
8
    public $nodes = array();
9
10
    public function __construct($node = null)
11
    {
12
        if (null !== $node) {
13
            $this->push($node);
14
        }
15
    }
16
17
    public function replace($other)
18
    {
19
        $other->nodes = $this->nodes;
20
    }
21
22
    public function push($node)
23
    {
24
        return array_push($this->nodes, $node);
25
    }
26
27
    public function isEmpty()
28
    {
29
        return 0 === count($this->nodes);
30
    }
31
32
    public function unshift($node)
33
    {
34
        return array_unshift($this->nodes, $node);
35
    }
36
37
    public function getYield()
38
    {
39
        foreach ($this->nodes as $node) {
40
            if (isset($node->yield)) {
41
                return $node;
42
            } elseif (isset($node->block) && $yield = $node->block->getYield()) {
43
                return $yield;
44
            }
45
        }
46
47
        return false;
48
    }
49
50
    public function includeBlock()
51
    {
52
        $ret = $this;
53
        foreach ($this->nodes as $node) {
54
            if (isset($node->yield)) {
55
                return $node;
56
            }
57
58
            if (isset($node->block) && $yield = $node->block->getYield()) {
59
                return $yield;
60
            }
61
62
            if (isset($node->textOnly)) {
63
                continue;
64
            }
65
66
            if (method_exists($node, 'includeBlock')) {
67
                $ret = $node->includeBlock();
68
            } elseif (isset($node->block) && !$node->block->isEmpty()) {
69
                $ret = $node->block->includeBlock();
70
            }
71
        }
72
73
        return $ret;
74
    }
75
}
76