|
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 as ArrayList; |
|
20
|
|
|
use Seboettg\Collection\ArrayList\ArrayListInterface; |
|
21
|
|
|
use SimpleXMLElement; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Macro |
|
25
|
|
|
* |
|
26
|
|
|
* Macros, defined with cs:macro elements, contain formatting instructions. Macros can be called with cs:text from |
|
27
|
|
|
* within other macros and the cs:layout element of cs:citation and cs:bibliography, and with cs:key from within cs:sort |
|
28
|
|
|
* of cs:citation and cs:bibliography. It is recommended to place macros after any cs:locale elements and before the |
|
29
|
|
|
* cs:citation element. |
|
30
|
|
|
* |
|
31
|
|
|
* Macros are referenced by the value of the required name attribute on cs:macro. The cs:macro element must contain one |
|
32
|
|
|
* or more rendering elements. |
|
33
|
|
|
* |
|
34
|
|
|
* @package Seboettg\CiteProc\Rendering |
|
35
|
|
|
* |
|
36
|
|
|
* @author Sebastian Böttger <[email protected]> |
|
37
|
|
|
*/ |
|
38
|
|
|
class Macro implements Rendering, HasParent |
|
39
|
|
|
{ |
|
40
|
|
|
use ConsecutivePunctuationCharacterTrait; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var ArrayList |
|
44
|
|
|
*/ |
|
45
|
|
|
private $children; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
private $name; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var Root |
|
54
|
|
|
*/ |
|
55
|
|
|
private $parent; |
|
56
|
|
|
|
|
57
|
71 |
|
public static function factory(SimpleXMLElement $node, $parent): Macro |
|
58
|
|
|
{ |
|
59
|
71 |
|
$name = (string) $node->attributes()['name']; |
|
60
|
71 |
|
$children = new ArrayList(); |
|
61
|
71 |
|
foreach ($node->children() as $child) { |
|
62
|
71 |
|
$children->append(Factory::create($child, $parent)); |
|
63
|
|
|
} |
|
64
|
71 |
|
return new Macro($children, $parent, $name); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Macro constructor. |
|
69
|
|
|
* @param ArrayListInterface $children |
|
70
|
|
|
* @param mixed $parent |
|
71
|
|
|
* @param $name |
|
72
|
|
|
*/ |
|
73
|
71 |
|
public function __construct(ArrayListInterface $children, $parent, $name) |
|
74
|
|
|
{ |
|
75
|
71 |
|
$this->children = $children; |
|
|
|
|
|
|
76
|
71 |
|
$this->parent = $parent; |
|
77
|
71 |
|
$this->name = $name; |
|
78
|
71 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param array|DataList $data |
|
82
|
|
|
* @param int|null $citationNumber |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
65 |
|
public function render($data, $citationNumber = null) |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
65 |
|
$ret = []; |
|
88
|
|
|
/** @var Rendering $child */ |
|
89
|
65 |
|
foreach ($this->children as $child) { |
|
90
|
65 |
|
$res = $child->render($data, $citationNumber); |
|
91
|
65 |
|
$this->getChildrenAffixesAndDelimiter($child); |
|
92
|
65 |
|
if (!empty($res)) { |
|
93
|
65 |
|
$ret[] = $res; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
65 |
|
$res = implode("", $ret); |
|
97
|
65 |
|
if (!empty($res)) { |
|
98
|
65 |
|
$res = $this->removeConsecutiveChars($res); |
|
99
|
|
|
} |
|
100
|
65 |
|
return $res; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
71 |
|
public function getName() |
|
107
|
|
|
{ |
|
108
|
71 |
|
return $this->name; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return Root |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getParent() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->parent; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function setParent($parent) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->parent = $parent; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.