|
1
|
|
|
<?php /** @noinspection PhpUnused */ |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* citeproc-php: AbstractConstraint.php |
|
5
|
|
|
* User: Sebastian Böttger <[email protected]> |
|
6
|
|
|
* created at 26.10.19, 14:12 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Seboettg\CiteProc\Constraint; |
|
10
|
|
|
|
|
11
|
|
|
use stdClass; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class AbstractConstraint |
|
15
|
|
|
* @package Seboettg\CiteProc\Constraint |
|
|
|
|
|
|
16
|
|
|
* @noinspection PhpUnused |
|
17
|
|
|
*/ |
|
|
|
|
|
|
18
|
|
|
abstract class AbstractConstraint implements Constraint |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
|
|
|
|
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $match; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
|
|
|
|
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $conditionVariables; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
|
|
|
|
|
32
|
|
|
* @param string $variable |
|
|
|
|
|
|
33
|
|
|
* @param stdClass $data; |
|
|
|
|
|
|
34
|
|
|
* @return bool |
|
|
|
|
|
|
35
|
|
|
*/ |
|
36
|
|
|
protected abstract function matchForVariable($variable, $data); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Variable constructor. |
|
40
|
|
|
* @param string $value |
|
|
|
|
|
|
41
|
|
|
* @param string $match |
|
42
|
|
|
*/ |
|
43
|
|
|
/** @noinspection PhpUnused */ |
|
|
|
|
|
|
44
|
45 |
|
public function __construct($value, $match = "any") |
|
45
|
|
|
{ |
|
46
|
45 |
|
$this->conditionVariables = explode(" ", $value); |
|
47
|
45 |
|
$this->match = $match; |
|
48
|
45 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
|
|
|
|
|
51
|
|
|
* @param $value |
|
|
|
|
|
|
52
|
|
|
* @param int|null $citationNumber |
|
|
|
|
|
|
53
|
|
|
* @return bool |
|
|
|
|
|
|
54
|
|
|
*/ |
|
55
|
39 |
|
public function validate($value, $citationNumber = null) |
|
56
|
|
|
{ |
|
57
|
39 |
|
switch ($this->match) { |
|
58
|
39 |
|
case Constraint::MATCH_ALL: |
|
|
|
|
|
|
59
|
37 |
|
return $this->matchAll($value); |
|
60
|
28 |
|
case Constraint::MATCH_NONE: |
|
|
|
|
|
|
61
|
17 |
|
return !$this->matchAny($value); //no match for any value |
|
62
|
26 |
|
case Constraint::MATCH_ANY: |
|
|
|
|
|
|
63
|
|
|
default: |
|
|
|
|
|
|
64
|
26 |
|
return $this->matchAny($value); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
28 |
|
private function matchAny($value) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
28 |
|
$conditionMatched = false; |
|
71
|
28 |
|
foreach ($this->conditionVariables as $variable) { |
|
72
|
28 |
|
$conditionMatched |= $this->matchForVariable($variable, $value); |
|
73
|
|
|
} |
|
74
|
28 |
|
return (bool)$conditionMatched; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
37 |
|
private function matchAll($value) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
37 |
|
$conditionMatched = true; |
|
80
|
37 |
|
foreach ($this->conditionVariables as $variable) { |
|
81
|
37 |
|
$conditionMatched &= $this->matchForVariable($variable, $value); |
|
82
|
|
|
} |
|
83
|
37 |
|
return (bool)$conditionMatched; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|