-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathForm.php
65 lines (54 loc) · 1.77 KB
/
Form.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
<?php
namespace Amarkal\Form;
use Amarkal\UI;
/**
* Implements a Form controller.
*
* The form object is used to encapsulate UI components and the update/validation
* process into a single entity. The template script can be overriden by a child
* class or by 'Form::set_script_path()' to tailor the form rendering output to
* the application needs.
*/
class Form extends \Amarkal\Template\Controller
{
private $components;
public $updater;
private $names;
public function __construct( array $components = array() )
{
$this->components = $components;
$this->updater = new Updater( $components );
$this->names = array();
$this->validate_components($components);
}
public function get_components()
{
return $this->components;
}
/**
* Internally used to validate each form component.
*
* @param \Amarkal\UI\AbstractComponent[] $components
* @throws \Amarkal\Form\DuplicateNameException
*/
private function validate_components( $components )
{
foreach( $components as $component )
{
if( ! $component instanceof UI\AbstractComponent )
{
throw new WrongTypeException( \gettype( $component ) );
}
if( $component instanceof UI\Components\Composite )
{
$this->validate_components( $component->components );
continue;
}
if( $component instanceof UI\ValueComponentInterface && in_array( $component->name, $this->names ) )
{
throw new DuplicateNameException( $component->name );
}
$this->names[] = $component->name;
}
}
}