forked from peoplepath/homework-modern-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserFilter.php
More file actions
59 lines (53 loc) · 1.63 KB
/
UserFilter.php
File metadata and controls
59 lines (53 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* Class UserFilter
* Allows ignore and replacement pattern
*/
class UserFilter
{
private string $ignorePattern; // pattern marking string to be ignored
private string $replacePattern; // pattern marking substring for replacement
private string $replacement; // replacement for previous pattern
/**
* @param $ignorePattern String pattern
*/
public function setIgnorePattern(string $ignorePattern)
{
$this->ignorePattern = $ignorePattern;
}
/**
* @param string $replacePattern
* @param string $replacement
*/
public function setReplacePattern(string $replacePattern, string $replacement)
{
$this->replacePattern = $replacePattern;
$this->replacement = $replacement;
}
/**
* Test provided string against ignore pattern
* @param string $line to be tested
* @return bool true if $line should be ignored
*/
public function testIgnorePattern(string $line): bool
{
if (isset($this->ignorePattern)) {
return preg_match($this->ignorePattern, $line);
} else {
return false;
}
}
/**
* Performs replacement on line with replacePattern for replacement
* @param string $line for replacement search
* @return string line with replaced string or unchanged original line if pattern is not found
*/
public function performReplace(string $line): string
{
if (isset($this->replacePattern)) {
return preg_replace($this->replacePattern, $this->replacement, $line);
} else {
return $line;
}
}
}