Completed
Push — master ( c434f2...7ac5be )
by Sebastian
03:19
created

Substitute::getVariable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
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\Name;
11
use Seboettg\CiteProc\Exception\CiteProcException;
12
use Seboettg\CiteProc\Rendering\Layout;
13
use Seboettg\CiteProc\Rendering\RenderingInterface;
14
use Seboettg\CiteProc\Util\Factory;
15
use Seboettg\Collection\ArrayList;
16
17
18
/**
19
 * Class Substitute
20
 * The optional cs:substitute element, which must be included as the last child element of cs:names, adds substitution
21
 * in case the name variables specified in the parent cs:names element are empty. The substitutions are specified as
22
 * child elements of cs:substitute, and must consist of one or more rendering elements (with the exception of cs:layout).
23
 *
24
 * A shorthand version of cs:names without child elements, which inherits the attributes values set on the cs:name and
25
 * cs:et-al child elements of the original cs:names element, may also be used.
26
 *
27
 * If cs:substitute contains multiple child elements, the first element to return a non-empty result is used for
28
 * substitution. Substituted variables are suppressed in the rest of the output to prevent duplication. An example,
29
 * where an empty “author” name variable is substituted by the “editor” name variable, or, when no editors exist, by
30
 * the “title” macro:
31
 * <pre>
32
 *   <macro name="author">
33
 *      <names variable="author">
34
 *        <substitute>
35
 *          <names variable="editor"/>
36
 *          <text macro="title"/>
37
 *        </substitute>
38
 *      </names>
39
 *   </macro>
40
 * </pre>
41
 * @package Seboettg\CiteProc\Rendering\Name
42
 *
43
 * @author Sebastian Böttger <[email protected]>
44
 */
45
class Substitute implements RenderingInterface
46
{
47
48
    /**
49
     * @var ArrayList
50
     */
51
    private $children;
52
53
    /**
54
     * @var Names
55
     */
56
    private $parent;
57
58
    /**
59
     * Substitute constructor.
60
     * @param \SimpleXMLElement $node
61
     * @param Names $parent
62
     */
63
    public function __construct(\SimpleXMLElement $node, Names $parent)
64
    {
65
        $this->parent = $parent;
66
        $this->children = new ArrayList();
67
        foreach ($node->children() as $child) {
68
69
            /** @var \SimpleXMLElement $child */
70
            if ($child->getName() === "Names") {
71
72
                /** @var Names $names */
73
                $names = Factory::create($child);
74
75
                /* A shorthand version of cs:names without child elements, which inherits the attributes values set on
76
                the cs:name and cs:et-al child elements of the original cs:names element, may also be used. */
77
                if (!$names->hasChilds()) {
0 ignored issues
show
Bug introduced by
The method hasChilds() does not seem to exist on object<Seboettg\CiteProc\Rendering\Name\Names>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
                    if ($this->parent->hasEtAl()) {
79
                        $names->setEtAl($this->parent->getEtAl());
80
                    }
81
                    if ($this->parent->hasName()) {
82
                        $names->setName($this->parent->getName());
83
                    }
84
                }
85
                $this->children->append($names);
86
87
            } else {
88
                $object = Factory::create($child);
89
                $this->children->append($object);
90
            }
91
        }
92
    }
93
94
    /**
95
     * @param $data
96
     * @return string
97
     */
98
    public function render($data)
99
    {
100
        $str = "";
101
102
        /* adds substitution in case the name variables specified in the parent cs:names element are empty. */
103
        if ($this->parent->getVariables()->count() === 0) {
104
            /** @var RenderingInterface $child */
105
            foreach ($this->children as $child) {
106
                /* If cs:substitute contains multiple child elements, the first element to return a
107
                non-empty result is used for substitution. */
108
                $str = $child->render($data);
109
                if (!empty($str)) {
110
                    return $str;
111
                }
112
            }
113
            return $str;
114
        }
115
    }
116
}