|
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
|
|
|
/** |
|
22
|
|
|
* Class Choose |
|
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
|
|
|
* @param SimpleXMLElement $node |
|
|
|
|
|
|
40
|
|
|
* @param $parent |
|
|
|
|
|
|
41
|
|
|
* @throws ClassNotFoundException |
|
|
|
|
|
|
42
|
|
|
* @throws InvalidStylesheetException |
|
|
|
|
|
|
43
|
|
|
*/ |
|
44
|
40 |
|
public function __construct(SimpleXMLElement $node, $parent) |
|
45
|
|
|
{ |
|
46
|
40 |
|
$this->parent = $parent; |
|
47
|
40 |
|
$this->children = new ArrayList(); |
|
48
|
40 |
|
$elseIf = []; |
|
49
|
40 |
|
foreach ($node->children() as $child) { |
|
50
|
40 |
|
switch ($child->getName()) { |
|
51
|
40 |
|
case 'if': |
|
|
|
|
|
|
52
|
40 |
|
$this->children->add("if", new ChooseIf($child, $this)); |
|
53
|
40 |
|
break; |
|
54
|
39 |
|
case 'else-if': |
|
|
|
|
|
|
55
|
33 |
|
$elseIf[] = new ChooseElseIf($child, $this); |
|
56
|
33 |
|
break; |
|
57
|
39 |
|
case 'else': |
|
|
|
|
|
|
58
|
39 |
|
$this->children->add("else", new ChooseElse($child, $this)); |
|
59
|
40 |
|
break; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
40 |
|
if (!empty($elseIf)) { |
|
63
|
33 |
|
$this->children->add("elseif", $elseIf); |
|
64
|
|
|
} |
|
65
|
40 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
|
|
|
|
|
68
|
|
|
* @param array|DataList $data |
|
|
|
|
|
|
69
|
|
|
* @param null|int $citationNumber |
|
|
|
|
|
|
70
|
|
|
* @return string |
|
|
|
|
|
|
71
|
|
|
*/ |
|
72
|
34 |
|
public function render($data, $citationNumber = null) |
|
73
|
|
|
{ |
|
74
|
34 |
|
$arr = []; |
|
75
|
|
|
|
|
76
|
|
|
// IF |
|
77
|
34 |
|
if ($prevCondition = $this->children->get("if")->match($data)) { |
|
78
|
32 |
|
$arr[] = $this->children->get("if")->render($data); |
|
79
|
|
|
|
|
80
|
32 |
|
} else if (!$prevCondition && $this->children->hasKey("elseif")) { // ELSEIF |
|
81
|
|
|
/** @var ChooseElseIf $child */ |
|
|
|
|
|
|
82
|
21 |
|
foreach ($this->children->get("elseif") as $child) { |
|
83
|
21 |
|
$condition = $child->match($data); |
|
84
|
21 |
|
if ($condition && !$prevCondition) { |
|
85
|
12 |
|
$arr[] = $child->render($data); |
|
86
|
12 |
|
$prevCondition = true; |
|
87
|
12 |
|
break; //break loop as soon as condition matches |
|
88
|
|
|
} |
|
89
|
21 |
|
$prevCondition = $condition; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
//ELSE |
|
94
|
34 |
|
if (!$prevCondition && $this->children->hasKey("else")) { |
|
95
|
30 |
|
$arr[] = $this->children->get("else")->render($data); |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
34 |
|
return implode("", $arr); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** |
|
|
|
|
|
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
5 |
|
public function getParent() |
|
106
|
|
|
{ |
|
107
|
5 |
|
return $this->parent; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|