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
|
|
|
/** |
55
|
|
|
* Set the ID of the form. |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function setId($id) |
59
|
|
|
{ |
60
|
|
|
$this->getContainer()->setId($id); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function open() |
64
|
|
|
{ |
65
|
|
|
return $this->open_form(); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function close($submit = 'Submit') |
69
|
|
|
{ |
70
|
|
|
return $this->close_form($submit); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function pushContainer(string $name, Container $container) |
74
|
|
|
{ |
75
|
|
|
if ($this->baseContainer === null && empty($this->containers)) { |
76
|
|
|
// If a container is opened and there is no base container, we can assume its the base container. |
77
|
|
|
$this->baseContainer = $container; |
78
|
|
|
} else if ($this->baseContainer !== null && empty($this->containers)) { |
79
|
|
|
// If the container is stack is empty while we still have a base container, something is wrong. |
80
|
|
|
throw new HonamException("Cannot add a container to a form that has already been closed."); |
81
|
|
|
} |
82
|
|
|
array_push($this->containers, ['name' => $name, 'instance' => $container]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function popContainer(string $name) |
86
|
|
|
{ |
87
|
|
|
$container = array_pop($this->containers); |
88
|
|
|
if ($name === $container['name']) { |
89
|
|
|
// Add this container to a parent if containers are nested in any way. |
90
|
|
|
$currentContainer = end($this->containers); |
91
|
|
|
if ($currentContainer === false) { |
92
|
|
|
$this->baseContainer = null; |
93
|
|
|
} else { |
94
|
|
|
$currentContainer['instance']->add($container['instance']); |
95
|
|
|
} |
96
|
|
|
return $container['instance']; |
97
|
|
|
} else { |
98
|
|
|
throw new HonamException("You cannot close the $name container while {$container['name']} is open."); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getActiveContainer(): Container |
103
|
|
|
{ |
104
|
|
|
return end($this->containers)['instance']; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|