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