|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebComplete\core\utils\helpers; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class InflectorHelper |
|
7
|
|
|
* based on Yii BaseInflector |
|
8
|
|
|
*/ |
|
9
|
|
|
class InflectorHelper |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Converts an underscored or CamelCase word into a English |
|
14
|
|
|
* sentence. |
|
15
|
|
|
* @param string $words |
|
16
|
|
|
* @param bool $ucAll whether to set all words to uppercase |
|
17
|
|
|
* @return string |
|
18
|
|
|
*/ |
|
19
|
|
|
public function titleize($words, $ucAll = false): string |
|
20
|
|
|
{ |
|
21
|
|
|
$words = $this->humanize($this->underscore($words), $ucAll); |
|
22
|
|
|
|
|
23
|
|
|
return $ucAll ? \ucwords($words) : \ucfirst($words); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Returns given word as CamelCased |
|
28
|
|
|
* Converts a word like "send_email" to "SendEmail". It |
|
29
|
|
|
* will remove non alphanumeric character from the word, so |
|
30
|
|
|
* "who's online" will be converted to "WhoSOnline" |
|
31
|
|
|
* @see variablize() |
|
32
|
|
|
* @param string $word the word to CamelCase |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public function camelize($word): string |
|
36
|
|
|
{ |
|
37
|
|
|
return \str_replace(' ', '', \ucwords( |
|
38
|
|
|
\preg_replace('/[^A-Za-z0-9]+/', ' ', $word) |
|
39
|
|
|
)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Converts a CamelCase name into space-separated words. |
|
44
|
|
|
* For example, 'PostTag' will be converted to 'Post Tag'. |
|
45
|
|
|
* @param string $name the string to be converted |
|
46
|
|
|
* @param bool $ucwords whether to capitalize the first letter in each word |
|
47
|
|
|
* @return string the resulting words |
|
48
|
|
|
*/ |
|
49
|
|
|
public function camel2words($name, $ucwords = true): string |
|
50
|
|
|
{ |
|
51
|
|
|
$label = \strtolower(\trim(\str_replace([ |
|
52
|
|
|
'-', |
|
53
|
|
|
'_', |
|
54
|
|
|
'.', |
|
55
|
|
|
], ' ', \preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $name)))); |
|
56
|
|
|
|
|
57
|
|
|
return $ucwords ? \ucwords($label) : $label; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Converts a CamelCase name into an ID in lowercase. |
|
62
|
|
|
* Words in the ID may be concatenated using the specified character (defaults to '-'). |
|
63
|
|
|
* For example, 'PostTag' will be converted to 'post-tag'. |
|
64
|
|
|
* @param string $name the string to be converted |
|
65
|
|
|
* @param string $separator the character used to concatenate the words in the ID |
|
66
|
|
|
* @param bool|string $strict whether to insert a separator between two consecutive uppercase chars |
|
67
|
|
|
* @return string the resulting ID |
|
68
|
|
|
*/ |
|
69
|
|
|
public function camel2id($name, $separator = '-', $strict = false): string |
|
70
|
|
|
{ |
|
71
|
|
|
$regex = $strict ? '/[A-Z]/' : '/(?<![A-Z])[A-Z]/'; |
|
72
|
|
|
if ($separator === '_') { |
|
73
|
|
|
return \strtolower(\trim(\preg_replace($regex, '_\0', $name), '_')); |
|
74
|
|
|
} |
|
75
|
|
|
return \strtolower(\trim( |
|
76
|
|
|
\str_replace('_', $separator, \preg_replace($regex, $separator . '\0', $name)), |
|
77
|
|
|
$separator |
|
78
|
|
|
)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Converts an ID into a CamelCase name. |
|
83
|
|
|
* Words in the ID separated by `$separator` (defaults to '-') will be concatenated into a CamelCase name. |
|
84
|
|
|
* For example, 'post-tag' is converted to 'PostTag'. |
|
85
|
|
|
* @param string $id the ID to be converted |
|
86
|
|
|
* @param string $separator the character used to separate the words in the ID |
|
87
|
|
|
* @return string the resulting CamelCase name |
|
88
|
|
|
*/ |
|
89
|
|
|
public function id2camel($id, $separator = '-'): string |
|
90
|
|
|
{ |
|
91
|
|
|
return \str_replace(' ', '', \ucwords(\implode(' ', \explode($separator, $id)))); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Converts any "CamelCased" into an "underscored_word". |
|
96
|
|
|
* @param string $words the word(s) to underscore |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function underscore($words): string |
|
100
|
|
|
{ |
|
101
|
|
|
return \strtolower(\preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $words)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns a human-readable string from $word |
|
106
|
|
|
* @param string $word the string to humanize |
|
107
|
|
|
* @param bool $ucAll whether to set all words to uppercase or not |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function humanize($word, $ucAll = false): string |
|
111
|
|
|
{ |
|
112
|
|
|
$word = \str_replace('_', ' ', \preg_replace('/_id$/', '', $word)); |
|
113
|
|
|
|
|
114
|
|
|
return $ucAll ? \ucwords($word) : \ucfirst($word); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Same as camelize but first char is in lowercase. |
|
119
|
|
|
* Converts a word like "send_email" to "sendEmail". It |
|
120
|
|
|
* will remove non alphanumeric character from the word, so |
|
121
|
|
|
* "who's online" will be converted to "whoSOnline" |
|
122
|
|
|
* @param string $word to lowerCamelCase |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function variablize($word): string |
|
126
|
|
|
{ |
|
127
|
|
|
$word = $this->camelize($word); |
|
128
|
|
|
|
|
129
|
|
|
return \strtolower($word[0]) . \substr($word, 1); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|