1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Shared |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Shared\Globbing; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* GlobbingTrait |
19
|
|
|
* |
20
|
|
|
* Matches exact name like 'user.login' with 'user.*', 'u*.*' etc. |
21
|
|
|
* |
22
|
|
|
* @package Phossa2\Shared |
23
|
|
|
* @author Hong Zhang <[email protected]> |
24
|
|
|
* @version 2.0.25 |
25
|
|
|
* @since 2.0.20 added |
26
|
|
|
* @since 2.0.25 ending '*' now matches everything |
27
|
|
|
*/ |
28
|
|
|
trait GlobbingTrait |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Returns all names matches with $exactName |
32
|
|
|
* |
33
|
|
|
* e.g. |
34
|
|
|
* 'user.login' matches '*', 'u*.*', 'user.*', 'user.l*', 'user.login' etc. |
35
|
|
|
* |
36
|
|
|
* @param string $exactName |
37
|
|
|
* @param array $names |
38
|
|
|
* @return array |
39
|
|
|
* @access protected |
40
|
|
|
*/ |
41
|
|
|
protected function globbingNames( |
42
|
|
|
/*# string */ $exactName, |
43
|
|
|
array $names |
44
|
|
|
)/*# : array */ { |
45
|
|
|
$result = []; |
46
|
|
|
foreach ($names as $name) { |
47
|
|
|
if ($this->nameGlobbing($exactName, $name)) { |
48
|
|
|
$result[] = $name; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
return $result; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check to see if $name matches with $exactName |
56
|
|
|
* |
57
|
|
|
* e.g. |
58
|
|
|
* ```php |
59
|
|
|
* // true |
60
|
|
|
* $this->nameGlobbing('user.*', 'user.login'); |
61
|
|
|
* |
62
|
|
|
* // true |
63
|
|
|
* $this->nameGlobbing('*', 'user.login'); |
64
|
|
|
* |
65
|
|
|
* // false |
66
|
|
|
* $this->nameGLobbing('blog.*', 'user.login'); |
67
|
|
|
* ``` |
68
|
|
|
* |
69
|
|
|
* @param string $exactName |
70
|
|
|
* @param string $name |
71
|
|
|
* @return bool |
72
|
|
|
* @access protected |
73
|
|
|
*/ |
74
|
|
|
protected function nameGlobbing( |
75
|
|
|
/*# string */ $exactName, |
76
|
|
|
/*# string */ $name |
77
|
|
|
)/*# : bool */ { |
78
|
|
|
if ($name === $exactName) { |
79
|
|
|
return true; |
80
|
|
|
} elseif (false !== strpos($name, '*')) { |
81
|
|
|
$pat = str_replace(array('.', '*'), array('[.]', '[^.]*+'), $name); |
82
|
|
|
// last '*' should be different |
83
|
|
|
$pat = substr($pat, -6) != '[^.]*+' ? $pat : (substr($pat, 0, -6) . '.*+'); |
84
|
|
|
return (bool) preg_match('~^' . $pat . '$~', $exactName); |
85
|
|
|
} else { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|