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\Constraint\ConstraintInterface; |
13
|
|
|
use Seboettg\CiteProc\Constraint\Factory; |
14
|
|
|
use Seboettg\CiteProc\Rendering\HasParent; |
15
|
|
|
use Seboettg\CiteProc\Rendering\Rendering; |
16
|
|
|
use Seboettg\CiteProc\Rendering\RendersEmptyVariables; |
17
|
|
|
use Seboettg\CiteProc\Rendering\RendersEmptyVariablesTrait; |
18
|
|
|
use Seboettg\Collection\ArrayList; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class ChooseIf |
23
|
|
|
* @package Seboettg\CiteProc\Node\Choose |
24
|
|
|
* |
25
|
|
|
* @author Sebastian Böttger <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ChooseIf implements Rendering, HasParent, RendersEmptyVariables |
28
|
|
|
{ |
29
|
|
|
use RendersEmptyVariablesTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ArrayList<ConstraintInterface> |
33
|
|
|
*/ |
34
|
|
|
private $constraints; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ConstraintInterface |
38
|
|
|
*/ |
39
|
|
|
private $constraint; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var ArrayList |
43
|
|
|
*/ |
44
|
|
|
protected $children; |
45
|
|
|
|
46
|
|
|
private $match; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var |
50
|
|
|
*/ |
51
|
|
|
protected $parent; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param \SimpleXMLElement $node |
55
|
|
|
* @param Choose $parent |
56
|
|
|
*/ |
57
|
|
|
public function __construct(\SimpleXMLElement $node, $parent) |
58
|
|
|
{ |
59
|
|
|
$this->parent = $parent; |
60
|
|
|
$this->constraints = new ArrayList(); |
61
|
|
|
$this->children = new ArrayList(); |
62
|
|
|
$this->match = (string) $node['match']; |
63
|
|
|
|
64
|
|
|
if (empty($this->match)) { |
65
|
|
|
$this->match = "all"; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
foreach ($node->attributes() as $name => $value) { |
69
|
|
|
if ('match' !== $name) { |
70
|
|
|
$this->constraints->append(Factory::createConstraint((string) $name, (string) $value)); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
foreach ($node->children() as $child) { |
75
|
|
|
$this->children->append(Factory::create($child, $this)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array|\Seboettg\CiteProc\Data\DataList $data |
81
|
|
|
* @param null|int $citationNumber |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function render($data, $citationNumber = null) |
85
|
|
|
{ |
86
|
|
|
$ret = []; |
87
|
|
|
/** @var Rendering $child */ |
88
|
|
|
foreach ($this->children as $child) { |
89
|
|
|
$ret[] = $child->render($data, $citationNumber); |
90
|
|
|
} |
91
|
|
|
return implode("", $ret); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $data |
96
|
|
|
* @param null|int $citationNumber |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function match($data, $citationNumber = null) |
100
|
|
|
{ |
101
|
|
|
if (isset($this->constraint)) { |
102
|
|
|
return $this->constraint->validate($data); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$result = $this->match === "none" ? false : true; |
106
|
|
|
|
107
|
|
|
/** @var ConstraintInterface $constraint */ |
108
|
|
|
foreach ($this->constraints as $constraint) { |
109
|
|
|
if ($this->match === "any") { |
110
|
|
|
if ($constraint->validate($data, $citationNumber)) { |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
} else { |
114
|
|
|
$result &= $constraint->validate($data, $citationNumber); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($this->match === "all") { |
119
|
|
|
return (bool) $result; |
120
|
|
|
} else if ($this->match === "none") { |
121
|
|
|
return !$result; |
122
|
|
|
} |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return Choose |
128
|
|
|
*/ |
129
|
|
|
public function getParent() |
130
|
|
|
{ |
131
|
|
|
return $this->parent; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
public function rendersEmptyVariables($data) |
136
|
|
|
{ |
137
|
|
|
// TODO: Implement rendersEmptyVariables() method. |
138
|
|
|
} |
139
|
|
|
} |
|
|
|
|
140
|
|
|
|
This check marks files that end in a newline character, i.e. an empy line.