|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ntentan\honam\engines\php\helpers; |
|
4
|
|
|
|
|
5
|
|
|
use ntentan\honam\engines\php\Helper; |
|
6
|
|
|
use ntentan\honam\engines\php\helpers\form\Container; |
|
7
|
|
|
use ntentan\honam\engines\php\helpers\form\ElementFactory; |
|
8
|
|
|
use ntentan\honam\exceptions\HonamException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* A helper for rendering forms. |
|
12
|
|
|
*/ |
|
13
|
|
|
class FormHelper extends Helper |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
use ElementFactory; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* An instance of the container for the Form. |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $containers = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* An instance of the first container added to the form. |
|
26
|
|
|
* This is saved so that methods that need to access the first container can still work after the form is closed. |
|
27
|
|
|
*/ |
|
28
|
|
|
private $baseContainer; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Data to be rendered into the form. |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $data = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Any errors that should be rendered with the form. |
|
38
|
|
|
*/ |
|
39
|
|
|
private $errors = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Renders the form when the value is used as a string |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __toString() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->baseContainer->setData($this->data); |
|
48
|
|
|
$this->baseContainer->setErrors($this->errors); |
|
49
|
|
|
$rendered = (string) $this->baseContainer; |
|
50
|
|
|
$this->baseContainer = null; |
|
51
|
|
|
return $rendered; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function setId($id) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->getContainer()->setId($id); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function setErrors($errors) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->errors = $errors; |
|
62
|
|
|
return $this->container; |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function setData($data) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->data = $data; |
|
68
|
|
|
return $this->container; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function open() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->open_form(); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function close($submit = 'Submit') |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->close_form($submit); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function data(array $data) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->data = $data; |
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function errors(array $errors) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->errors = $errors; |
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function pushContainer(string $name, Container $container) |
|
94
|
|
|
{ |
|
95
|
|
|
if($this->baseContainer === null && empty($this->containers)) { |
|
96
|
|
|
$this->baseContainer = $container; |
|
97
|
|
|
} else if ($this->baseContainer !== null && empty($this->containers)) { |
|
98
|
|
|
throw new HonamException("Cannot add a container to a form that has already been closed."); |
|
99
|
|
|
} |
|
100
|
|
|
array_push($this->containers, [$name, $container]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function popContainer(string $name) |
|
104
|
|
|
{ |
|
105
|
|
|
$container = array_pop($this->containers); |
|
106
|
|
|
if($name === $container[0]) { |
|
107
|
|
|
return $container[1]; |
|
108
|
|
|
} else { |
|
109
|
|
|
throw new HonamException("You cannot close the $name container while {$container[0]} is open."); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function getActiveContainer(): Container |
|
114
|
|
|
{ |
|
115
|
|
|
return end($this->containers)[1]; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|