This repository has been archived by the owner on Feb 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
/
FormOpen.php
112 lines (84 loc) · 2.02 KB
/
FormOpen.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
<?php
namespace AdamWathan\Form\Elements;
class FormOpen extends Element
{
protected $attributes = [
'method' => 'POST',
'action' => '',
];
protected $token;
protected $hiddenMethod;
public function render()
{
$tags = [sprintf('<form%s>', $this->renderAttributes())];
if ($this->hasToken() && ($this->attributes['method'] !== 'GET')) {
$tags[] = $this->token->render();
}
if ($this->hasHiddenMethod()) {
$tags[] = $this->hiddenMethod->render();
}
return implode($tags);
}
protected function hasToken()
{
return isset($this->token);
}
protected function hasHiddenMethod()
{
return isset($this->hiddenMethod);
}
public function post()
{
$this->setMethod('POST');
return $this;
}
public function get()
{
$this->setMethod('GET');
return $this;
}
public function put()
{
return $this->setHiddenMethod('PUT');
}
public function patch()
{
return $this->setHiddenMethod('PATCH');
}
public function delete()
{
return $this->setHiddenMethod('DELETE');
}
public function token($token)
{
$this->token = new Hidden('_token');
$this->token->value($token);
return $this;
}
protected function setHiddenMethod($method)
{
$this->setMethod('POST');
$this->hiddenMethod = new Hidden('_method');
$this->hiddenMethod->value($method);
return $this;
}
public function setMethod($method)
{
$this->setAttribute('method', $method);
return $this;
}
public function action($action)
{
$this->setAttribute('action', $action);
return $this;
}
public function encodingType($type)
{
$this->setAttribute('enctype', $type);
return $this;
}
public function multipart()
{
return $this->encodingType('multipart/form-data');
}
}