1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package WPEmerge |
4
|
|
|
* @author Atanas Angelov <[email protected]> |
5
|
|
|
* @copyright 2018 Atanas Angelov |
6
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
7
|
|
|
* @link https://wpemerge.com/ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace WPEmerge\Helpers; |
11
|
|
|
|
12
|
|
|
class MixedType { |
13
|
|
|
/** |
14
|
|
|
* Converts a value to an array containing this value unless it is an array |
15
|
|
|
* |
16
|
|
|
* @param mixed $argument |
17
|
|
|
* @return array |
18
|
|
|
*/ |
19
|
2 |
|
public static function toArray( $argument ) { |
20
|
2 |
|
if ( ! is_array( $argument ) ) { |
21
|
1 |
|
$argument = [$argument]; |
22
|
1 |
|
} |
23
|
|
|
|
24
|
2 |
|
return $argument; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Executes a value depending on what type it is and returns the result |
29
|
|
|
* Callable: call; return result |
30
|
|
|
* Instance: call method; return result |
31
|
|
|
* Class: instantiate; call method; return result |
32
|
|
|
* Other: return value without taking any action |
33
|
|
|
* |
34
|
|
|
* @param mixed $entity |
35
|
|
|
* @param array $arguments |
36
|
|
|
* @param string $method |
37
|
|
|
* @param callable $instantiator |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
5 |
|
public static function value( $entity, $arguments = [], $method = '__invoke', $instantiator = 'static::instantiate' ) { |
41
|
5 |
|
if ( is_callable( $entity ) ) { |
42
|
2 |
|
return call_user_func_array( $entity, $arguments ); |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
if ( is_object( $entity ) ) { |
46
|
1 |
|
return call_user_func_array( [$entity, $method], $arguments ); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
if ( static::isClass( $entity ) ) { |
50
|
1 |
|
return call_user_func_array( [call_user_func( $instantiator, $entity ), $method], $arguments ); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
return $entity; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Check if a value is a valid class name |
58
|
|
|
* |
59
|
|
|
* @param mixed $class_name |
60
|
|
|
* @return boolean |
61
|
|
|
*/ |
62
|
|
|
public static function isClass( $class_name ) { |
63
|
1 |
|
return ( is_string( $class_name ) && class_exists( $class_name ) ); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create a new instance of the given class. |
68
|
|
|
* |
69
|
|
|
* @param string $class_name |
70
|
|
|
* @return object |
71
|
|
|
*/ |
72
|
|
|
public static function instantiate( $class_name ) { |
73
|
|
|
return new $class_name(); |
74
|
|
|
} |
75
|
1 |
|
|
76
|
1 |
|
/** |
77
|
|
|
* Normalize a path's slashes according to the current OS. |
78
|
|
|
* Solves mixed slashes that are sometimes returned by WordPress core functions. |
79
|
|
|
* |
80
|
|
|
* @param string $path |
81
|
|
|
* @param string $slash |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public static function normalizePath( $path, $slash = DIRECTORY_SEPARATOR ) { |
85
|
|
|
return preg_replace( '~[' . preg_quote( '/\\', '~' ) . ']+~', $slash, $path ); |
86
|
1 |
|
} |
87
|
1 |
|
|
88
|
1 |
|
/** |
89
|
1 |
|
* Ensure path has a trailing slash. |
90
|
|
|
* |
91
|
|
|
* @param string $path |
92
|
|
|
* @param string $slash |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public static function addTrailingSlash( $path, $slash = DIRECTORY_SEPARATOR ) { |
96
|
|
|
$path = static::normalizePath( $path, $slash ); |
97
|
|
|
$path = preg_replace( '~' . preg_quote( $slash, '~' ) . '*$~', $slash, $path ); |
98
|
|
|
return $path; |
99
|
1 |
|
} |
100
|
1 |
|
|
101
|
1 |
|
/** |
102
|
1 |
|
* Ensure path does not have a trailing slash. |
103
|
|
|
* |
104
|
|
|
* @param string $path |
105
|
|
|
* @param string $slash |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public static function removeTrailingSlash( $path, $slash = DIRECTORY_SEPARATOR ) { |
109
|
|
|
$path = static::normalizePath( $path, $slash ); |
110
|
|
|
$path = preg_replace( '~' . preg_quote( $slash, '~' ) . '+$~', '', $path ); |
111
|
|
|
return $path; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|