1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\String\Integrations; |
4
|
|
|
|
5
|
|
|
use Spatie\String\Str; |
6
|
|
|
|
7
|
|
|
class Underscore |
8
|
|
|
{ |
9
|
|
|
protected $underscoreMethods = |
10
|
|
|
[ |
11
|
|
|
//name, firstArgumentIsString, returnsAString |
12
|
|
|
'accord' => [false, true], |
13
|
|
|
'random' => [false, true], |
14
|
|
|
'quickRandom' => [false, true], |
15
|
|
|
'randomStrings' => [false, true], |
16
|
|
|
'endsWith' => [true, false], |
17
|
|
|
'isIp' => [true, false], |
18
|
|
|
'isEmail' => [true, false], |
19
|
|
|
'isUrl' => [true, false], |
20
|
|
|
'startsWith' => [true, false], |
21
|
|
|
'find' => [true, false], |
22
|
|
|
'slice' => [true, false], |
23
|
|
|
'sliceFrom' => [true, true], |
24
|
|
|
'sliceTo' => [true, true], |
25
|
|
|
'baseClass' => [true, true], |
26
|
|
|
'prepend' => [true, true], |
27
|
|
|
'append' => [true, true], |
28
|
|
|
'limit' => [true, true], |
29
|
|
|
'remove' => [true, true], |
30
|
|
|
'replace' => [true, true], |
31
|
|
|
'toggle' => [true, true], |
32
|
|
|
'slugify' => [true, true], |
33
|
|
|
'explode' => [true, false], |
34
|
|
|
'lower' => [true, true], |
35
|
|
|
'plural' => [true, true], |
36
|
|
|
'singular' => [true, true], |
37
|
|
|
'upper' => [true, true], |
38
|
|
|
'title' => [true, true], |
39
|
|
|
'words' => [true, true], |
40
|
|
|
'toPascalCase' => [true, true], |
41
|
|
|
'toSnakeCase' => [true, true], |
42
|
|
|
'toCamelCase' => [true, true], |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param \Spatie\String\Str $string |
47
|
|
|
* @param string $method |
48
|
|
|
* @param array $args |
49
|
|
|
* |
50
|
|
|
* @return mixed|\Spatie\String\Str |
51
|
|
|
*/ |
52
|
|
|
public function call($string, $method, $args) |
53
|
|
|
{ |
54
|
|
|
if ($this->methodUsesStringAsFirstArgument($method)) { |
55
|
|
|
array_unshift($args, (string) $string); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$underscoreResult = call_user_func_array(['Underscore\Types\Strings', $method], $args); |
59
|
|
|
|
60
|
|
|
if ($this->methodReturnsAString($method)) { |
61
|
|
|
return new Str($underscoreResult); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $underscoreResult; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Determine if the given method is supported. |
69
|
|
|
* |
70
|
|
|
* @param $method |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function isSupportedMethod($method) |
75
|
|
|
{ |
76
|
|
|
return array_key_exists($method, $this->underscoreMethods); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Determine if the given method uses the string as it's first argument. |
81
|
|
|
* |
82
|
|
|
* @param $method |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function methodUsesStringAsFirstArgument($method) |
87
|
|
|
{ |
88
|
|
|
return $this->isSupportedMethod($method) ? $this->underscoreMethods[$method][0] : false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Determine if the given method returns a string. |
93
|
|
|
* |
94
|
|
|
* @param $method |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function methodReturnsAString($method) |
99
|
|
|
{ |
100
|
|
|
return $this->isSupportedMethod($method) ? $this->underscoreMethods[$method][1] : false; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|