|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/* |
|
4
|
|
|
* @link http://github.com/seboettg/citeproc-php for the source repository |
|
5
|
|
|
* @copyright Copyright (c) 2019 Sebastian Böttger. |
|
6
|
|
|
* @license https://opensource.org/licenses/MIT |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Seboettg\CiteProc\Constraint; |
|
10
|
|
|
|
|
11
|
|
|
use Seboettg\Collection\ArrayList; |
|
12
|
|
|
use stdClass; |
|
13
|
|
|
|
|
14
|
|
|
abstract class AbstractConstraint implements Constraint |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $match; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ArrayList\ArrayListInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $conditionVariables; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $variable |
|
29
|
|
|
* @param stdClass $data; |
|
30
|
|
|
* @return bool |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract protected function matchForVariable(string $variable, stdClass $data): bool; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Variable constructor. |
|
36
|
|
|
* @param string $variableValues |
|
37
|
|
|
* @param string $match |
|
38
|
|
|
*/ |
|
39
|
70 |
|
public function __construct(string $variableValues, string $match = "any") |
|
40
|
|
|
{ |
|
41
|
70 |
|
$this->conditionVariables = new ArrayList(...explode(" ", $variableValues)); |
|
42
|
70 |
|
$this->match = $match; |
|
43
|
70 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param stdClass $data |
|
47
|
|
|
* @param int|null $citationNumber |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
63 |
|
public function validate(stdClass $data, int $citationNumber = null): bool |
|
51
|
|
|
{ |
|
52
|
63 |
|
switch ($this->match) { |
|
53
|
|
|
case Constraint::MATCH_ALL: |
|
54
|
58 |
|
return $this->matchAll($data); |
|
55
|
|
|
case Constraint::MATCH_NONE: |
|
56
|
33 |
|
return $this->matchNone($data); //no match for any value |
|
57
|
|
|
case Constraint::MATCH_ANY: |
|
58
|
|
|
default: |
|
59
|
46 |
|
return $this->matchAny($data); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
46 |
|
private function matchAny(stdClass $data): bool |
|
64
|
|
|
{ |
|
65
|
46 |
|
return $this->conditionVariables |
|
66
|
|
|
->map(function (string $conditionVariable) use ($data) { |
|
67
|
46 |
|
return $this->matchForVariable($conditionVariable, $data); |
|
68
|
46 |
|
}) |
|
69
|
|
|
->filter(function (bool $match) { |
|
70
|
46 |
|
return $match === true; |
|
71
|
46 |
|
}) |
|
72
|
46 |
|
->count() > 0; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
58 |
|
private function matchAll(stdClass $data): bool |
|
76
|
|
|
{ |
|
77
|
58 |
|
return $this->conditionVariables |
|
78
|
|
|
->map(function (string $conditionVariable) use ($data) { |
|
79
|
58 |
|
return $this->matchForVariable($conditionVariable, $data); |
|
80
|
58 |
|
}) |
|
81
|
|
|
->filter(function (bool $match) { |
|
82
|
58 |
|
return $match === true; |
|
83
|
58 |
|
}) |
|
84
|
58 |
|
->count() === $this->conditionVariables->count(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
33 |
|
private function matchNone(stdClass $data): bool |
|
88
|
|
|
{ |
|
89
|
33 |
|
return $this->conditionVariables |
|
90
|
|
|
->map(function (string $conditionVariable) use ($data) { |
|
91
|
33 |
|
return $this->matchForVariable($conditionVariable, $data); |
|
92
|
33 |
|
}) |
|
93
|
|
|
->filter(function (bool $match) { |
|
94
|
33 |
|
return $match === false; |
|
95
|
33 |
|
}) |
|
96
|
33 |
|
->count() === $this->conditionVariables->count(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|