-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathFactory.php
More file actions
121 lines (106 loc) · 3.31 KB
/
Factory.php
File metadata and controls
121 lines (106 loc) · 3.31 KB
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
<?php
namespace Gregwar\Formidable;
/**
* Form factory
*
* @author Grégoire Passault <g.passault@gmail.com>
*/
class Factory extends Language\LanguageAware
{
/**
* Form clas
*/
protected $formClass = '\Gregwar\Formidable\Form';
/**
* Parser class
*/
protected $parserClass = '\Gregwar\Formidable\Parser';
/**
* Field types
*/
protected $typeClasses = array(
'text' => '\Gregwar\Formidable\Fields\TextField',
'email' => '\Gregwar\Formidable\Fields\EmailField',
'number' => '\Gregwar\Formidable\Fields\NumberField',
'numeric' => '\Gregwar\Formidable\Fields\NumberField',
'int' => '\Gregwar\Formidable\Fields\IntField',
'integer' => '\Gregwar\Formidable\Fields\IntField',
'file' => '\Gregwar\Formidable\Fields\FileField',
'radio' => '\Gregwar\Formidable\Fields\RadioField',
'checkbox' => '\Gregwar\Formidable\Fields\CheckboxField',
'captcha' => '\Gregwar\Formidable\Fields\CaptchaField',
'hidden' => '\Gregwar\Formidable\Fields\HiddenField',
'multicheckbox' => '\Gregwar\Formidable\Fields\MulticheckboxField',
'multiradio' => '\Gregwar\Formidable\Fields\MultiradioField',
'password' => '\Gregwar\Formidable\Fields\PasswordField',
'date' => '\Gregwar\Formidable\Fields\DateField',
'button' => '\Gregwar\Formidable\Fields\ButtonField',
);
/**
* Objects types
*/
private $objectClasses = array(
'form' => '\Gregwar\Formidable\Head',
'textarea' => '\Gregwar\Formidable\Fields\Textarea',
'options' => '\Gregwar\Formidable\Fields\Options',
'option' => '\Gregwar\Formidable\Fields\Option',
'radios' => '\Gregwar\Formidable\Fields\Radios',
'select' => '\Gregwar\Formidable\Fields\Select',
'custom' => '\Gregwar\Formidable\Fields\Custom',
'multiple' => '\Gregwar\Formidable\Fields\Multiple'
);
/**
* Register a type
*/
public function registerType($type, $class)
{
$this->typeClasses[$type] = $class;
}
protected function inject($object)
{
if ($object instanceof Language\LanguageAware) {
$object->setLanguage($this->language);
}
return $object;
}
/**
* Get a parser
*/
public function getParser($content, $offset = 0)
{
$parserClass = $this->parserClass;
return $this->inject(new $parserClass($content, $this, $offset));
}
/**
* Get a field of a given type
*/
public function getField($name)
{
if (isset($this->typeClasses[$name])) {
return $this->inject(new $this->typeClasses[$name]);
} else {
throw new ParserException('Unknown field type: '.$name);
}
}
/**
* Get an object
*/
public function getObject($name)
{
return $this->inject(new $this->objectClasses[$name]);
}
/**
* Get a form in this factory
*/
public function getForm($pathOrContent, array $vars = array(), $cache = false) {
$formClass = $this->formClass;
return $this->inject(new $formClass($pathOrContent, $vars, $cache, $this));
}
/**
* Constructs the factory
*/
public function __construct()
{
$this->language = new Language\English;
}
}