-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComposition.php
More file actions
154 lines (130 loc) · 5.2 KB
/
Copy pathComposition.php
File metadata and controls
154 lines (130 loc) · 5.2 KB
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
<?php
declare(strict_types=1);
namespace Zero\Lib\Support\Concerns\Str;
trait Composition
{
public static function start(string $value, string $prefix): string
{
$quoted = preg_quote($prefix, '/');
return $prefix . preg_replace('/^(?:' . $quoted . ')+/u', '', $value);
}
public static function finish(string $value, string $cap): string
{
$quoted = preg_quote($cap, '/');
return preg_replace('/(?:' . $quoted . ')+$/u', '', $value) . $cap;
}
public static function ensureSuffix(string $value, string $suffix): string
{
return str_ends_with($value, $suffix) ? $value : $value . $suffix;
}
public static function wrap(string $value, string $before, ?string $after = null): string
{
return $before . $value . ($after ?? $before);
}
public static function unwrap(string $value, string $before, ?string $after = null): string
{
$after ??= $before;
if (str_starts_with($value, $before)) {
$value = substr($value, strlen($before));
}
if (str_ends_with($value, $after)) {
$value = substr($value, 0, -strlen($after));
}
return $value;
}
public static function reverse(string $value): string
{
return implode('', array_reverse(mb_str_split($value)));
}
public static function squish(string $value): string
{
return trim((string) preg_replace('/\s+|\x{3164}|\x{1160}/u', ' ', $value));
}
public static function deduplicate(string $value, string $character = ' '): string
{
$quoted = preg_quote($character, '/');
return (string) preg_replace('/(' . $quoted . ')+/u', $character, $value);
}
public static function chopStart(string $subject, string|array $needle): string
{
foreach ((array) $needle as $value) {
if ($value !== '' && str_starts_with($subject, $value)) {
return substr($subject, strlen($value));
}
}
return $subject;
}
public static function chopEnd(string $subject, string|array $needle): string
{
foreach ((array) $needle as $value) {
if ($value !== '' && str_ends_with($subject, $value)) {
return substr($subject, 0, -strlen($value));
}
}
return $subject;
}
public static function trim(string $value, ?string $charlist = null): string
{
return $charlist === null ? trim($value) : trim($value, $charlist);
}
public static function ltrim(string $value, ?string $charlist = null): string
{
return $charlist === null ? ltrim($value) : ltrim($value, $charlist);
}
public static function rtrim(string $value, ?string $charlist = null): string
{
return $charlist === null ? rtrim($value) : rtrim($value, $charlist);
}
public static function headline(string $value): string
{
$parts = explode(' ', $value);
$parts = count($parts) > 1
? array_map(static fn($part) => self::title($part), $parts)
: array_map(static fn($part) => self::title($part), self::ucsplit(implode('_', $parts)));
$collapsed = self::replace(['-', '_', ' '], '_', implode('_', $parts));
return implode(' ', array_filter(explode('_', $collapsed)));
}
public static function apa(string $value): string
{
if (trim($value) === '') {
return $value;
}
$minor = ['and', 'as', 'but', 'for', 'if', 'nor', 'or', 'so', 'yet', 'a', 'an', 'the', 'at', 'by', 'for', 'in', 'of', 'off', 'on', 'per', 'to', 'up', 'via'];
$words = preg_split('/\s+/', $value, -1, PREG_SPLIT_NO_EMPTY);
$result = [];
$count = count($words);
foreach ($words as $i => $word) {
$lower = mb_strtolower($word);
if ($i !== 0 && $i !== $count - 1 && in_array($lower, $minor, true)) {
$result[] = $lower;
} else {
$result[] = mb_strtoupper(mb_substr($word, 0, 1)) . mb_substr($word, 1);
}
}
return implode(' ', $result);
}
public static function initials(string $value, string $glue = ''): string
{
$words = preg_split('/\s+/', trim($value), -1, PREG_SPLIT_NO_EMPTY);
return implode($glue, array_map(static fn($word) => mb_strtoupper(mb_substr($word, 0, 1)), $words ?: []));
}
public static function mask(string $value, string $character, int $index, ?int $length = null, string $encoding = 'UTF-8'): string
{
if ($character === '') {
return $value;
}
$valueLength = mb_strlen($value, $encoding);
$start = $index;
if ($start < 0) {
$start = max(0, $valueLength + $start);
}
$segmentLength = $length ?? $valueLength;
if ($segmentLength < 0) {
$segmentLength = max(0, $valueLength - $start + $segmentLength);
}
$segmentLength = min($segmentLength, $valueLength - $start);
$start_str = mb_substr($value, 0, $start, $encoding);
$end_str = mb_substr($value, $start + $segmentLength, null, $encoding);
return $start_str . str_repeat(mb_substr($character, 0, 1, $encoding), $segmentLength) . $end_str;
}
}