|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the webmozart/console package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Webmozart\Console\Util; |
|
13
|
|
|
|
|
14
|
|
|
use Webmozart\Console\Api\Args\Format\InvalidValueException; |
|
15
|
|
|
use Webmozart\Console\Api\Formatter\Formatter; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @since 1.0 |
|
19
|
|
|
* |
|
20
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class StringUtil |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
277 |
|
public static function parseString($value, $nullable = true) |
|
25
|
|
|
{ |
|
26
|
277 |
|
if ($nullable && (null === $value || 'null' === $value)) { |
|
27
|
10 |
|
return null; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
267 |
|
if (null === $value) { |
|
31
|
2 |
|
return 'null'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
265 |
|
if (true === $value) { |
|
35
|
1 |
|
return 'true'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
264 |
|
if (false === $value) { |
|
39
|
1 |
|
return 'false'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
263 |
|
return (string) $value; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
28 |
|
public static function parseBoolean($value, $nullable = true) |
|
46
|
|
|
{ |
|
47
|
28 |
|
if ($nullable && (null === $value || 'null' === $value)) { |
|
48
|
4 |
|
return null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
24 |
|
if (is_bool($value)) { |
|
52
|
2 |
|
return $value; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
22 |
|
if (is_string($value) || is_int($value)) { |
|
56
|
20 |
|
switch ((string) $value) { |
|
57
|
20 |
|
case '': |
|
58
|
19 |
|
case 'false': |
|
59
|
16 |
|
case '0': |
|
60
|
14 |
|
case 'no': |
|
61
|
13 |
|
case 'off': |
|
62
|
8 |
|
return false; |
|
63
|
|
|
|
|
64
|
12 |
|
case 'true': |
|
65
|
9 |
|
case '1': |
|
66
|
7 |
|
case 'yes': |
|
67
|
6 |
|
case 'on': |
|
68
|
7 |
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
7 |
|
throw new InvalidValueException(sprintf( |
|
73
|
7 |
|
'The value "%s" cannot be parsed as boolean.', |
|
74
|
|
|
$value |
|
75
|
|
|
)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
36 |
View Code Duplication |
public static function parseInteger($value, $nullable = true) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
36 |
|
if ($nullable && (null === $value || 'null' === $value)) { |
|
81
|
4 |
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
32 |
|
if (is_numeric($value) || is_bool($value)) { |
|
85
|
20 |
|
return (int) $value; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
12 |
|
throw new InvalidValueException(sprintf( |
|
89
|
12 |
|
'The value "%s" cannot be parsed as integer.', |
|
90
|
|
|
$value |
|
91
|
|
|
)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
30 |
View Code Duplication |
public static function parseFloat($value, $nullable = true) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
30 |
|
if ($nullable && (null === $value || 'null' === $value)) { |
|
97
|
4 |
|
return null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
26 |
|
if (is_numeric($value) || is_bool($value)) { |
|
101
|
14 |
|
return (float) $value; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
12 |
|
throw new InvalidValueException(sprintf( |
|
105
|
12 |
|
'The value "%s" cannot be parsed as float.', |
|
106
|
|
|
$value |
|
107
|
|
|
)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
21 |
|
public static function getLength($string, Formatter $formatter = null) |
|
111
|
|
|
{ |
|
112
|
21 |
|
if ($formatter) { |
|
113
|
21 |
|
$string = $formatter->removeFormat($string); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
21 |
|
if (!function_exists('mb_strwidth')) { |
|
117
|
|
|
return strlen($string); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
21 |
|
if (false === $encoding = mb_detect_encoding($string)) { |
|
121
|
|
|
return strlen($string); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
21 |
|
return mb_strwidth($string, $encoding); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
14 |
View Code Duplication |
public static function getMaxWordLength($string, Formatter $formatter = null) |
|
|
|
|
|
|
128
|
|
|
{ |
|
129
|
14 |
|
if ($formatter) { |
|
130
|
14 |
|
$string = $formatter->removeFormat($string); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
14 |
|
$maxLength = 0; |
|
134
|
14 |
|
$words = preg_split('/\s+/', $string); |
|
135
|
|
|
|
|
136
|
14 |
|
foreach ($words as $word) { |
|
137
|
|
|
// No need to pass the formatter because the tags are already |
|
138
|
|
|
// removed |
|
139
|
14 |
|
$maxLength = max($maxLength, self::getLength($word)); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
14 |
|
return $maxLength; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
14 |
View Code Duplication |
public static function getMaxLineLength($string, Formatter $formatter = null) |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
14 |
|
if ($formatter) { |
|
148
|
14 |
|
$string = $formatter->removeFormat($string); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
14 |
|
$maxLength = 0; |
|
152
|
14 |
|
$lines = explode("\n", $string); |
|
153
|
|
|
|
|
154
|
14 |
|
foreach ($lines as $word) { |
|
155
|
|
|
// No need to pass the formatter because the tags are already |
|
156
|
|
|
// removed |
|
157
|
14 |
|
$maxLength = max($maxLength, self::getLength($word)); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
14 |
|
return $maxLength; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
private function __construct() |
|
164
|
|
|
{ |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|