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

Macro::setParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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\Style;
11
12
use Seboettg\CiteProc\Data\DataList;
13
use Seboettg\CiteProc\Exception\CiteProcException;
14
use Seboettg\CiteProc\Rendering\HasParent;
15
use Seboettg\CiteProc\Rendering\Rendering;
16
use Seboettg\CiteProc\Root\Root;
17
use Seboettg\CiteProc\Styles\ConsecutivePunctuationCharacterTrait;
18
use Seboettg\CiteProc\Util\Factory;
19
use Seboettg\Collection\ArrayList;
20
use SimpleXMLElement;
21
22
/**
23
 * Class Macro
24
 *
25
 * Macros, defined with cs:macro elements, contain formatting instructions. Macros can be called with cs:text from
26
 * within other macros and the cs:layout element of cs:citation and cs:bibliography, and with cs:key from within cs:sort
27
 * of cs:citation and cs:bibliography. It is recommended to place macros after any cs:locale elements and before the
28
 * cs:citation element.
29
 *
30
 * Macros are referenced by the value of the required name attribute on cs:macro. The cs:macro element must contain one
31
 * or more rendering elements.
32
 *
33
 * @package Seboettg\CiteProc\Rendering
34
 *
35
 * @author Sebastian Böttger <[email protected]>
36
 */
37
class Macro implements Rendering, HasParent
38
{
39
    use ConsecutivePunctuationCharacterTrait;
40
41
    /**
42
     * @var ArrayList
43
     */
44
    private $children;
45
46
    /**
47
     * @var string
48
     */
49
    private $name;
50
51
    /**
52
     * @var Root
53
     */
54
    private $parent;
55
    /**
56
     * Macro constructor.
57
     * @param SimpleXMLElement $node
58
     * @param Root $parent
59
     * @throws CiteProcException
60
     */
61 70
    public function __construct(SimpleXMLElement $node, $parent)
62
    {
63 70
        $this->parent = $parent;
64 70
        $attr = $node->attributes();
65 70
        if (!isset($attr['name'])) {
66
            throw new CiteProcException("Attribute \"name\" needed.");
67
        }
68 70
        $this->name = (string) $attr['name'];
69
70 70
        $this->children = new ArrayList();
71 70
        foreach ($node->children() as $child) {
72 70
            $this->children->append(Factory::create($child, $this));
73
        }
74 70
    }
75
76
    /**
77
     * @param array|DataList $data
78
     * @param int|null $citationNumber
79
     * @return string
80
     */
81 64
    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...
82
    {
83 64
        $ret = [];
84
        /** @var Rendering $child */
85 64
        foreach ($this->children as $child) {
86 64
            $res = $child->render($data, $citationNumber);
87 64
            $this->getChildrenAffixesAndDelimiter($child);
88 64
            if (!empty($res)) {
89 64
                $ret[] = $res;
90
            }
91
        }
92 64
        $res = implode("", $ret);
93 64
        if (!empty($res)) {
94 64
            $res = $this->removeConsecutiveChars($res);
95
        }
96 64
        return $res;
97
    }
98
99
    /**
100
     * @return string
101
     */
102 70
    public function getName()
103
    {
104 70
        return $this->name;
105
    }
106
107
    /**
108
     * @return Root
109
     */
110
    public function getParent()
111
    {
112
        return $this->parent;
113
    }
114
115
    public function setParent($parent)
116
    {
117
        $this->parent = $parent;
118
    }
119
}
120