|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @license GNU AGPL version 3 or any later version |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
10
|
|
|
* License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OCA\WorkflowEngine\Check; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
use OCP\Files\Storage\IStorage; |
|
26
|
|
|
use OCP\IL10N; |
|
27
|
|
|
use OCP\WorkflowEngine\ICheck; |
|
28
|
|
|
|
|
29
|
|
|
abstract class AbstractStringCheck implements ICheck { |
|
30
|
|
|
|
|
31
|
|
|
/** @var array[] Nested array: [Pattern => [ActualValue => Regex Result]] */ |
|
32
|
|
|
protected $matches; |
|
33
|
|
|
|
|
34
|
|
|
/** @var IL10N */ |
|
35
|
|
|
protected $l; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param IL10N $l |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(IL10N $l) { |
|
41
|
|
|
$this->l = $l; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param IStorage $storage |
|
46
|
|
|
* @param string $path |
|
47
|
|
|
*/ |
|
48
|
|
|
public function setFileInfo(IStorage $storage, $path) { |
|
49
|
|
|
// Nothing changes here with a different path |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
abstract protected function getActualValue(); |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $operator |
|
59
|
|
|
* @param string $value |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
|
|
public function executeCheck($operator, $value) { |
|
63
|
|
|
$actualValue = $this->getActualValue(); |
|
64
|
|
|
return $this->executeStringCheck($operator, $value, $actualValue); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $operator |
|
69
|
|
|
* @param string $checkValue |
|
70
|
|
|
* @param string $actualValue |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function executeStringCheck($operator, $checkValue, $actualValue) { |
|
74
|
|
|
if ($operator === 'is') { |
|
75
|
|
|
return $checkValue === $actualValue; |
|
76
|
|
|
} else if ($operator === '!is') { |
|
77
|
|
|
return $checkValue !== $actualValue; |
|
78
|
|
|
} else { |
|
79
|
|
|
$match = $this->match($checkValue, $actualValue); |
|
80
|
|
|
if ($operator === 'matches') { |
|
81
|
|
|
return $match === 1; |
|
82
|
|
|
} else { |
|
83
|
|
|
return $match === 0; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $operator |
|
90
|
|
|
* @param string $value |
|
91
|
|
|
* @throws \UnexpectedValueException |
|
92
|
|
|
*/ |
|
93
|
|
|
public function validateCheck($operator, $value) { |
|
94
|
|
View Code Duplication |
if (!in_array($operator, ['is', '!is', 'matches', '!matches'])) { |
|
|
|
|
|
|
95
|
|
|
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (in_array($operator, ['matches', '!matches']) && |
|
99
|
|
|
@preg_match($value, null) === false) { |
|
100
|
|
|
throw new \UnexpectedValueException($this->l->t('The given regular expression is invalid'), 2); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $pattern |
|
106
|
|
|
* @param string $subject |
|
107
|
|
|
* @return int|bool |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function match($pattern, $subject) { |
|
110
|
|
|
$patternHash = md5($pattern); |
|
111
|
|
|
$subjectHash = md5($subject); |
|
112
|
|
|
if (isset($this->matches[$patternHash][$subjectHash])) { |
|
113
|
|
|
return $this->matches[$patternHash][$subjectHash]; |
|
114
|
|
|
} |
|
115
|
|
|
if (!isset($this->matches[$patternHash])) { |
|
116
|
|
|
$this->matches[$patternHash] = []; |
|
117
|
|
|
} |
|
118
|
|
|
$this->matches[$patternHash][$subjectHash] = preg_match($pattern, $subject); |
|
119
|
|
|
return $this->matches[$patternHash][$subjectHash]; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.