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
|
|
|
use function Seboettg\Collection\Lists\emptyList; |
24
|
|
|
|
25
|
|
|
class ChooseIf implements Rendering, HasParent |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ArrayList<Constraint>|Constraint[] |
29
|
|
|
*/ |
30
|
|
|
private $constraints; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ArrayList |
34
|
|
|
*/ |
35
|
|
|
protected $children; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $match; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var |
44
|
|
|
*/ |
45
|
|
|
protected $parent; |
46
|
|
|
/** |
47
|
|
|
* @param SimpleXMLElement $node |
48
|
|
|
* @param Choose $parent |
49
|
|
|
* @throws InvalidStylesheetException |
50
|
|
|
* @throws ClassNotFoundException |
51
|
|
|
*/ |
52
|
|
|
public function __construct(SimpleXMLElement $node, Choose $parent) |
53
|
|
|
{ |
54
|
|
|
$this->parent = $parent; |
55
|
|
|
$this->constraints = emptyList(); |
|
|
|
|
56
|
|
|
$this->children = emptyList(); |
|
|
|
|
57
|
|
|
$this->match = (string) $node["match"]; |
58
|
|
|
if (empty($this->match)) { |
59
|
|
|
$this->match = Constraint::MATCH_ALL; |
60
|
|
|
} |
61
|
|
|
foreach ($node->attributes() as $name => $value) { |
62
|
|
|
if ("match" !== $name) { |
63
|
|
|
$this->constraints->add(Factory::createConstraint((string) $name, (string) $value, $this->match)); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
foreach ($node->children() as $child) { |
67
|
|
|
$this->children->add(Factory::create($child, $this)); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
/** |
71
|
|
|
* @param array|DataList $data |
72
|
|
|
* @param null|int $citationNumber |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function render($data, $citationNumber = null): string |
76
|
|
|
{ |
77
|
|
|
$ret = []; |
78
|
|
|
/** @var Rendering $child */ |
79
|
|
|
foreach ($this->children as $child) { |
80
|
|
|
$ret[] = $child->render($data, $citationNumber); |
81
|
|
|
} |
82
|
|
|
$glue = ""; |
83
|
|
|
$parent = $this->parent->getParent(); |
84
|
|
|
if ($parent instanceof Group && $parent->hasDelimiter()) { |
85
|
|
|
$glue = $parent->getDelimiter(); |
86
|
|
|
} |
87
|
|
|
return implode($glue, array_filter($ret)); |
88
|
|
|
} |
89
|
|
|
/** |
90
|
|
|
* @param $data |
91
|
|
|
* @param null|int $citationNumber |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function match($data, int $citationNumber = null): bool |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
if ($this->constraints->count() === 1) { |
97
|
|
|
return $this->constraints->current()->validate($data); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
switch ($this->match) { |
101
|
|
|
case Constraint::MATCH_ANY: |
102
|
|
|
return $this->constraints |
103
|
|
|
->map(fn (Constraint $constraint) => $constraint->validate($data)) |
104
|
|
|
->filter(fn (bool $match) => $match == true) |
|
|
|
|
105
|
|
|
->count() > 0; |
106
|
|
|
case Constraint::MATCH_ALL: |
107
|
|
|
return $this->constraints |
108
|
|
|
->map(fn (Constraint $constraint) => $constraint->validate($data)) |
109
|
|
|
->filter(fn (bool $match) => $match == true) |
|
|
|
|
110
|
|
|
->count() == $this->constraints->count(); |
111
|
|
|
case Constraint::MATCH_NONE: |
112
|
|
|
return !$this->constraints |
113
|
|
|
->map(fn (Constraint $constraint) => $constraint->validate($data)) |
114
|
|
|
->filter(fn (bool $match) => $match == false) |
|
|
|
|
115
|
|
|
->count() == $this->constraints->count(); |
116
|
|
|
} |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @noinspection PhpUnused |
123
|
|
|
* @return Choose |
124
|
|
|
*/ |
125
|
|
|
public function getParent(): Choose |
126
|
|
|
{ |
127
|
|
|
return $this->parent; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..