This repository was archived by the owner on Jul 8, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathPathNormalizer.php
More file actions
171 lines (151 loc) · 4.57 KB
/
Copy pathPathNormalizer.php
File metadata and controls
171 lines (151 loc) · 4.57 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/*
* This file is part of the Pathogen package.
*
* Copyright © 2014 Erin Millard
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
namespace Eloquent\Pathogen\Normalizer;
use Eloquent\Pathogen\AbsolutePathInterface;
use Eloquent\Pathogen\AbstractPath;
use Eloquent\Pathogen\Factory\PathFactory;
use Eloquent\Pathogen\Factory\PathFactoryInterface;
use Eloquent\Pathogen\PathInterface;
use Eloquent\Pathogen\RelativePathInterface;
/**
* A path normalizer suitable for generic, Unix-style path instances.
*/
class PathNormalizer implements PathNormalizerInterface
{
/**
* Get a static instance of this path normalizer.
*
* @return PathNormalizerInterface The static path normalizer.
*/
public static function instance()
{
if (null === self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Construct a new path normalizer.
*
* @param PathFactoryInterface|null $factory The path factory to use.
*/
public function __construct(PathFactoryInterface $factory = null)
{
if (null === $factory) {
$factory = PathFactory::instance();
}
$this->factory = $factory;
}
/**
* Get the path factory used by this normalizer.
*
* @return PathFactoryInterface The path factory.
*/
public function factory()
{
return $this->factory;
}
/**
* Normalize the supplied path to its most canonical form.
*
* @param PathInterface $path The path to normalize.
*
* @return PathInterface The normalized path.
*/
public function normalize(PathInterface $path)
{
if ($path instanceof AbsolutePathInterface) {
return $this->normalizeAbsolutePath($path);
}
return $this->normalizeRelativePath($path);
}
/**
* Normalize the supplied absolute path.
*
* @param AbsolutePathInterface $path The path to normalize.
*
* @return AbsolutePathInterface The normalized path.
*/
protected function normalizeAbsolutePath(AbsolutePathInterface $path)
{
return $this->factory()->createFromAtoms(
$this->normalizeAbsolutePathAtoms($path->atoms()),
true,
false
);
}
/**
* Normalize the supplied relative path.
*
* @param RelativePathInterface $path The path to normalize.
*
* @return RelativePathInterface The normalized path.
*/
protected function normalizeRelativePath(RelativePathInterface $path)
{
return $this->factory()->createFromAtoms(
$this->normalizeRelativePathAtoms($path->atoms()),
false,
false
);
}
/**
* Normalize the supplied path atoms for an absolute path.
*
* @param array<string> $atoms The path atoms to normalize.
*
* @return array<string> The normalized path atoms.
*/
protected function normalizeAbsolutePathAtoms(array $atoms)
{
$resultingAtoms = array();
foreach ($atoms as $atom) {
if (AbstractPath::PARENT_ATOM === $atom) {
array_pop($resultingAtoms);
} elseif (AbstractPath::SELF_ATOM !== $atom) {
$resultingAtoms[] = $atom;
}
}
return $resultingAtoms;
}
/**
* Normalize the supplied path atoms for a relative path.
*
* @param array<string> $atoms The path atoms to normalize.
*
* @return array<string> The normalized path atoms.
*/
protected function normalizeRelativePathAtoms(array $atoms)
{
$resultingAtoms = array();
$resultingAtomsCount = 0;
$numAtoms = count($atoms);
for ($i = 0; $i < $numAtoms; $i++) {
if (AbstractPath::SELF_ATOM !== $atoms[$i]) {
$resultingAtoms[] = $atoms[$i];
$resultingAtomsCount++;
}
if (
$resultingAtomsCount > 1 &&
AbstractPath::PARENT_ATOM === $resultingAtoms[$resultingAtomsCount - 1] &&
AbstractPath::PARENT_ATOM !== $resultingAtoms[$resultingAtomsCount - 2]
) {
array_splice($resultingAtoms, $resultingAtomsCount - 2, 2);
$resultingAtomsCount -= 2;
}
}
if (count($resultingAtoms) < 1) {
$resultingAtoms = array(AbstractPath::SELF_ATOM);
}
return $resultingAtoms;
}
private static $instance;
private $factory;
}