|
1
|
|
|
<?php |
|
2
|
|
|
namespace Subreality\Dilmun\Anshar\Utils; |
|
3
|
|
|
|
|
4
|
|
|
class StringHelper |
|
5
|
|
|
{ |
|
6
|
|
|
protected $string; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* StringHelper constructor. Accepts and stores a string. |
|
10
|
|
|
* |
|
11
|
|
|
* @throws \InvalidArgumentException if not initialized with a string |
|
12
|
|
|
* |
|
13
|
|
|
* @param string $string The string needing help |
|
14
|
|
|
*/ |
|
15
|
55 |
|
public function __construct($string) |
|
16
|
|
|
{ |
|
17
|
55 |
|
if (!is_string($string)) { |
|
18
|
6 |
|
throw new \InvalidArgumentException("Supplied string is not a string"); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
49 |
|
$this->string = $string; |
|
22
|
49 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Given a delimited string, returns a copy of the string with chunks defined by the delimiter affected by a given |
|
26
|
|
|
* function. |
|
27
|
|
|
* |
|
28
|
|
|
* The supplied function must be callable and must accept a string as its only required parameter |
|
29
|
|
|
* |
|
30
|
|
|
* @param callable $function The function to be called against the delimited chunks; must be callable and must |
|
31
|
|
|
* accept a string as its only parameter |
|
32
|
|
|
* @param string[] $delimiters One or more strings delimiting the chunks |
|
|
|
|
|
|
33
|
|
|
* |
|
34
|
|
|
* @return string A delimited string with chunks affected by the supplied function |
|
35
|
|
|
*/ |
|
36
|
14 |
|
public function affectChunks(callable $function, ...$delimiters) |
|
37
|
|
|
{ |
|
38
|
14 |
|
$delimiter_string = ""; |
|
39
|
14 |
|
$replace_callback = function ($matches) use ($function) { |
|
40
|
|
|
|
|
41
|
12 |
|
$callback_string = $function($matches[0]); |
|
42
|
|
|
|
|
43
|
12 |
|
return $callback_string; |
|
44
|
14 |
|
}; |
|
45
|
|
|
|
|
46
|
14 |
|
foreach ($delimiters as $delimiter) { |
|
47
|
14 |
|
$delimiter_string .= preg_quote($delimiter, "/"); |
|
48
|
14 |
|
} |
|
49
|
|
|
|
|
50
|
14 |
|
$pattern = "/[^{$delimiter_string}]+/"; |
|
51
|
|
|
|
|
52
|
14 |
|
$affected_string = preg_replace_callback($pattern, $replace_callback, $this->string); |
|
53
|
|
|
|
|
54
|
14 |
|
return $affected_string; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Determines whether the string starts with a given string. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $string A string the string may or may not start with |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool Returns true if the string starts with the given string |
|
63
|
|
|
* Returns false if the string does not start with the given string |
|
64
|
|
|
*/ |
|
65
|
24 |
|
public function startsWith($string) |
|
66
|
|
|
{ |
|
67
|
24 |
|
$pattern = "/^" . preg_quote($string, "/") . "/"; |
|
68
|
|
|
|
|
69
|
24 |
|
$starts_with_string = preg_match($pattern, $this->string); |
|
70
|
|
|
|
|
71
|
24 |
|
return (bool) $starts_with_string; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Given a string, collapses any repetition at the start of the string into a single instance of the given string. |
|
76
|
|
|
* |
|
77
|
|
|
* For example, if the StringHelper was constructed with "ffoo", StringHelper::collapseStartingRepetition("f") will |
|
78
|
|
|
* return "foo". |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $string A string the string may or may not start with, with or without repetition |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool|string Returns a potentially modified string with a repeating, starting string collapsed into |
|
|
|
|
|
|
83
|
|
|
* a single instance of the string |
|
84
|
|
|
* Returns false if the string does not start with any quantity of the given string |
|
85
|
|
|
*/ |
|
86
|
10 |
|
public function collapseStartingRepetition($string) |
|
87
|
|
|
{ |
|
88
|
10 |
|
$matches = array(); |
|
89
|
10 |
|
$pattern = "/^[" . preg_quote($string, "/") . "]+(?P<remainder>.*)/"; |
|
90
|
|
|
|
|
91
|
10 |
|
$starts_with_string = preg_match($pattern, $this->string, $matches); |
|
92
|
|
|
|
|
93
|
10 |
|
if ($starts_with_string) { |
|
94
|
7 |
|
$collapsed_string = $string . $matches["remainder"]; |
|
95
|
|
|
|
|
96
|
7 |
|
return $collapsed_string; |
|
97
|
|
|
} else { |
|
98
|
4 |
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.