Completed
Push — master ( f51140...ee6dc6 )
by Frank
04:13
created

Util::removeFunkyWhiteSpace()   A

↳ Parent: Util

Complexity

Conditions 2
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 9
Code Lines 4

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace League\Flysystem;
4
5
use League\Flysystem\Util\MimeType;
6
use LogicException;
7
8
class Util
9
{
10
    /**
11
     * Get normalized pathinfo.
12
     *
13
     * @param string $path
14
     *
15
     * @return array pathinfo
16
     */
17 12
    public static function pathinfo($path)
18
    {
19 12
        $pathinfo = pathinfo($path) + compact('path');
20 12
        $pathinfo['dirname'] = array_key_exists('dirname', $pathinfo)
21 12
            ? static::normalizeDirname($pathinfo['dirname']) : '';
22
23 12
        return $pathinfo;
24
    }
25
26
    /**
27
     * Normalize a dirname return value.
28
     *
29
     * @param string $dirname
30
     *
31
     * @return string normalized dirname
32
     */
33 36
    public static function normalizeDirname($dirname)
34
    {
35 36
        return $dirname === '.' ? '' : $dirname;
36
    }
37
38
    /**
39
     * Get a normalized dirname from a path.
40
     *
41
     * @param string $path
42
     *
43
     * @return string dirname
44
     */
45 33
    public static function dirname($path)
46
    {
47 33
        return static::normalizeDirname(dirname($path));
48
    }
49
50
    /**
51
     * Map result arrays.
52
     *
53
     * @param array $object
54
     * @param array $map
55
     *
56
     * @return array mapped result
57
     */
58 6
    public static function map(array $object, array $map)
59
    {
60 6
        $result = [];
61
62 6
        foreach ($map as $from => $to) {
63 6
            if ( ! isset($object[$from])) {
64 3
                continue;
65
            }
66
67 6
            $result[$to] = $object[$from];
68 6
        }
69
70 6
        return $result;
71
    }
72
73
    /**
74
     * Normalize path.
75
     *
76
     * @param string $path
77
     *
78
     * @throws LogicException
79
     *
80
     * @return string
81
     */
82 279
    public static function normalizePath($path)
83
    {
84 279
        return static::normalizeRelativePath($path);
85
    }
86
87
    /**
88
     * Normalize relative directories in a path.
89
     *
90
     * @param string $path
91
     *
92
     * @throws LogicException
93
     *
94
     * @return string
95
     */
96 279
    public static function normalizeRelativePath($path)
97
    {
98 279
        $path = str_replace('\\', '/', $path);
99 279
        $path = static::removeFunkyWhiteSpace($path);
100
101 279
        $parts = [];
102
103 279
        foreach (explode('/', $path) as $part) {
104
            switch ($part) {
105 279
                case '':
106 279
                case '.':
107 66
                break;
108
109 270
            case '..':
110 30
                if (empty($parts)) {
111 15
                    throw new LogicException(
112 15
                        'Path is outside of the defined root, path: [' . $path . ']'
113 15
                    );
114
                }
115 27
                array_pop($parts);
116 27
                break;
117
118 267
            default:
119 267
                $parts[] = $part;
120 267
                break;
121 267
            }
122 276
        }
123
124 264
        return implode('/', $parts);
125
    }
126
127
    /**
128
     * Removes unprintable characters and invalid unicode characters.
129
     *
130
     * @param string $path
131
     *
132
     * @return string $path
133
     */
134 279
    protected static function removeFunkyWhiteSpace($path) {
135
        // We do this check in a loop, since removing invalid unicode characters
136
        // can lead to new characters being created.
137 279
        while (preg_match('#\p{C}+|^\./#u', $path)) {
138 6
            $path = preg_replace('#\p{C}+|^\./#u', '', $path);
139 6
        }
140
141 279
        return $path;
142
    }
143
144
    /**
145
     * Normalize prefix.
146
     *
147
     * @param string $prefix
148
     * @param string $separator
149
     *
150
     * @return string normalized path
151
     */
152 3
    public static function normalizePrefix($prefix, $separator)
153
    {
154 3
        return rtrim($prefix, $separator) . $separator;
155
    }
156
157
    /**
158
     * Get content size.
159
     *
160
     * @param string $contents
161
     *
162
     * @return int content size
163
     */
164 3
    public static function contentSize($contents)
165
    {
166 3
        return defined('MB_OVERLOAD_STRING') ? mb_strlen($contents, '8bit') : strlen($contents);
167
    }
168
169
    /**
170
     * Guess MIME Type based on the path of the file and it's content.
171
     *
172
     * @param string $path
173
     * @param string|resource $content
174
     *
175
     * @return string|null MIME Type or NULL if no extension detected
176
     */
177 27
    public static function guessMimeType($path, $content)
178
    {
179 27
        $mimeType = MimeType::detectByContent($content);
180
181 27
        if ( ! (empty($mimeType) || in_array($mimeType, ['application/x-empty', 'text/plain', 'text/x-asm']))) {
182 3
            return $mimeType;
183
        }
184
185 24
        return MimeType::detectByFilename($path);
186
    }
187
188
    /**
189
     * Emulate directories.
190
     *
191
     * @param array $listing
192
     *
193
     * @return array listing with emulated directories
194
     */
195 3
    public static function emulateDirectories(array $listing)
196
    {
197 3
        $directories = [];
198 3
        $listedDirectories = [];
199
200 3
        foreach ($listing as $object) {
201 3
            list($directories, $listedDirectories) = static::emulateObjectDirectories(
202 3
                $object,
203 3
                $directories,
204
                $listedDirectories
205 3
            );
206 3
        }
207
208 3
        $directories = array_diff(array_unique($directories), array_unique($listedDirectories));
209
210 3
        foreach ($directories as $directory) {
211 3
            $listing[] = static::pathinfo($directory) + ['type' => 'dir'];
212 3
        }
213
214 3
        return $listing;
215
    }
216
217
    /**
218
     * Ensure a Config instance.
219
     *
220
     * @param null|array|Config $config
221
     *
222
     * @return Config config instance
223
     *
224
     * @throw  LogicException
225
     */
226 147
    public static function ensureConfig($config)
227
    {
228 147
        if ($config === null) {
229 3
            return new Config();
230
        }
231
232 147
        if ($config instanceof Config) {
233 144
            return $config;
234
        }
235
236 6
        if (is_array($config)) {
237 3
            return new Config($config);
238
        }
239
240 3
        throw new LogicException('A config should either be an array or a Flysystem\Config object.');
241
    }
242
243
    /**
244
     * Rewind a stream.
245
     *
246
     * @param resource $resource
247
     */
248 36
    public static function rewindStream($resource)
249
    {
250 36
        if (ftell($resource) !== 0 && static::isSeekableStream($resource)) {
251 18
            rewind($resource);
252 18
        }
253 36
    }
254
255 18
    public static function isSeekableStream($resource)
256
    {
257 18
        $metadata = stream_get_meta_data($resource);
258
259 18
        return $metadata['seekable'];
260
    }
261
262
    /**
263
     * Get the size of a stream.
264
     *
265
     * @param resource $resource
266
     *
267
     * @return int stream size
268
     */
269 3
    public static function getStreamSize($resource)
270
    {
271 3
        $stat = fstat($resource);
272
273 3
        return $stat['size'];
274
    }
275
276
    /**
277
     * Emulate the directories of a single object.
278
     *
279
     * @param array $object
280
     * @param array $directories
281
     * @param array $listedDirectories
282
     *
283
     * @return array
284
     */
285 3
    protected static function emulateObjectDirectories(array $object, array $directories, array $listedDirectories)
286
    {
287 3
        if ($object['type'] === 'dir') {
288 3
            $listedDirectories[] = $object['path'];
289 3
        }
290
291 3
        if (empty($object['dirname'])) {
292 3
            return [$directories, $listedDirectories];
293
        }
294
295 3
        $parent = $object['dirname'];
296
297 3
        while ( ! empty($parent) && ! in_array($parent, $directories)) {
298 3
            $directories[] = $parent;
299 3
            $parent = static::dirname($parent);
300 3
        }
301
302 3
        if (isset($object['type']) && $object['type'] === 'dir') {
303 3
            $listedDirectories[] = $object['path'];
304
305 3
            return [$directories, $listedDirectories];
306
        }
307
308 3
        return [$directories, $listedDirectories];
309
    }
310
}
311