-
Notifications
You must be signed in to change notification settings - Fork 0
/
Builder.php
143 lines (123 loc) · 3.39 KB
/
Builder.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* PHP form builder
*
* @author Brendan Markham
* @copyright 2016
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @version 1.0.0
*/
namespace Php\Form\Builder;
use Php\Form\Builder\Element;
use Php\Form\Builder\Signature\Checkable;
class Builder
{
/**
* @var Element\Form
*/
private $form;
/**
* @var Element\Fieldset
*/
private $temp_fieldset;
/**
* @var Element\Group
*/
private $temp_group;
public function __construct(array $form_attributes = [])
{
$this->form = new Element\Form($form_attributes);
}
public function __call($name, $arguments)
{
return call_user_func_array([$this->form, $name], $arguments);
}
public function __get($name)
{
return $this->form->{$name};
}
public function __set($name, $value)
{
return $this->form->{$name} = $value;
}
/**
* @return Element\Form
*/
public function get_form()
{
return $this->form;
}
public function fieldset($legend = null, array $attributes = [])
{
$this->close_fieldset();
$this->close_group();
$this->temp_fieldset = new Element\Fieldset([], $legend, $attributes);
return $this;
}
public function close_fieldset()
{
if ($this->temp_fieldset) {
$this->close_group();
$this->form->add_line($this->temp_fieldset);
$this->temp_fieldset = null;
}
return $this;
}
public function group($help = null, array $help_attributes = [], Element\HTML $buffer = null, array $attributes = [])
{
$this->close_group();
$this->temp_group = new Element\Group([], $help, $help_attributes, $buffer, $attributes);
return $this;
}
public function close_group()
{
if ($this->temp_group) {
if ($this->temp_fieldset) {
$this->add_group($this->temp_group);
} else {
$this->form->add_line($this->temp_group);
}
$this->temp_group = null;
}
return $this;
}
public function pair(Element\Control $control, $label = null, array $attributes = [])
{
if (!$this->temp_group) {
$this->group();
}
if ($control instanceof Checkable) {
$pair = new Element\CheckablePair($control, $label, $attributes);
} else {
$pair = new Element\Pair($control, $label, $attributes);
}
$this->temp_group->add_pair($pair);
return $this;
}
public function submit(array $submit_attributes = [])
{
$this->close_group(); //<* Assume the current group (if any) can be finalised
$group = new Element\Group([new Element\Pair(new Element\Submit($submit_attributes))]);
$group->add_attribute('class', 'submit');
$this->add_group($group);
return $this;
}
public function generate($tabs = 0)
{
$this->finalise();
return $this->form->generate($tabs);
}
public function finalise()
{
$this->close_group();
$this->close_fieldset();
}
protected function add_group(Element\Group $group)
{
if ($this->temp_fieldset) {
$this->temp_fieldset->add_group($group);
} else {
$this->form->add_line($group);
}
}
}