|
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\Constraint\Constraint; |
|
14
|
|
|
use Seboettg\CiteProc\Constraint\Factory; |
|
15
|
|
|
use Seboettg\CiteProc\Data\DataList; |
|
16
|
|
|
use Seboettg\CiteProc\Exception\ClassNotFoundException; |
|
17
|
|
|
use Seboettg\CiteProc\Exception\InvalidStylesheetException; |
|
18
|
|
|
use Seboettg\CiteProc\Rendering\Group; |
|
19
|
|
|
use Seboettg\CiteProc\Rendering\HasParent; |
|
20
|
|
|
use Seboettg\CiteProc\Rendering\Rendering; |
|
21
|
|
|
use Seboettg\Collection\ArrayList; |
|
22
|
|
|
use SimpleXMLElement; |
|
23
|
|
|
|
|
24
|
|
|
class ChooseIf implements Rendering, HasParent |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var ArrayList<Constraint>|Constraint[] |
|
28
|
|
|
*/ |
|
29
|
|
|
private $constraints; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ArrayList |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $children; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private $match; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $parent; |
|
45
|
|
|
/** |
|
46
|
|
|
* @param SimpleXMLElement $node |
|
47
|
|
|
* @param Choose $parent |
|
48
|
|
|
* @throws InvalidStylesheetException |
|
49
|
|
|
* @throws ClassNotFoundException |
|
50
|
|
|
*/ |
|
51
|
70 |
|
public function __construct(SimpleXMLElement $node, Choose $parent) |
|
52
|
|
|
{ |
|
53
|
70 |
|
$this->parent = $parent; |
|
54
|
70 |
|
$this->constraints = new ArrayList(); |
|
55
|
70 |
|
$this->children = new ArrayList(); |
|
56
|
70 |
|
$this->match = (string) $node['match']; |
|
57
|
70 |
|
if (empty($this->match)) { |
|
58
|
69 |
|
$this->match = Constraint::MATCH_ALL; |
|
59
|
|
|
} |
|
60
|
70 |
|
foreach ($node->attributes() as $name => $value) { |
|
61
|
70 |
|
if ('match' !== $name) { |
|
62
|
70 |
|
$this->constraints->append(Factory::createConstraint((string) $name, (string) $value, $this->match)); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
70 |
|
foreach ($node->children() as $child) { |
|
66
|
70 |
|
$this->children->append(Factory::create($child, $this)); |
|
67
|
|
|
} |
|
68
|
70 |
|
} |
|
69
|
|
|
/** |
|
70
|
|
|
* @param array|DataList $data |
|
71
|
|
|
* @param null|int $citationNumber |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
63 |
|
public function render($data, $citationNumber = null): string |
|
75
|
|
|
{ |
|
76
|
63 |
|
$ret = []; |
|
77
|
|
|
/** @var Rendering $child */ |
|
78
|
63 |
|
foreach ($this->children as $child) { |
|
79
|
63 |
|
$ret[] = $child->render($data, $citationNumber); |
|
80
|
|
|
} |
|
81
|
63 |
|
$glue = ""; |
|
82
|
63 |
|
$parent = $this->parent->getParent(); |
|
83
|
63 |
|
if ($parent instanceof Group && $parent->hasDelimiter()) { |
|
84
|
26 |
|
$glue = $parent->getDelimiter(); |
|
85
|
|
|
} |
|
86
|
63 |
|
return implode($glue, array_filter($ret)); |
|
87
|
|
|
} |
|
88
|
|
|
/** |
|
89
|
|
|
* @param $data |
|
90
|
|
|
* @param null|int $citationNumber |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
63 |
|
public function match($data, int $citationNumber = null): bool |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
63 |
|
if ($this->constraints->count() === 1) { |
|
96
|
62 |
|
return $this->constraints->current()->validate($data); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
22 |
|
switch ($this->match) { |
|
100
|
22 |
|
case Constraint::MATCH_ANY: |
|
101
|
13 |
|
return $this->constraints |
|
102
|
|
|
->map(function (Constraint $constraint) use ($data) { |
|
103
|
13 |
|
return $constraint->validate($data); |
|
104
|
13 |
|
}) |
|
105
|
|
|
->filter(function (bool $match) { |
|
106
|
13 |
|
return $match === true; |
|
107
|
13 |
|
}) |
|
108
|
13 |
|
->count() > 0; |
|
109
|
21 |
|
case Constraint::MATCH_ALL: |
|
110
|
20 |
|
return $this->constraints |
|
111
|
|
|
->map(function (Constraint $constraint) use ($data) { |
|
112
|
20 |
|
return $constraint->validate($data); |
|
113
|
20 |
|
}) |
|
114
|
|
|
->filter(function (bool $match) { |
|
115
|
20 |
|
return $match === true; |
|
116
|
20 |
|
}) |
|
117
|
20 |
|
->count() === $this->constraints->count(); |
|
118
|
5 |
|
case Constraint::MATCH_NONE: |
|
119
|
5 |
|
return !$this->constraints |
|
120
|
|
|
->map(function (Constraint $constraint) use ($data) { |
|
121
|
5 |
|
return $constraint->validate($data); |
|
122
|
5 |
|
}) |
|
123
|
|
|
->filter(function (bool $match) { |
|
124
|
5 |
|
return $match === false; |
|
125
|
5 |
|
}) |
|
126
|
5 |
|
->count() === $this->constraints->count(); |
|
127
|
|
|
} |
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @noinspection PhpUnused |
|
134
|
|
|
* @return Choose |
|
135
|
|
|
*/ |
|
136
|
5 |
|
public function getParent(): Choose |
|
137
|
|
|
{ |
|
138
|
5 |
|
return $this->parent; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.