1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Cecil. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Cecil\Util; |
15
|
|
|
|
16
|
|
|
class Str |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Combines an array into a string. |
20
|
|
|
* |
21
|
|
|
* @param string $keyToKey The key that become the key of the new array |
22
|
|
|
* @param string $keyToValue The key that become the value of the new array |
23
|
|
|
* @param string $separator The separtor between the key and the value in the result string |
24
|
|
|
*/ |
25
|
1 |
|
public static function combineArrayToString( |
26
|
|
|
array $array, |
27
|
|
|
string $keyToKey, |
28
|
|
|
string $keyToValue, |
29
|
|
|
string $separator = ':' |
30
|
|
|
): string { |
31
|
1 |
|
$string = ''; |
32
|
|
|
|
33
|
1 |
|
foreach ($array as $subArray) { |
34
|
1 |
|
$string .= \sprintf('%s%s%s, ', $subArray[$keyToKey], $separator, $subArray[$keyToValue]); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
return substr($string, 0, -2); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Converts 'true', 'false', 'on', 'off', 'yes', 'no' to a boolean. |
42
|
|
|
* |
43
|
|
|
* @param mixed $value Value to convert |
44
|
|
|
* |
45
|
|
|
* @return bool|mixed |
46
|
|
|
*/ |
47
|
1 |
|
public static function strToBool($value) |
48
|
|
|
{ |
49
|
1 |
|
if (is_string($value)) { |
50
|
1 |
|
if (in_array($value, ['true', 'on', 'yes'])) { |
51
|
|
|
return true; |
52
|
|
|
} |
53
|
1 |
|
if (in_array($value, ['false', 'off', 'no'])) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
return $value; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Checks if a string starts with the given string. |
63
|
|
|
*/ |
64
|
1 |
|
public static function startsWith(string $haystack, string $needle): bool |
65
|
|
|
{ |
66
|
1 |
|
$length = strlen($needle); |
67
|
|
|
|
68
|
1 |
|
return substr($haystack, 0, $length) === $needle; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Checks if a string ends with the given string. |
73
|
|
|
*/ |
74
|
|
|
public static function endsWith(string $haystack, string $needle): bool |
75
|
|
|
{ |
76
|
|
|
$length = strlen($needle); |
77
|
|
|
if (!$length) { |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return substr($haystack, -$length) === $needle; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|