1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Form; |
4
|
|
|
|
5
|
|
|
use Nip\Form\Elements\AbstractElement as ElementAbstract; |
6
|
|
|
use Nip\View; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class AbstractForm |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractForm implements FormInterface |
13
|
|
|
{ |
14
|
|
|
use Traits\DataProcessingTrait; |
15
|
|
|
use Traits\CanInitializeTrait; |
16
|
|
|
use Traits\HasAttributesTrait; |
17
|
|
|
use Traits\HasButtonsTrait; |
18
|
|
|
use Traits\HasCacheTrait; |
19
|
|
|
use Traits\HasClassTrait; |
20
|
|
|
use Traits\HasDisplayGroupsTrait; |
21
|
|
|
use Traits\HasElementsTrait; |
22
|
|
|
use Traits\HasErrorsTrait; |
23
|
|
|
use Traits\HasExecutionMethodsTrait; |
24
|
|
|
use Traits\HasRendererTrait; |
25
|
|
|
use Traits\MagicMethodElementsFormTrait; |
26
|
|
|
use Traits\MessagesTrait; |
27
|
|
|
use Traits\NewElementsMethods; |
28
|
|
|
|
29
|
|
|
public const ENCTYPE_URLENCODED = 'application/x-www-form-urlencoded'; |
30
|
|
|
public const ENCTYPE_MULTIPART = 'multipart/form-data'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $methods = ['delete', 'get', 'post', 'put']; |
36
|
|
|
|
37
|
|
|
protected $_options = []; |
38
|
|
|
|
39
|
|
|
protected $_decorators = []; |
40
|
|
|
|
41
|
|
|
protected $controllerView = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* AbstractForm constructor. |
45
|
27 |
|
*/ |
46
|
|
|
public function __construct() |
47
|
27 |
|
{ |
48
|
27 |
|
} |
49
|
27 |
|
|
50
|
|
|
/** |
51
|
27 |
|
* @param string $action |
52
|
|
|
* @return AbstractForm |
53
|
27 |
|
*/ |
54
|
27 |
|
public function setAction(string $action): AbstractForm |
55
|
|
|
{ |
56
|
27 |
|
return $this->setAttrib('action', (string)$action); |
57
|
|
|
} |
58
|
27 |
|
|
59
|
|
|
/** |
60
|
|
|
* @param $name |
61
|
27 |
|
* @return ElementAbstract|null |
62
|
|
|
*/ |
63
|
|
|
public function __get($name) |
64
|
|
|
{ |
65
|
|
|
$element = $this->getElement($name); |
66
|
|
|
if ($element) { |
|
|
|
|
67
|
|
|
return $element; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return null; |
71
|
|
|
} |
72
|
27 |
|
|
73
|
|
|
/** |
74
|
27 |
|
* @param $key |
75
|
|
|
* @param $value |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function setOption($key, $value) |
79
|
|
|
{ |
80
|
1 |
|
$key = (string)$key; |
81
|
|
|
$this->_options[$key] = $value; |
82
|
1 |
|
|
83
|
1 |
|
return $this; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $key |
88
|
|
|
* @return mixed|null |
89
|
|
|
*/ |
90
|
|
|
public function getOption($key) |
91
|
|
|
{ |
92
|
|
|
$key = (string)$key; |
93
|
|
|
if (!isset($this->_options[$key])) { |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->_options[$key]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $method |
102
|
|
|
* @return AbstractForm |
103
|
|
|
*/ |
104
|
|
|
public function setMethod($method) |
105
|
|
|
{ |
106
|
|
|
if (in_array($method, $this->methods)) { |
107
|
|
|
return $this->setAttrib('method', $method); |
108
|
|
|
} |
109
|
|
|
trigger_error('Method is not valid', E_USER_ERROR); |
110
|
|
|
|
111
|
|
|
return null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getName() |
118
|
|
|
{ |
119
|
|
|
return get_class($this); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return View|null |
124
|
|
|
*/ |
125
|
|
|
public function getControllerView() |
126
|
|
|
{ |
127
|
|
|
if (!$this->controllerView) { |
128
|
|
|
$this->controllerView = app('kernel')->getDispatcher()->getCurrentController()->getView(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->controllerView; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|