1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/* |
4
|
|
|
* citeproc-php |
5
|
|
|
* |
6
|
|
|
* @link http://github.com/seboettg/citeproc-php for the source repository |
7
|
|
|
* @copyright Copyright (c) 2016 Sebastian Böttger. |
8
|
|
|
* @license https://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Seboettg\CiteProc\Rendering\Choose; |
12
|
|
|
|
13
|
|
|
use Seboettg\CiteProc\Data\DataList; |
14
|
|
|
use Seboettg\CiteProc\Exception\ClassNotFoundException; |
15
|
|
|
use Seboettg\CiteProc\Exception\InvalidStylesheetException; |
16
|
|
|
use Seboettg\CiteProc\Rendering\HasParent; |
17
|
|
|
use Seboettg\CiteProc\Rendering\Rendering; |
18
|
|
|
use Seboettg\Collection\ArrayList; |
19
|
|
|
use Seboettg\Collection\ArrayList\ArrayListInterface; |
20
|
|
|
use SimpleXMLElement; |
21
|
|
|
|
22
|
|
|
class Choose implements Rendering, HasParent |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ArrayList |
27
|
|
|
*/ |
28
|
|
|
private $children; |
29
|
|
|
|
30
|
|
|
private $parent; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param SimpleXMLElement $node |
34
|
|
|
* @param null $parent |
|
|
|
|
35
|
|
|
* @return Choose |
36
|
|
|
* @throws ClassNotFoundException |
37
|
|
|
* @throws InvalidStylesheetException |
38
|
|
|
*/ |
39
|
63 |
|
public static function factory(SimpleXMLElement $node, $parent = null): Choose |
|
|
|
|
40
|
|
|
{ |
41
|
63 |
|
$choose = new Choose($parent); |
42
|
63 |
|
$children = new ArrayList(); |
43
|
63 |
|
$elseIf = []; |
44
|
63 |
|
foreach ($node->children() as $child) { |
45
|
63 |
|
switch ($child->getName()) { |
46
|
63 |
|
case 'if': |
47
|
63 |
|
$children->add("if", ChooseIf::factory($child, $choose)); |
48
|
63 |
|
break; |
49
|
61 |
|
case 'else-if': |
50
|
47 |
|
$elseIf[] = ChooseElseIf::factory($child, $choose); |
51
|
47 |
|
break; |
52
|
61 |
|
case 'else': |
53
|
61 |
|
$children->add("else", ChooseElse::factory($child, $choose)); |
54
|
63 |
|
break; |
55
|
|
|
} |
56
|
|
|
} |
57
|
63 |
|
if (!empty($elseIf)) { |
58
|
47 |
|
$children->add("elseif", $elseIf); |
59
|
|
|
} |
60
|
63 |
|
$choose->setChildren($children); |
61
|
63 |
|
return $choose; |
62
|
|
|
} |
63
|
|
|
|
64
|
63 |
|
public function __construct($parent) |
65
|
|
|
{ |
66
|
63 |
|
$this->parent = $parent; |
67
|
63 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array|DataList $data |
71
|
|
|
* @param null|int $citationNumber |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
56 |
|
public function render($data, $citationNumber = null) |
|
|
|
|
75
|
|
|
{ |
76
|
56 |
|
$arr = []; |
77
|
|
|
|
78
|
|
|
// IF |
79
|
56 |
|
if ($prevCondition = $this->children->get("if")->match($data)) { |
80
|
48 |
|
$arr[] = $this->children->get("if")->render($data); |
81
|
55 |
|
} elseif (!$prevCondition && $this->children->hasKey("elseif")) { // ELSEIF |
82
|
|
|
/** |
83
|
|
|
* @var ChooseElseIf $child |
84
|
|
|
*/ |
85
|
32 |
|
foreach ($this->children->get("elseif") as $child) { |
86
|
32 |
|
$condition = $child->match($data); |
87
|
32 |
|
if ($condition && !$prevCondition) { |
88
|
22 |
|
$arr[] = $child->render($data); |
89
|
22 |
|
$prevCondition = true; |
90
|
22 |
|
break; //break loop as soon as condition matches |
91
|
|
|
} |
92
|
31 |
|
$prevCondition = $condition; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
//ELSE |
97
|
56 |
|
if (!$prevCondition && $this->children->hasKey("else")) { |
98
|
51 |
|
$arr[] = $this->children->get("else")->render($data); |
99
|
|
|
} |
100
|
56 |
|
return implode("", $arr); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
56 |
|
public function getParent() |
107
|
|
|
{ |
108
|
56 |
|
return $this->parent; |
109
|
|
|
} |
110
|
|
|
|
111
|
39 |
|
public function setParent($parent) |
112
|
|
|
{ |
113
|
39 |
|
$this->parent = $parent; |
114
|
39 |
|
} |
115
|
|
|
|
116
|
63 |
|
private function setChildren(ArrayListInterface $children) |
117
|
|
|
{ |
118
|
63 |
|
$this->children = $children; |
|
|
|
|
119
|
63 |
|
} |
120
|
|
|
} |
121
|
|
|
|