Passed
Push — fix/rendering-log ( 7c5c18...9945e1 )
by Arnaud
12:56 queued 09:58
created

Util   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Importance

Changes 11
Bugs 2 Features 0
Metric Value
eloc 44
c 11
b 2
f 0
dl 0
loc 194
rs 10
wmc 21

10 Methods

Rating   Name   Duplication   Size   Complexity  
A dateToDatetime() 0 13 4
A formatClassName() 0 11 2
A getFS() 0 7 2
A isDateValid() 0 8 3
A isExternalUrl() 0 7 2
A isUrlFileExists() 0 8 2
A joinFile() 0 8 1
A joinPath() 0 8 1
A arrayToString() 0 9 2
A CombineArrayToString() 0 13 2
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil;
10
11
use Symfony\Component\Filesystem\Filesystem;
12
13
class Util
14
{
15
    /**
16
     * Symfony\Component\Filesystem.
17
     *
18
     * @var Filesystem
19
     */
20
    protected static $fs;
21
22
    /**
23
     * Return Symfony\Component\Filesystem instance.
24
     *
25
     * @return Filesystem
26
     */
27
    public static function getFS(): Filesystem
28
    {
29
        if (!self::$fs instanceof Filesystem) {
30
            self::$fs = new Filesystem();
31
        }
32
33
        return self::$fs;
34
    }
35
36
    /**
37
     * Checks if a date is valid.
38
     *
39
     * @param string|null $date
40
     * @param string      $format
41
     *
42
     * @return bool
43
     */
44
    public static function isDateValid($date, string $format = 'Y-m-d'): bool
45
    {
46
        if ($date === null) {
47
            return false;
48
        }
49
        $d = \DateTime::createFromFormat($format, $date);
50
51
        return $d && $d->format($format) === $date;
52
    }
53
54
    /**
55
     * Date to DateTime.
56
     *
57
     * @param mixed $date
58
     *
59
     * @return \DateTime
60
     */
61
    public static function dateToDatetime($date): \DateTime
62
    {
63
        // DateTime
64
        if ($date instanceof \DateTime) {
65
            return $date;
66
        }
67
        // timestamp or AAAA-MM-DD
68
        if (is_numeric($date)) {
69
            return (new \DateTime())->setTimestamp($date);
70
        }
71
        // string (ie: '01/01/2019', 'today')
72
        if (is_string($date)) {
73
            return new \DateTime($date);
74
        }
75
    }
76
77
    /**
78
     * Format class name.
79
     *
80
     * @param \object $class
81
     * @param array   $options
82
     *
83
     * @return string
84
     */
85
    public static function formatClassName($class, array $options = []): string
86
    {
87
        $lowercase = false;
88
        extract($options);
89
90
        $className = substr(strrchr(get_class($class), '\\'), 1);
91
        if ($lowercase) {
1 ignored issue
show
introduced by
The condition $lowercase is always false.
Loading history...
92
            $className = strtolower($className);
93
        }
94
95
        return $className;
96
    }
97
98
    /**
99
     * Test if a string is an external URL or not.
100
     *
101
     * @param string|null $url
102
     *
103
     * @return bool
104
     */
105
    public static function isExternalUrl($url): bool
106
    {
107
        if ($url === null) {
108
            return false;
109
        }
110
111
        return (bool) preg_match('~^(?:f|ht)tps?://~i', $url);
112
    }
113
114
    /**
115
     * Test if a remote file exists or not.
116
     *
117
     * @param string $remoteFile
118
     *
119
     * @return bool
120
     */
121
    public static function isUrlFileExists(string $remoteFile): bool
122
    {
123
        $handle = @fopen($remoteFile, 'r');
124
        if (is_resource($handle)) {
125
            return true;
126
        }
127
128
        return false;
129
    }
130
131
    /**
132
     * Convert an array of strings into a path.
133
     *
134
     * @param string $path
135
     *
136
     * @return string
137
     */
138
    public static function joinPath(string ...$path): string
139
    {
140
        array_walk($path, function (&$value) {
141
            $value = str_replace('\\', '/', $value);
142
            $value = rtrim($value, '/');
143
        });
144
145
        return implode('/', $path);
146
    }
147
148
    /**
149
     * Convert an array of strings into a system path.
150
     *
151
     * @param string $path
152
     *
153
     * @return string
154
     */
155
    public static function joinFile(string ...$path): string
156
    {
157
        array_walk($path, function (&$value) {
158
            $value = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $value);
159
            $value = rtrim($value, DIRECTORY_SEPARATOR);
160
        });
161
162
        return implode(DIRECTORY_SEPARATOR, $path);
163
    }
164
165
    /**
166
     * Convert array to string.
167
     *
168
     * @param array  $array
169
     * @param string $separator Separtor between the key and the value in the result string
170
     *
171
     * @return string
172
     */
173
    public static function arrayToString(array $array, string $separator = ':'): string
174
    {
175
        $string = '';
176
177
        foreach ($array as $key => $value) {
178
            $string .= sprintf('%s%s%s, ', $key, $separator, $value);
179
        }
180
181
        return substr($string, 0, -2);
182
    }
183
184
    /**
185
     * Conbine array to string.
186
     *
187
     * @param array  $array
188
     * @param string $keyToKey   the cuurrent key who become the key of the new array
189
     * @param string $keyToValue the cuurrent key who become the value of the new array
190
     * @param string $separator  Separtor between the key and the value in the result string
191
     *
192
     * @return string
193
     */
194
    public static function CombineArrayToString(
195
        array $array,
196
        string $keyToKey,
197
        string $keyToValue,
198
        string $separator = ':'
199
    ): string {
200
        $string = '';
201
202
        foreach ($array as $subArray) {
203
            $string .= sprintf('%s%s%s, ', $subArray[$keyToKey], $separator, $subArray[$keyToValue]);
204
        }
205
206
        return substr($string, 0, -2);
207
    }
208
}
209