Skip to content

Commit 0d5d8bd

Browse files
committed
update to stable
1 parent 319f19e commit 0d5d8bd

File tree

8 files changed

+411
-10
lines changed

8 files changed

+411
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

composer.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@
1515
}
1616
},
1717
"require": {
18-
"php": ">=7.1"
18+
"php": ">=7.1",
19+
"illuminate/support": "^7.0|^8.0",
20+
"illuminate/validation": "^7.0|^8.0"
1921
},
2022
"minimum-stability": "dev",
21-
"prefer-stable": true
23+
"prefer-stable": true,
24+
"extra": {
25+
"laravel": {
26+
"providers": [
27+
"Gustiawan\\FormBuilder\\FormBuilderServiceProvider"
28+
]
29+
}
30+
}
2231
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
namespace Gustiawan\FormBuilder\Commands;
3+
4+
use Illuminate\Support\Str;
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\File;
7+
8+
class CreateForm extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'make:form {name : The Name of the Form}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create Form';
23+
24+
/**
25+
* Create a new command instance.
26+
*
27+
* @return void
28+
*/
29+
public function __construct()
30+
{
31+
parent::__construct();
32+
}
33+
34+
/**
35+
* Execute the console command.
36+
*
37+
* @return int
38+
*/
39+
public function handle()
40+
{
41+
$file = File::get(__DIR__."/form.stub");
42+
$name_argument = $this->argument('name');
43+
$name_arguments = explode("/", $name_argument);
44+
45+
$name = $name_arguments[count($name_arguments) - 1];
46+
unset($name_arguments[count($name_arguments) - 1]);
47+
48+
$str = Str::of($file)->replaceFirst("{name}", $name);
49+
50+
if (count($name_arguments) > 0) {
51+
$str = Str::of($str)->replaceFirst("{namespace}", "\\".implode("\\", $name_arguments));
52+
} else {
53+
$str = Str::of($str)->replaceFirst("{namespace}", "");
54+
}
55+
56+
$path = base_path("app/Form");
57+
$folders = explode("/", $name_argument);
58+
unset($folders[count($folders) - 1]);
59+
$folder = implode("/", $folders);
60+
61+
if(!File::exists($path."/".$folder)) {
62+
File::makeDirectory($path."/".$folder, $mode = 0755, true, true);
63+
}
64+
65+
if (File::exists($path."/".$name_argument.".php")) {
66+
$this->info("File already exists!");
67+
return;
68+
}
69+
70+
File::put($path."/".$name_argument.".php", $str);
71+
$this->info('Form was created!');
72+
}
73+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace App\Form{namespace};
3+
4+
use Gustiawan\FormBuilder\Form;
5+
6+
class {name} extends Form
7+
{
8+
public function handle()
9+
{
10+
//
11+
}
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Gustiawan\FormBuilder\Components;
4+
5+
use Illuminate\View\Component;
6+
7+
class Form extends Component
8+
{
9+
public $form;
10+
/**
11+
* Create a new component instance.
12+
*
13+
* @return void
14+
*/
15+
public function __construct($form)
16+
{
17+
$form->handle();
18+
$this->form = $form;
19+
}
20+
21+
/**
22+
* Get the view / contents that represent the component.
23+
*
24+
* @return \Illuminate\Contracts\View\View|string
25+
*/
26+
public function render()
27+
{
28+
return view('form-generator::components.form');
29+
}
30+
}

src/Gustiawan/FormBuilder/Form.php

Lines changed: 182 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,182 @@
1-
<?php
2-
3-
namespace Gustiawan\FormBuilder;
4-
5-
class Form
6-
{
7-
//
8-
}
1+
<?php
2+
3+
namespace Gustiawan\FormBuilder;
4+
5+
class Form
6+
{
7+
public $action;
8+
public $method;
9+
public $fields = [];
10+
public $button = [
11+
"label" => "Submit",
12+
"color" => "bg-blue-400"
13+
];
14+
15+
/**
16+
* Create Form
17+
*
18+
* @param string $action action of form
19+
* @param string $method method of form
20+
* @return Form
21+
*/
22+
public function __construct($action, $method)
23+
{
24+
$this->action = $action;
25+
// need checker for method list
26+
$this->method = $method;
27+
28+
return $this;
29+
}
30+
31+
public function handle()
32+
{
33+
//
34+
}
35+
36+
/**
37+
* Create Input Field
38+
*
39+
* @param string $name name
40+
* @param any $value value
41+
* @param string $type type
42+
* @return Form
43+
*/
44+
public function text($name, $label, $value="")
45+
{
46+
$this->fields[] = [
47+
"label" => $label,
48+
"type" => "text",
49+
"name" => $name,
50+
"value" => $value
51+
];
52+
53+
return $this;
54+
}
55+
56+
/**
57+
* Create Password Field
58+
*
59+
* @param string $name name
60+
* @param any $value value
61+
* @param string $type type
62+
* @return Form
63+
*/
64+
public function password($name, $label, $value="")
65+
{
66+
$this->fields[] = [
67+
"label" => $label,
68+
"type" => "password",
69+
"name" => $name,
70+
"value" => $value
71+
];
72+
73+
return $this;
74+
}
75+
76+
/**
77+
* Create Password Field
78+
*
79+
* @param string $name name
80+
* @param any $value value
81+
* @param string $type type
82+
* @return Form
83+
*/
84+
public function date($name, $label, $value="")
85+
{
86+
$this->fields[] = [
87+
"label" => $label,
88+
"type" => "date",
89+
"name" => $name,
90+
"value" => $value
91+
];
92+
93+
return $this;
94+
}
95+
96+
/**
97+
* Create Password Field
98+
*
99+
* @param string $name name
100+
* @param any $value value
101+
* @param string $type type
102+
* @return Form
103+
*/
104+
public function textArea($name, $label, $value="")
105+
{
106+
$this->fields[] = [
107+
"label" => $label,
108+
"type" => "textarea",
109+
"name" => $name,
110+
"value" => $value
111+
];
112+
113+
return $this;
114+
}
115+
116+
/**
117+
* Create Password Field
118+
*
119+
* @param string $name name
120+
* @param any $value value
121+
* @param string $type type
122+
* @return Form
123+
*/
124+
public function select($name, $label, $choices, $value="")
125+
{
126+
$this->fields[] = [
127+
"label" => $label,
128+
"type" => "select",
129+
"name" => $name,
130+
"value" => $value,
131+
"choices" => $choices
132+
];
133+
134+
return $this;
135+
}
136+
137+
public function radio($name, $label, $choices, $value="")
138+
{
139+
$this->fields[] = [
140+
"label" => $label,
141+
"type" => "radio",
142+
"name" => $name,
143+
"value" => $value,
144+
"choices" => $choices
145+
];
146+
147+
return $this;
148+
}
149+
150+
public function checkBox($name, $label, $choices, array $value=[])
151+
{
152+
$this->fields[] = [
153+
"label" => $label,
154+
"type" => "checkbox",
155+
"name" => $name,
156+
"value" => $value,
157+
"choices" => $choices
158+
];
159+
160+
return $this;
161+
}
162+
163+
public function button($label="Submit", $color="bg-blue-500")
164+
{
165+
$this->button["label"] = $label;
166+
$this->button["color"] = $color;
167+
}
168+
169+
/**
170+
* check if form need csrf
171+
*
172+
* @return boolean
173+
*/
174+
public function isNeedCsrf()
175+
{
176+
if ($this->method == "GET") {
177+
return false;
178+
}
179+
180+
return true;
181+
}
182+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Gustiawan\FormBuilder;
4+
5+
use Gustiawan\FormBuilder\Components\Form;
6+
use Gustiawan\FormBuilder\Commands\CreateForm;
7+
use Illuminate\Support\ServiceProvider;
8+
9+
class FormBuilderServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* Register services.
13+
*
14+
* @return void
15+
*/
16+
public function register()
17+
{
18+
//
19+
}
20+
21+
/**
22+
* Bootstrap services.
23+
*
24+
* @return void
25+
*/
26+
public function boot()
27+
{
28+
$this->loadViewComponentsAs('form-generator', [
29+
Form::class,
30+
]);
31+
32+
$this->loadViewsFrom(__DIR__.'/views', 'form-generator');
33+
34+
if ($this->app->runningInConsole()) {
35+
$this->commands([
36+
CreateForm::class
37+
]);
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)