Passed
Push — new-api ( 5677f6...2a03a6 )
by Sebastian
04:12
created

Choose::setParent()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Rendering\Choose;
11
12
use Seboettg\CiteProc\Data\DataList;
13
use Seboettg\CiteProc\Exception\ClassNotFoundException;
14
use Seboettg\CiteProc\Exception\InvalidStylesheetException;
15
use Seboettg\CiteProc\Rendering\HasParent;
16
use Seboettg\CiteProc\Rendering\Rendering;
17
use Seboettg\Collection\ArrayList;
18
use SimpleXMLElement;
19
20
/**
21
 * Class Choose
22
 *
23
 * @package Seboettg\CiteProc\Node
24
 *
25
 * @author Sebastian Böttger <[email protected]>
26
 */
27
class Choose implements Rendering, HasParent
28
{
29
30
    /**
31
     * @var ArrayList
32
     */
33
    private $children;
34
35
    private $parent;
36
37
    /**
38
     * Choose constructor.
39
     *
40
     * @param  SimpleXMLElement $node
41
     * @param  $parent
42
     * @throws ClassNotFoundException
43
     * @throws InvalidStylesheetException
44
     */
45 63
    public function __construct(SimpleXMLElement $node, $parent = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$parent" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$parent"; expected 0 but found 1
Loading history...
46
    {
47 63
        $this->parent = $parent;
48 63
        $this->children = new ArrayList();
49 63
        $elseIf = [];
50 63
        foreach ($node->children() as $child) {
51 63
            switch ($child->getName()) {
52 63
                case 'if':
53 63
                    $this->children->add("if", new ChooseIf($child, $this));
54 63
                    break;
55 61
                case 'else-if':
56 47
                    $elseIf[] = new ChooseElseIf($child, $this);
57 47
                    break;
58 61
                case 'else':
59 61
                    $this->children->add("else", new ChooseElse($child, $this));
60 63
                    break;
61
            }
62
        }
63 63
        if (!empty($elseIf)) {
64 47
            $this->children->add("elseif", $elseIf);
65
        }
66 63
    }
67
68
    /**
69
     * @param  array|DataList $data
70
     * @param  null|int       $citationNumber
71
     * @return string
72
     */
73 56
    public function render($data, $citationNumber = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$citationNumber" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$citationNumber"; expected 0 but found 1
Loading history...
74
    {
75 56
        $arr = [];
76
77
        // IF
78 56
        if ($prevCondition = $this->children->get("if")->match($data)) {
79 48
            $arr[] = $this->children->get("if")->render($data);
80 55
        } elseif (!$prevCondition && $this->children->hasKey("elseif")) { // ELSEIF
81
            /**
82
             * @var ChooseElseIf $child
83
             */
84 32
            foreach ($this->children->get("elseif") as $child) {
85 32
                $condition = $child->match($data);
86 32
                if ($condition && !$prevCondition) {
87 22
                    $arr[] = $child->render($data);
88 22
                    $prevCondition = true;
89 22
                    break; //break loop as soon as condition matches
90
                }
91 31
                $prevCondition = $condition;
92
            }
93
        }
94
95
        //ELSE
96 56
        if (!$prevCondition && $this->children->hasKey("else")) {
97 51
            $arr[] = $this->children->get("else")->render($data);
98
        }
99 56
        return implode("", $arr);
100
    }
101
102
    /**
103
     * @return mixed
104
     */
105 56
    public function getParent()
106
    {
107 56
        return $this->parent;
108
    }
109
110 39
    public function setParent($parent)
111
    {
112 39
        $this->parent = $parent;
113 39
    }
114
}
115