Passed
Push — master ( 279db1...661b73 )
by Sebastian
18:02 queued 11:37
created

Substitute::render()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 12
nop 2
dl 0
loc 21
ccs 0
cts 17
cp 0
crap 30
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/*
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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\CiteProc;
12
use Seboettg\CiteProc\Exception\InvalidStylesheetException;
13
use Seboettg\CiteProc\Rendering\Rendering;
14
use Seboettg\CiteProc\RenderingState;
15
use Seboettg\CiteProc\Util\Factory;
16
use Seboettg\Collection\ArrayList;
17
use SimpleXMLElement;
18
use stdClass;
19
20
21
/**
22
 * Class Substitute
23
 * The optional cs:substitute element, which must be included as the last child element of cs:names, adds substitution
24
 * in case the name variables specified in the parent cs:names element are empty. The substitutions are specified as
25
 * child elements of cs:substitute, and must consist of one or more rendering elements (with the exception of cs:layout).
26
 *
27
 * A shorthand version of cs:names without child elements, which inherits the attributes values set on the cs:name and
28
 * cs:et-al child elements of the original cs:names element, may also be used.
29
 *
30
 * If cs:substitute contains multiple child elements, the first element to return a non-empty result is used for
31
 * substitution. Substituted variables are suppressed in the rest of the output to prevent duplication. An example,
32
 * where an empty “author” name variable is substituted by the “editor” name variable, or, when no editors exist, by
33
 * the “title” macro:
34
 * <pre>
35
 *   <macro name="author">
36
 *      <names variable="author">
37
 *        <substitute>
38
 *          <names variable="editor"/>
39
 *          <text macro="title"/>
40
 *        </substitute>
41
 *      </names>
42
 *   </macro>
43
 * </pre>
44
 * @package Seboettg\CiteProc\Rendering\Name
1 ignored issue
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
45
 *
46
 * @author Sebastian Böttger <[email protected]>
47
 */
3 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
48
class Substitute implements Rendering
49
{
50
51
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
52
     * @var ArrayList
53
     */
54
    private $children;
0 ignored issues
show
Coding Style introduced by
Private member variable "children" must be prefixed with an underscore
Loading history...
55
56
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
57
     * @var Names
58
     */
59
    private $parent;
0 ignored issues
show
Coding Style introduced by
Private member variable "parent" must be prefixed with an underscore
Loading history...
60
61
    /**
62
     * Substitute constructor.
63
     * @param SimpleXMLElement $node
3 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
64
     * @param Names $parent
3 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
65
     * @throws InvalidStylesheetException
1 ignored issue
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
66
     * @throws InvalidStylesheetException
1 ignored issue
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
67
     */
68
    public function __construct(SimpleXMLElement $node, Names $parent)
69
    {
70
        $this->parent = $parent;
71
        $this->children = new ArrayList();
72
        foreach ($node->children() as $child) {
73
74
            /** @var SimpleXMLElement $child */
3 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
75
            if ($child->getName() === "names") {
76
77
                /** @var Names $names */
3 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
78
                $names = Factory::create($child, $this);
79
80
                /* A shorthand version of cs:names without child elements, which inherits the attributes values set on
81
                the cs:name and cs:et-al child elements of the original cs:names element, may also be used. */
82
                if (!$names->hasEtAl()) {
83
                    // inherit et-al
84
                    if ($this->parent->hasEtAl()) {
85
                        $names->setEtAl($this->parent->getEtAl());
86
                    }
87
                }
88
                if (!$names->hasName()) {
89
                    // inherit name
90
                    if ($this->parent->hasName()) {
91
                        $names->setName($this->parent->getName());
92
                    }
93
                }
94
                // inherit label
95
                if (!$names->hasLabel() && $this->parent->hasLabel()) {
96
                    $names->setLabel($this->parent->getLabel());
97
                }
98
99
                $this->children->append($names);
100
            } else {
101
                $object = Factory::create($child, $this);
102
                $this->children->append($object);
103
            }
104
        }
105
    }
106
107
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
108
     * @param stdClass $data
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
109
     * @param int|null $citationNumber
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
110
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
111
     */
112
    public function render($data, $citationNumber = null)
113
    {
114
        $ret = [];
115
        if (CiteProc::getContext()->getRenderingState()->getValue() !== RenderingState::SORTING) {
116
            CiteProc::getContext()->setRenderingState(new RenderingState(RenderingState::SUBSTITUTION));
117
        }
118
119
        /** @var Rendering $child */
3 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
120
        foreach ($this->children as $child) {
121
            /* If cs:substitute contains multiple child elements, the first element to return a
122
            non-empty result is used for substitution. */
123
            $res = $child->render($data, $citationNumber);
124
            if (!empty($res)) {
125
                $ret[] = $res;
126
                break;
127
            }
128
        }
129
        if (CiteProc::getContext()->getRenderingState()->getValue() === RenderingState::SUBSTITUTION) {
130
            CiteProc::getContext()->setRenderingState(new RenderingState(RenderingState::RENDERING));
131
        }
132
        return implode("", $ret);
133
    }
134
}
135