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\Choose; |
11
|
|
|
use Seboettg\CiteProc\Rendering\HasParent; |
12
|
|
|
use Seboettg\CiteProc\Rendering\Rendering; |
13
|
|
|
use Seboettg\CiteProc\Rendering\Text; |
14
|
|
|
use Seboettg\CiteProc\Style\Macro; |
15
|
|
|
use Seboettg\Collection\ArrayList; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Choose |
20
|
|
|
* @package Seboettg\CiteProc\Node |
21
|
|
|
* |
22
|
|
|
* @author Sebastian Böttger <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class Choose implements Rendering, HasParent |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ArrayList |
29
|
|
|
*/ |
30
|
|
|
private $children; |
31
|
|
|
|
32
|
|
|
private $parent; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Choose constructor. |
36
|
|
|
* @param \SimpleXMLElement $node |
37
|
|
|
* @param $parent |
38
|
|
|
*/ |
39
|
|
|
public function __construct(\SimpleXMLElement $node, $parent) |
40
|
|
|
{ |
41
|
|
|
$this->parent = $parent; |
42
|
|
|
$this->children = new ArrayList(); |
43
|
|
|
$elseIf = []; |
44
|
|
|
foreach ($node->children() as $child) { |
45
|
|
|
switch ($child->getName()) { |
46
|
|
|
case 'if': |
47
|
|
|
$this->children->add("if", new ChooseIf($child, $this)); |
48
|
|
|
break; |
49
|
|
|
case 'else-if': |
50
|
|
|
$elseIf[] = new ChooseElseIf($child, $this); |
51
|
|
|
break; |
52
|
|
|
case 'else': |
53
|
|
|
$this->children->add("else", new ChooseElse($child, $this)); |
54
|
|
|
break; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
if (!empty($elseIf)) { |
58
|
|
|
$this->children->add("elseif", $elseIf); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array|\Seboettg\CiteProc\Data\DataList $data |
64
|
|
|
* @param null|int $citationNumber |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function render($data, $citationNumber = null) |
68
|
|
|
{ |
69
|
|
|
$arr = []; |
70
|
|
|
|
71
|
|
|
// IF |
72
|
|
|
if ($prevCondition = $this->children->get("if")->match($data)) { |
73
|
|
|
$arr[] = $this->children->get("if")->render($data); |
74
|
|
|
|
75
|
|
|
} else if (!$prevCondition && $this->children->hasKey("elseif")) { // ELSEIF |
76
|
|
|
/** @var ChooseElseIf $child */ |
77
|
|
|
foreach ($this->children->get("elseif") as $child) { |
78
|
|
|
$condition = $child->match($data); |
79
|
|
|
if ($condition && !$prevCondition) { |
80
|
|
|
$arr[] = $child->render($data); |
81
|
|
|
$prevCondition = true; |
82
|
|
|
break; //break loop as soon as condition matches |
83
|
|
|
} |
84
|
|
|
$prevCondition = $condition; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
//ELSE |
89
|
|
|
if (!$prevCondition && $this->children->hasKey("else")) { |
90
|
|
|
$arr[] = $this->children->get("else")->render($data); |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
return implode("", $arr); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function rendersEmptyVariables($data) |
97
|
|
|
{ |
98
|
|
|
if ($prevCondition = $this->children->get("if")->match($data)) { |
99
|
|
|
return $this->children->get("if")->rendersEmptyVariables($data); |
100
|
|
|
|
101
|
|
|
} else if (!$prevCondition && $this->children->hasKey("elseif")) { // ELSEIF |
102
|
|
|
/** @var ChooseElseIf $child */ |
103
|
|
|
foreach ($this->children->get("elseif") as $child) { |
104
|
|
|
$condition = $child->match($data); |
105
|
|
|
if ($condition && !$prevCondition) { |
106
|
|
|
return $child->rendersEmptyVariables($data); |
107
|
|
|
} |
108
|
|
|
$prevCondition = $condition; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
//ELSE |
113
|
|
|
if (!$prevCondition && $this->children->hasKey("else")) { |
114
|
|
|
return $this->children->get("else")->rendersEmptyVariables($data); |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
|
|
public function getParent() |
124
|
|
|
{ |
125
|
|
|
return $this->parent; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
This check marks files that end in a newline character, i.e. an empy line.