forked from pingpong-labs/generators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestGenerator.php
113 lines (95 loc) · 2.12 KB
/
RequestGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace Pingpong\Generators;
use Pingpong\Generators\Migrations\SchemaParser;
class RequestGenerator extends Generator
{
/**
* Get stub name.
*
* @var string
*/
protected $stub = 'request';
/**
* Get base path of destination file.
*
* @return string
*/
public function getBasePath()
{
return app_path().'/Http/Requests/';
}
/**
* Get destination path for generated file.
*
* @return string
*/
public function getPath()
{
return $this->getBasePath().$this->getName().'.php';
}
/**
* Get root namespace.
*
* @return string
*/
public function getRootNamespace()
{
return $this->getAppNamespace().'Http\Requests\\';
}
/**
* Get stub replacements.
*
* @return array
*/
public function getReplacements()
{
return array_merge(parent::getReplacements(), [
'auth' => $this->getAuth(),
'rules' => $this->getRules(),
]);
}
/**
* Get auth replacement.
*
* @return string
*/
public function getAuth()
{
$authorize = $this->auth ? 'true' : 'false';
return 'return '.$authorize.';';
}
/**
* Get replacement for "$RULES$".
*
* @return string
*/
public function getRules()
{
if (!$this->rules) {
return 'return [];';
}
$parser = new SchemaParser($this->rules);
$results = 'return ['.PHP_EOL;
foreach ($parser->toArray() as $field => $rules) {
$results .= $this->createRules($field, $rules);
}
$results .= "\t\t];";
return $results;
}
/**
* Create a rule.
*
* @param string $field
* @param string $rules
*
* @return string
*/
protected function createRules($field, $rules)
{
$rule = str_replace(['(', ')', ';'], [':', '', ','], implode('|', $rules));
if ($this->scaffold) {
$rule = 'required';
}
return "\t\t\t'{$field}' => '".$rule."',".PHP_EOL;
}
}