Issues (115)

src/Style/Macro.php (1 issue)

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
use function Seboettg\Collection\Lists\emptyList;
22
use function Seboettg\Collection\Lists\listOf;
23
24
/**
25
 * Class Macro
26
 *
27
 * Macros, defined with cs:macro elements, contain formatting instructions. Macros can be called with cs:text from
28
 * within other macros and the cs:layout element of cs:citation and cs:bibliography, and with cs:key from within cs:sort
29
 * of cs:citation and cs:bibliography. It is recommended to place macros after any cs:locale elements and before the
30
 * cs:citation element.
31
 *
32
 * Macros are referenced by the value of the required name attribute on cs:macro. The cs:macro element must contain one
33
 * or more rendering elements.
34
 *
35
 * @package Seboettg\CiteProc\Rendering
36
 *
37
 * @author Sebastian Böttger <[email protected]>
38
 */
39
class Macro implements Rendering, HasParent
40
{
41
    use ConsecutivePunctuationCharacterTrait;
42
43
    /**
44
     * @var ArrayList
45
     */
46
    private $children;
47
48
    /**
49
     * @var string
50
     */
51
    private $name;
52
53
    /**
54
     * @var Root
55
     */
56
    private $parent;
57
    /**
58
     * Macro constructor.
59
     * @param SimpleXMLElement $node
60
     * @param Root $parent
61
     * @throws CiteProcException
62
     */
63
    public function __construct(SimpleXMLElement $node, $parent)
64
    {
65
        $this->parent = $parent;
66
        $attr = $node->attributes();
67
        if (!isset($attr['name'])) {
68
            throw new CiteProcException("Attribute \"name\" needed.");
69
        }
70
        $this->name = (string) $attr['name'];
71
72
        $this->children = emptyList();
0 ignored issues
show
Documentation Bug introduced by
It seems like emptyList() of type anonymous//vendor/seboet...c/Lists/Functions.php$0 is incompatible with the declared type Seboettg\Collection\ArrayList of property $children.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73
        foreach ($node->children() as $child) {
74
            $this->children->add(Factory::create($child, $this));
75
        }
76
    }
77
78
    /**
79
     * @param array|DataList $data
80
     * @param int|null $citationNumber
81
     * @return string
82
     */
83
    public function render($data, $citationNumber = null)
84
    {
85
        $ret = [];
86
        /** @var Rendering $child */
87
        foreach ($this->children as $child) {
88
            $res = $child->render($data, $citationNumber);
89
            $this->getChildrenAffixesAndDelimiter($child);
90
            if (!empty($res)) {
91
                $ret[] = $res;
92
            }
93
        }
94
        $res = implode("", $ret);
95
        if (!empty($res)) {
96
            $res = $this->removeConsecutiveChars($res);
97
        }
98
        return $res;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getName()
105
    {
106
        return $this->name;
107
    }
108
109
    /**
110
     * @return Root
111
     */
112
    public function getParent()
113
    {
114
        return $this->parent;
115
    }
116
}
117