|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Joesama\Pintu\Routings\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
|
|
8
|
|
|
trait Grammar |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Path naming convention definition. |
|
12
|
|
|
* |
|
13
|
|
|
* @param string $function |
|
14
|
|
|
* @param array $attributes |
|
15
|
|
|
* |
|
16
|
|
|
* @return string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function pathConvention(string $type, string $function, array $attributes): string |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
$keymap = $this->keymapIsString(Arr::get($attributes, 'keymap')); |
|
21
|
|
|
|
|
22
|
|
|
$keymap = $this->appendIdParameter($keymap); |
|
23
|
|
|
|
|
24
|
|
|
if (!\is_array($keymap) || empty($keymap)) { |
|
|
|
|
|
|
25
|
|
|
return $function; |
|
26
|
|
|
} else { |
|
27
|
|
|
$keymap = collect($keymap)->map(function ($id) { |
|
28
|
|
|
return '{' . $id . '}'; |
|
29
|
|
|
})->implode('/'); |
|
30
|
|
|
|
|
31
|
|
|
return $function . '/' . $keymap; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Check if keymap is string. |
|
37
|
|
|
* |
|
38
|
|
|
* @param $keymap |
|
39
|
|
|
* |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
|
|
private function keymapIsString($keymap): array |
|
43
|
|
|
{ |
|
44
|
|
|
if (\is_string($keymap) && Str::length($keymap) > 0) { |
|
45
|
|
|
return [$keymap]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return []; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Append ID parameter. |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $keymap |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
private function appendIdParameter(array $keymap): array |
|
59
|
|
|
{ |
|
60
|
|
|
// @todo Need to analize the need on default id parameter. |
|
61
|
|
|
// if (Str::lower($type) === 'head') { |
|
62
|
|
|
// if (!in_array('id', $keymap)) { |
|
63
|
|
|
// $keymap = Arr::prepend($keymap, 'id'); |
|
64
|
|
|
// } |
|
65
|
|
|
// } |
|
66
|
|
|
|
|
67
|
|
|
return $keymap; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Class naming convention definition. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $type |
|
74
|
|
|
* @param string $controller |
|
75
|
|
|
* @param string $function |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function classConvention(string $type, string $controller, string $function): string |
|
80
|
|
|
{ |
|
81
|
|
|
return Str::ucfirst($controller) . 'Controller@' . Str::camel(Str::lower($type) . '_' . $function); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Router name naming convention. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $controller |
|
88
|
|
|
* @param string $function |
|
89
|
|
|
* @param array $attributes |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function namedConvention(string $controller, string $function, array $attributes): string |
|
94
|
|
|
{ |
|
95
|
|
|
$named = Arr::get($attributes, 'named', null); |
|
96
|
|
|
|
|
97
|
|
|
if ($named === '' || $named === null) { |
|
98
|
|
|
$named = $controller . '.' . $function; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return Str::lower($named); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.