1 | <?php |
||
14 | class PathCollisionDetector |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * Known paths. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | private $_knownPaths = array(); |
||
23 | |||
24 | /** |
||
25 | * Expanded array of paths. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | private $_expandedPaths = array('/' => 1); |
||
30 | |||
31 | /** |
||
32 | * Longest path length. |
||
33 | * |
||
34 | * @var integer |
||
35 | */ |
||
36 | private $_longestPathLength = 1; |
||
37 | |||
38 | /** |
||
39 | * Adds paths. |
||
40 | * |
||
41 | * @param array $paths Paths. |
||
42 | * |
||
43 | * @return void |
||
44 | */ |
||
45 | 7 | public function addPaths(array $paths) |
|
46 | { |
||
47 | 7 | foreach ( $paths as $path ) { |
|
48 | 7 | $parent_path = rtrim($path, '/'); |
|
49 | 7 | $this->_knownPaths[$path] = true; |
|
50 | |||
51 | do { |
||
52 | 7 | $this->_expandedPaths[$parent_path . '/'] = strlen($parent_path . '/'); |
|
53 | 7 | $parent_path = dirname($parent_path); |
|
54 | 7 | } while ( $parent_path !== '/' && $parent_path !== '' ); |
|
55 | 7 | } |
|
56 | |||
57 | 7 | $path_count = count($this->_expandedPaths); |
|
58 | |||
59 | 7 | if ( $path_count > 1 ) { |
|
60 | 5 | $this->_longestPathLength = call_user_func_array('max', $this->_expandedPaths); |
|
61 | 5 | } |
|
62 | 2 | elseif ( $path_count === 1 ) { |
|
63 | 2 | $this->_longestPathLength = current($this->_expandedPaths); |
|
64 | 2 | } |
|
65 | else { |
||
66 | $this->_longestPathLength = 0; // @codeCoverageIgnore |
||
67 | } |
||
68 | 7 | } |
|
69 | |||
70 | /** |
||
71 | * Checks path collision. |
||
72 | * |
||
73 | * @param string $path Path. |
||
74 | * |
||
75 | * @return boolean |
||
76 | */ |
||
77 | 9 | public function isCollision($path) |
|
87 | |||
88 | } |
||
89 |