|
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 Seboettg\Collection\Map\MapInterface; |
|
19
|
|
|
use SimpleXMLElement; |
|
20
|
|
|
use function Seboettg\Collection\Lists\emptyList; |
|
21
|
|
|
use function Seboettg\Collection\Map\emptyMap; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Choose |
|
25
|
|
|
* |
|
26
|
|
|
* @package Seboettg\CiteProc\Node |
|
27
|
|
|
* |
|
28
|
|
|
* @author Sebastian Böttger <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class Choose implements Rendering, HasParent |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
private const IF = "if"; |
|
34
|
|
|
private const ELSE_IF = "elseif"; |
|
35
|
|
|
private const ELSE = "else"; |
|
36
|
|
|
|
|
37
|
|
|
private MapInterface $children; |
|
38
|
|
|
|
|
39
|
|
|
private $parent; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Choose constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param SimpleXMLElement $node |
|
45
|
|
|
* @param $parent |
|
46
|
|
|
* @throws ClassNotFoundException |
|
47
|
|
|
* @throws InvalidStylesheetException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct(SimpleXMLElement $node, $parent) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->parent = $parent; |
|
52
|
|
|
$this->children = emptyMap(); |
|
53
|
|
|
$elseIf = emptyList(); |
|
54
|
|
|
foreach ($node->children() as $child) { |
|
55
|
|
|
switch ($child->getName()) { |
|
56
|
|
|
case 'if': |
|
57
|
|
|
$this->children->put(self::IF, new ChooseIf($child, $this)); |
|
|
|
|
|
|
58
|
|
|
break; |
|
59
|
|
|
case 'else-if': |
|
60
|
|
|
$elseIf->add(new ChooseElseIf($child, $this)); |
|
|
|
|
|
|
61
|
|
|
break; |
|
62
|
|
|
case 'else': |
|
63
|
|
|
$this->children->put(self::ELSE, new ChooseElse($child, $this)); |
|
|
|
|
|
|
64
|
|
|
break; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
if ($elseIf->count() > 0) { |
|
68
|
|
|
$this->children->put(self::ELSE_IF, $elseIf); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array|DataList $data |
|
74
|
|
|
* @param null|int $citationNumber |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
public function render($data, $citationNumber = null): string |
|
78
|
|
|
{ |
|
79
|
|
|
$result = emptyList(); |
|
80
|
|
|
$matchedIfs = false; |
|
81
|
|
|
|
|
82
|
|
|
$ifCondition = $this->children->get(self::IF); |
|
83
|
|
|
|
|
84
|
|
|
if ($ifCondition->match($data)) { //IF CONDITION |
|
85
|
|
|
$matchedIfs = true; |
|
86
|
|
|
$result->add($ifCondition->render($data)); |
|
87
|
|
|
} elseif ($this->children->containsKey(self::ELSE_IF)) { // ELSEIF |
|
88
|
|
|
$elseIfs = $this->children |
|
89
|
|
|
->get(self::ELSE_IF) |
|
90
|
|
|
->map(fn (ChooseIf $elseIf) => new Tuple($elseIf, $elseIf->match($data))) |
|
91
|
|
|
->filter(fn (Tuple $elseIfToMatch) => $elseIfToMatch->second === true); |
|
92
|
|
|
$matchedIfs = $elseIfs->count() > 0; |
|
93
|
|
|
if ($matchedIfs) { |
|
94
|
|
|
$result->add( |
|
95
|
|
|
$elseIfs |
|
96
|
|
|
->first() //returns a Tuple |
|
97
|
|
|
->first |
|
98
|
|
|
->render($data) |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// !$matchedIfs ensures that each previous condition has not been met |
|
104
|
|
|
if (!$matchedIfs && $this->children->containsKey(self::ELSE)) { //ELSE |
|
105
|
|
|
$result->add($this->children->get(self::ELSE)->render($data)); |
|
106
|
|
|
} |
|
107
|
|
|
return $result->joinToString(""); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return mixed |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getParent() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->parent; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|