-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPattern.class.php
More file actions
77 lines (66 loc) · 1.7 KB
/
Pattern.class.php
File metadata and controls
77 lines (66 loc) · 1.7 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* NAME:
* Pattern
*
* DESCRIPTION:
*
* Pattern class represents a valid pattern. Providing method to match
* paths.
*
*/
class Pattern
{
const PCRE_DELIMITER = '|';
protected $_pattern = '';
/**
* constructor
*
* @param $str path string
*/
public function __construct($str)
{
$this->_pattern = trim($str);
}
public function getPattern()
{
return $this->_pattern;
}
/**
* match given path
*
* @param $path string Path name
* @param $type string algorithm type, default to PCRE
* (perl compatible regexp), more
* advanced algorithm could be used
* here for faster pattern match
*
* @return boolean true: path matchs
*/
public function match(Path $path, $type = 'PCRE')
{
$result = false;
switch($type){
case 'PCRE':
$result = $this->_pregMatch($path);
break;
default:
throw Exception('Invalid RegExp type');
}
return $result;
}
public function getPcrePattern()
{
return $this->_getPcrePattern();
}
//* implementation
protected function _pregMatch(Path $path)
{
$pcre = $this->_getPcrePattern();
return preg_match($pcre, $path->getPath());
}
protected function _getPcrePattern()
{
return self::PCRE_DELIMITER . '^' . str_replace(array(',','*'), array('/', '[^/]+'), $this->_pattern) . '$'. self::PCRE_DELIMITER;
}
}