|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Helper class that provides useful php functions. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Vettivel Satheez <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://github.com/satheez |
|
8
|
|
|
* |
|
9
|
|
|
* @license MIT |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sa\Helper; |
|
13
|
|
|
|
|
14
|
|
|
class Str |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Convert the given string to title case.. |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $value |
|
20
|
|
|
* |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function title(string $value): string |
|
24
|
|
|
{ |
|
25
|
|
|
return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Convert the given string to upper-case.. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $value |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function upper(string $value) |
|
36
|
|
|
{ |
|
37
|
|
|
return mb_strtoupper($value, 'UTF-8'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Convert the given string to upper-case.. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $value |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function lower(string $value) |
|
48
|
|
|
{ |
|
49
|
|
|
return mb_strtolower($value, 'UTF-8'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns the portion of string specified by the start and length parameters.. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $string |
|
56
|
|
|
* @param int $start |
|
57
|
|
|
* @param int|null $length |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function substr(string $string, int $start, ?int $length = null): string |
|
62
|
|
|
{ |
|
63
|
|
|
return mb_substr($string, $start, $length, 'UTF-8'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $str . |
|
68
|
|
|
* @param int $limit |
|
69
|
|
|
* @param string $end |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function limit(string $str, int $limit = 30, string $end = '...'): string |
|
74
|
|
|
{ |
|
75
|
|
|
if (strlen($str) <= $limit) { |
|
76
|
|
|
return $str; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return self::substr($str, 0, $limit).$end; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Upper case for each word start letters. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $str |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function camel(string $str): string |
|
90
|
|
|
{ |
|
91
|
|
|
return ucwords(self::lower($str)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Generate random string. |
|
96
|
|
|
* |
|
97
|
|
|
* @param int $limit |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function random(int $limit = 10): string |
|
102
|
|
|
{ |
|
103
|
|
|
return Generate::randomString($limit); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|