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\Lists\ListInterface; |
12
|
|
|
use stdClass; |
13
|
|
|
use function Seboettg\Collection\Lists\listOf; |
14
|
|
|
|
15
|
|
|
abstract class AbstractConstraint implements Constraint |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
protected string $match; |
19
|
|
|
|
20
|
|
|
protected ListInterface $conditionVariables; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $variable |
24
|
|
|
* @param stdClass $data; |
25
|
|
|
* @return bool |
26
|
|
|
*/ |
27
|
|
|
abstract protected function matchForVariable(string $variable, stdClass $data): bool; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Variable constructor. |
31
|
|
|
* @param string $variableValues |
32
|
|
|
* @param string $match |
33
|
|
|
*/ |
34
|
|
|
public function __construct(string $variableValues, string $match = "any") |
35
|
|
|
{ |
36
|
|
|
$this->conditionVariables = listOf(...explode(" ", $variableValues)); |
37
|
|
|
$this->match = $match; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param stdClass $data |
42
|
|
|
* @param int|null $citationNumber |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function validate(stdClass $data, int $citationNumber = null): bool |
46
|
|
|
{ |
47
|
|
|
switch ($this->match) { |
48
|
|
|
case Constraint::MATCH_ALL: |
49
|
|
|
return $this->matchAll($data); |
50
|
|
|
case Constraint::MATCH_NONE: |
51
|
|
|
return $this->matchNone($data); //no match for any value |
52
|
|
|
case Constraint::MATCH_ANY: |
53
|
|
|
default: |
54
|
|
|
return $this->matchAny($data); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function matchAny(stdClass $data): bool |
59
|
|
|
{ |
60
|
|
|
return $this->conditionVariables |
61
|
|
|
->any(fn (string $conditionVariable) => $this->matchForVariable($conditionVariable, $data)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function matchAll(stdClass $data): bool |
65
|
|
|
{ |
66
|
|
|
return $this->conditionVariables |
67
|
|
|
->all(fn (string $conditionVariable) => $this->matchForVariable($conditionVariable, $data)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function matchNone(stdClass $data): bool |
71
|
|
|
{ |
72
|
|
|
return $this->conditionVariables |
73
|
|
|
->all(fn (string $conditionVariable) => !$this->matchForVariable($conditionVariable, $data)); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|