1 | <?php |
||
19 | class StringUtil |
||
|
|||
20 | { |
||
21 | /** |
||
22 | * Returns the short class name for a fully-qualified class name. |
||
23 | * |
||
24 | * @param string $className The fully-qualified class name. |
||
25 | * |
||
26 | * @return string The short class name. |
||
27 | */ |
||
28 | 56 | public static function getShortClassName($className) |
|
29 | { |
||
30 | 56 | if (false !== ($pos = strrpos($className, '\\'))) { |
|
31 | 51 | return substr($className, $pos + 1); |
|
32 | } |
||
33 | |||
34 | 5 | return $className; |
|
35 | } |
||
36 | |||
37 | 15 | public static function parseValue($string) |
|
38 | { |
||
39 | switch ($string) { |
||
40 | 15 | case '': return null; |
|
41 | 15 | case 'null': return null; |
|
42 | 14 | case 'true': return true; |
|
43 | 12 | case 'false': return false; |
|
44 | } |
||
45 | |||
46 | 11 | if ($string === (string) ($int = (int) $string)) { |
|
47 | 2 | return $int; |
|
48 | } |
||
49 | |||
50 | // Check for " or ' delimiters |
||
51 | // https://3v4l.org/5u0AU |
||
52 | 9 | if (preg_match('/^(["\']).*\1$/m', $string)) { |
|
53 | return substr($string, 1, -1); |
||
54 | } |
||
55 | |||
56 | 9 | return $string; |
|
57 | } |
||
58 | |||
59 | 21 | public static function formatValue($value, $quote = true) |
|
60 | { |
||
61 | 21 | if (null === $value) { |
|
62 | 2 | return 'null'; |
|
63 | } |
||
64 | |||
65 | 20 | if (is_bool($value)) { |
|
66 | 7 | return $value ? 'true' : 'false'; |
|
67 | } |
||
68 | |||
69 | 15 | if (is_string($value)) { |
|
70 | 11 | $q = $quote ? '"' : ''; |
|
71 | |||
72 | 11 | return $q.$value.$q; |
|
73 | } |
||
74 | |||
75 | 4 | return (string) $value; |
|
76 | } |
||
77 | |||
78 | private function __construct() |
||
81 | } |
||
82 |