1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Alejandro Peña Florentín ([email protected]). |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Tight; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Utils class |
31
|
|
|
* |
32
|
|
|
* @author Alejandro Peña Florentín ([email protected]) |
33
|
|
|
*/ |
34
|
|
|
class Utils |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Removes the first ocurrence of a substring in a string |
39
|
|
|
* @param string $string String |
40
|
|
|
* @param string $substrToRemove Substring to remove |
41
|
|
|
* @return string String without $substrToRemove or $string if $substrToRemove hasn't been found |
42
|
|
|
*/ |
43
|
|
|
public static function removeSubstring($string, $substrToRemove) { |
44
|
|
|
// Check index position on $string |
45
|
|
|
$firstIndex = strpos($string, $substrToRemove); |
46
|
|
|
// If $firstIndex is false $substrToRemove wasn't found in $string |
47
|
|
|
if ($firstIndex === FALSE) { |
48
|
|
|
return $string; |
49
|
|
|
} |
50
|
|
|
// Check last index |
51
|
|
|
$lastIndex = $firstIndex + strlen($substrToRemove); |
52
|
|
|
// Get the substring before $substrToRemove |
53
|
|
|
$pre = substr($string, 0, $firstIndex); |
54
|
|
|
// Get the substring after $substrToRemove |
55
|
|
|
$post = substr($string, $lastIndex); |
56
|
|
|
// Return pre and post |
57
|
|
|
return $pre . $post; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check if a substring is in a string |
62
|
|
|
* @param string $string String |
63
|
|
|
* @param string $strToSearch Substring to search |
64
|
|
|
* @return boolean TRUE on success,FALSE otherwise |
65
|
|
|
*/ |
66
|
|
|
public static function inString($string, $strToSearch) { |
67
|
|
|
return (strpos($string, $strToSearch) !== FALSE); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Adds a trailing slash to a given path. |
72
|
|
|
* |
73
|
|
|
* This method does not add the trailing slash if is already set. |
74
|
|
|
* |
75
|
|
|
* @param string $path Path |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public static function addTrailingSlash($path) { |
79
|
|
|
if (null !== $path && is_string($path) && !empty($path)) { |
80
|
|
|
if (substr($path, -1) !== "/") { |
81
|
|
|
$path .="/"; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return $path; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Replace all the backslash for slashes |
89
|
|
|
* |
90
|
|
|
* @param string $path Path |
91
|
|
|
* @return string Converted path |
92
|
|
|
*/ |
93
|
|
|
public static function filterPath($path) { |
94
|
|
|
return self::addTrailingSlash(str_replace(DIRECTORY_SEPARATOR, "/", str_replace("\\", "/", $path))); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Replace repeated substring. |
99
|
|
|
* |
100
|
|
|
* @param string $string String |
101
|
|
|
* @param string $substring Substring |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public static function removeDouble($string, $substring) { |
105
|
|
|
return str_replace($substring . $substring, $substring, $string); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Slices a file into name and extension |
110
|
|
|
* |
111
|
|
|
* @param string $filePath File path |
112
|
|
|
* @return array Associative array with 2 keys: name and extension |
113
|
|
|
*/ |
114
|
|
|
public static function getSlicedFile($filePath) { |
115
|
|
|
$output = []; |
116
|
|
|
$filePath = str_replace("\\", "/", $filePath); |
117
|
|
|
if (is_file($filePath)) { |
118
|
|
|
// Removes the path |
119
|
|
|
$explodePath = explode("/", $filePath); |
120
|
|
|
$file = $explodePath[count($explodePath) - 1]; |
121
|
|
|
$explode = explode(".", $file); |
122
|
|
|
$extension = array_pop($explode); |
123
|
|
|
$output["name"] = implode(".", $explode); |
124
|
|
|
$output["ext"] = $extension; |
125
|
|
|
} |
126
|
|
|
return $output; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public static function startsWith($string, $substring) { |
130
|
|
|
return (strpos($string, $substring) === 0); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|