|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Automation tool mixed with code generator for easier continuous development |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/hidev |
|
6
|
|
|
* @package hidev |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hidev\helpers; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\php\collection\ArrayHelper; |
|
14
|
|
|
use Yii; |
|
15
|
|
|
use yii\helpers\Inflector; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Hidev Helper. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Helper |
|
21
|
|
|
{ |
|
22
|
3 |
|
public static function isYii20() |
|
23
|
|
|
{ |
|
24
|
3 |
|
return method_exists(Yii::class, 'autoload'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
4 |
|
public static function bad2sep($str, $separator = '-') |
|
28
|
|
|
{ |
|
29
|
4 |
|
return preg_replace('/[^a-zA-Z0-9-]/', $separator, $str); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
4 |
|
public static function id2camel($id, $separator = '-') |
|
33
|
|
|
{ |
|
34
|
4 |
|
return Inflector::id2camel(self::bad2sep($id, $separator), $separator); |
|
35
|
|
|
//return Inflector::id2camel(strtolower(self::bad2sep($id,$separator)), $separator); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public static function camel2id($name, $separator = '-', $strict = false) |
|
39
|
|
|
{ |
|
40
|
1 |
|
return str_replace('--', '-', Inflector::camel2id(str_replace(' ', '', ucwords($name)), $separator, $strict)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function file2template($file, $separator = '-') |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
return trim($file, '.'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function csplit($input, $delimiter = ',') |
|
49
|
|
|
{ |
|
50
|
|
|
if (is_array($input)) { |
|
51
|
|
|
return $input; |
|
52
|
|
|
} |
|
53
|
|
|
$res = explode($delimiter, $input); |
|
54
|
|
|
|
|
55
|
|
|
return array_values(array_filter(array_map('trim', $res))); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function asplit($input) |
|
59
|
|
|
{ |
|
60
|
|
|
if (is_array($input)) { |
|
61
|
|
|
return $input; |
|
62
|
|
|
} |
|
63
|
|
|
$res = preg_split('/[\s,]+/', $input); |
|
64
|
|
|
|
|
65
|
|
|
return array_values(array_filter(array_map('trim', $res))); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public static function ksplit($input, $delimiter = ',') |
|
69
|
|
|
{ |
|
70
|
|
|
if (is_array($input)) { |
|
71
|
|
|
return $input; |
|
72
|
|
|
} |
|
73
|
|
|
$res = self::csplit($input, $delimiter); |
|
74
|
|
|
|
|
75
|
|
|
return array_combine($res, $res); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public static function getPublicVars($subj) |
|
79
|
|
|
{ |
|
80
|
|
|
return is_object($subj) ? get_object_vars($subj) : get_class_vars($subj); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
public static function titleize($str, $ucAll = true) |
|
84
|
|
|
{ |
|
85
|
2 |
|
return Inflector::titleize(strtr($str, '-', ' '), $ucAll); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Recursively removes duplicate values from non-associative arrays. |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function uniqueConfig($array) |
|
92
|
|
|
{ |
|
93
|
|
|
return ArrayHelper::unique($array); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public static function readline($prompt) |
|
97
|
|
|
{ |
|
98
|
|
|
return readline($prompt); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public static function readpassword($prompt) |
|
102
|
|
|
{ |
|
103
|
|
|
echo $prompt; |
|
104
|
|
|
system('stty -echo'); |
|
105
|
|
|
$password = rtrim(fgets(STDIN), PHP_EOL); |
|
106
|
|
|
system('stty echo'); |
|
107
|
|
|
echo "\n"; |
|
108
|
|
|
|
|
109
|
|
|
return $password; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Is response Ok. |
|
114
|
|
|
* @param Response|int $response |
|
|
|
|
|
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function isResponseOk($response) |
|
118
|
|
|
{ |
|
119
|
|
|
return !(is_object($response) ? $response->exitStatus : $response); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.