Completed
Push — master ( 7cb054...584ba8 )
by Tim
44:11 queued 26:38
created

Path::normalize()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 10
c 1
b 0
f 1
nc 8
nop 1
dl 0
loc 14
rs 8.2222
1
<?php
2
/*
3
 * Copyright (c) Ouzo contributors, http://ouzoframework.org
4
 * This file is made available under the MIT License (view the LICENSE file for more information).
5
 */
6
namespace Ouzo\Utilities;
7
8
/**
9
 * Class Path
10
 * @package Ouzo\Utilities
11
 */
12
class Path
13
{
14
    /**
15
     * Returns a path created by placing DIRECTORY_SEPARATOR between each argument
16
     *
17
     * Example:
18
     * <code>
19
     * $path = Path::join('/my', 'path', 'to/file.txt');
20
     * </code>
21
     * Result:
22
     * <code>
23
     * /my/path/to/file.txt
24
     * </code>
25
     *
26
     * @param string ...
27
     * @return mixed
28
     */
29
    public static function join()
30
    {
31
        $args = Arrays::filterNotBlank(func_get_args());
32
        return preg_replace('~[/\\\]+~', DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, $args));
33
    }
34
35
    /**
36
     * Returns a path starting in the system temporary directory.
37
     *
38
     * Example:
39
     * <code>
40
     * $path = Path::joinWithTemp('my/file.txt');
41
     * </code>
42
     * Result:
43
     * <code>
44
     * //Unix
45
     * /tmp/my/file.txt
46
     * </code>
47
     *
48
     * @param string ...
49
     * @return mixed
50
     */
51
    public static function joinWithTemp()
52
    {
53
        $args = array_merge(array(sys_get_temp_dir()), func_get_args());
54
        return call_user_func_array('\Ouzo\Utilities\Path::join', $args);
55
    }
56
57
    /**
58
     * Returns a normalized path.
59
     * Actions:
60
     * <ul>
61
     *      <li>removes dots from relative path</li>
62
     *      <li>removes dots from absolute path</li>
63
     *      <li>does not remove leading dots</li>
64
     *      <li>removes double slashes</li>
65
     * </ul>
66
     *
67
     * Example:
68
     * <code>
69
     * $normalized = Path::normalize('dir/../dir2/file.txt');
70
     * $normalized = Path::normalize('/tmp/../dir2/file.txt');
71
     * $normalized = Path::normalize('../file.txt');
72
     * $normalized = Path::normalize('//dir/file.txt');
73
     * </code>
74
     * Result:
75
     * <code>
76
     * dir2/file.txt
77
     * /dir2/file.txt
78
     * ../file.txt
79
     * /dir/file.txt
80
     * </code>
81
     *
82
     * @param string $path
83
     * @return string
84
     */
85
    public static function normalize($path)
86
    {
87
        $parts = explode('/', trim($path, '/'));
88
        $result = array();
89
        foreach ($parts as $part) {
90
            if ($part == '..' && !empty($result)) {
91
                array_pop($result);
92
            } elseif ($part != '.' && !empty($part)) {
93
                array_push($result, $part);
94
            }
95
        }
96
        $root = $path[0] == '/' ? '/' : '';
97
        return $root . implode('/', $result);
98
    }
99
}
100