forked from joachim-n/case-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringAssembler.php
162 lines (137 loc) · 3.3 KB
/
StringAssembler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace CaseConverter;
/**
* Assembles a string from pieces.
*/
class StringAssembler {
/**
* Whether the $pieces are each in title case.
*
* @var bool
*/
protected $areTitleCase = FALSE;
/**
* Creates a new StringAssembler.
*
* @param array $pieces
* The string pieces.
*/
public function __construct($pieces) {
$this->pieces = $pieces;
}
/**
* Sets that the pieces are already in title case.
*
* @return
* $this.
*/
public function areTitleCase() {
$this->areTitleCase = TRUE;
return $this;
}
/**
* Outputs the pieces as a camel case string.
*
* @return string
* The resulting string.
*/
public function camel() {
if ($this->areTitleCase) {
// If the pieces are already in title case, lower the case of the first
// piece only, unless it's in uppercase.
if (!preg_match('@^[[:upper:]]+$@', $this->pieces[0])) {
$this->pieces[0] = lcfirst($this->pieces[0]);
}
}
else {
$first_piece = array_shift($this->pieces);
$this->pieces = array_map('ucfirst', $this->pieces);
array_unshift($this->pieces, $first_piece);
}
return implode('', $this->pieces);
}
/**
* Outputs the pieces as a pascal case string.
*
* @return string
* The resulting string.
*/
public function pascal() {
if (!$this->areTitleCase) {
$this->pieces = array_map('ucfirst', $this->pieces);
}
return implode('', $this->pieces);
}
/**
* Outputs the pieces as a snak case string.
*
* @return string
* The resulting string.
*/
public function snake() {
if ($this->areTitleCase) {
foreach ($this->pieces as &$piece) {
if (preg_match('@^[[:upper:]]+$@', $piece)) {
// Leave an entirely uppercase piece alone.
continue;
}
$piece = lcfirst($piece);
}
}
return implode('_', $this->pieces);
}
/**
* Outputs the pieces as a kebab case string.
*
* @return string
* The resulting string.
*/
public function kebab() {
if ($this->areTitleCase) {
foreach ($this->pieces as &$piece) {
if (preg_match('@^[[:upper:]]+$@', $piece)) {
// Leave an entirely uppercase piece alone.
continue;
}
$piece = lcfirst($piece);
}
}
return implode('-', $this->pieces);
}
/**
* Outputs the pieces as a title case string.
*
* @return string
* The resulting string.
*/
public function title() {
if (!$this->areTitleCase) {
$this->pieces = array_map('ucfirst', $this->pieces);
}
return implode(' ', $this->pieces);
}
/**
* Outputs the pieces as a title case string.
*
* @return string
* The resulting string.
*/
public function sentence() {
$pieces = $this->pieces;
// Take off the first piece.
$first_piece = array_shift($pieces);
if ($this->areTitleCase) {
foreach ($pieces as &$piece) {
if (preg_match('@^[[:upper:]]+$@', $piece)) {
// Leave an entirely uppercase piece alone.
continue;
}
$piece = lcfirst($piece);
}
}
// Make the first piece into title case.
$first_piece = ucfirst($first_piece);
array_unshift($pieces, $first_piece);
return implode(' ', $pieces);
}
}