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 $type |
14
|
|
|
* @param string $function |
15
|
|
|
* @param array $attributes |
16
|
|
|
* |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
1 |
|
public function pathConvention(string $type, string $function, array $attributes): string |
20
|
|
|
{ |
21
|
1 |
|
$keymap = $this->keymapIsString(Arr::get($attributes, 'keymap')); |
22
|
|
|
|
23
|
1 |
|
if (!\is_array($keymap) || empty($keymap)) { |
|
|
|
|
24
|
|
|
return $function; |
25
|
|
|
} else { |
26
|
|
|
$keymap = collect($keymap)->map(function ($id, $key) use ($type) { |
27
|
1 |
|
if (!is_int($key) && strtolower($key) === strtolower($type)) { |
28
|
|
|
return '{' . $id . '}'; |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
return null; |
32
|
1 |
|
})->implode('/'); |
33
|
|
|
|
34
|
1 |
|
return $function . '/' . $keymap; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Check if keymap is string. |
40
|
|
|
* |
41
|
|
|
* @param $keymap |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
1 |
|
private function keymapIsString($keymap): array |
46
|
|
|
{ |
47
|
1 |
|
if (\is_string($keymap)) { |
48
|
1 |
|
return (Str::length($keymap) > 0) ? Arr::wrap($keymap) : []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $keymap; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Append ID parameter. |
56
|
|
|
* |
57
|
|
|
* @param array $keymap |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
private function appendIdParameter(array $keymap): array |
62
|
|
|
{ |
63
|
|
|
// @todo Need to analize the need on default id parameter. |
64
|
|
|
// if (Str::lower($type) === 'head') { |
65
|
|
|
// if (!in_array('id', $keymap)) { |
66
|
|
|
// $keymap = Arr::prepend($keymap, 'id'); |
67
|
|
|
// } |
68
|
|
|
// } |
69
|
|
|
|
70
|
|
|
return $keymap; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Class naming convention definition. |
75
|
|
|
* |
76
|
|
|
* @param string $type |
77
|
|
|
* @param string $controller |
78
|
|
|
* @param string $function |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
2 |
|
public function classConvention(string $type, string $controller, string $function): string |
83
|
|
|
{ |
84
|
2 |
|
return Str::ucfirst($controller) . 'Controller@' . Str::camel(Str::lower($type) . '_' . $function); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Router name naming convention. |
89
|
|
|
* |
90
|
|
|
* @param string $type |
91
|
|
|
* @param string $controller |
92
|
|
|
* @param string $function |
93
|
|
|
* @param array $attributes |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
1 |
|
public function namedConvention(string $type, string $controller, string $function, array $attributes): string |
98
|
|
|
{ |
99
|
1 |
|
$named = Arr::get($attributes, 'named', null); |
100
|
|
|
|
101
|
1 |
|
if ($named === '' || $named === null) { |
102
|
1 |
|
$named = Str::lower($controller . '.' . $function); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
if (\is_string($named)) { |
106
|
1 |
|
return Str::lower($type . '.' . $named); |
107
|
|
|
} |
108
|
|
|
$type = Str::lower($type); |
109
|
|
|
|
110
|
|
|
$names = array_change_key_case($named, \CASE_LOWER); |
111
|
|
|
|
112
|
|
|
if (collect($names)->has($type)) { |
113
|
|
|
return Str::lower(collect($names)->get($type, \null)); |
114
|
|
|
} else { |
115
|
|
|
return Str::lower($type . '.' . $controller . '.' . $function); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|