This repository has been archived by the owner on Mar 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormFactory.php
177 lines (165 loc) · 4.37 KB
/
FormFactory.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Cli\Factory;
use Ulrack\Cli\Common\Element\Form\FieldInterface;
use Ulrack\Cli\Common\Io\OptionProviderInterface;
use Ulrack\Cli\Common\Theme\ThemeInterface;
use Ulrack\Cli\Common\Element\FormInterface;
use Ulrack\Cli\Component\Element\Form\Field;
use Ulrack\Cli\Component\Element\FormElement;
use Ulrack\Cli\Common\Element\ElementInterface;
use Ulrack\Cli\Common\Factory\IoFactoryInterface;
use Ulrack\Cli\Component\Element\Form\ArrayField;
use Ulrack\Cli\Common\Factory\FormFactoryInterface;
class FormFactory implements FormFactoryInterface
{
/**
* Contains the io factory to create writers and readers.
*
* @var IoFactoryInterface
*/
private $ioFactory;
/**
* Constructor.
*
* @param IoFactoryInterface $ioFactory
* @param ThemeInterface $theme
*/
public function __construct(
IoFactoryInterface $ioFactory,
ThemeInterface $theme
) {
$this->ioFactory = $ioFactory;
$this->theme = $theme;
}
/**
* Creates a new form.
*
* @param ElementInterface ...$description
*
* @return FormInterface
*/
public function createForm(
ElementInterface ...$description
): FormInterface {
return new FormElement(
...$description
);
}
/**
* Creates a new field.
*
* @param ElementInterface $label
*
* @return FieldInterface
*/
public function createField(
ElementInterface $label
): FieldInterface {
return new Field(
$this->ioFactory->createStandardReader(),
$label
);
}
/**
* Creates a new obscured field.
*
* @param ElementInterface $label
*
* @return FieldInterface
*/
public function createObscuredField(
ElementInterface $label
): FieldInterface {
return new Field(
$this->ioFactory->createHiddenReader(),
$label
);
}
/**
* Creates a new autocompleting field.
*
* @param ElementInterface $label
* @param OptionProviderInterface $optionProvider
* @param string $style
*
* @return FieldInterface
*/
public function createAutocompletingField(
ElementInterface $label,
OptionProviderInterface $optionProvider,
string $style = 'autocomplete'
): FieldInterface {
return new Field(
$this->ioFactory->createAutocompletingReader(
$optionProvider,
$this->theme->getStyle($style)
),
$label
);
}
/**
* Creates a new array field.
*
* @param ElementInterface $label
* @param ElementInterface $extraLabel
*
* @return FieldInterface
*/
public function createArrayField(
ElementInterface $label,
ElementInterface $extraLabel
): FieldInterface {
return new ArrayField(
$this->ioFactory->createStandardReader(),
$label,
$this->createField($extraLabel)
);
}
/**
* Creates a new obscured array field.
*
* @param ElementInterface $label
* @param ElementInterface $extraLabel
*
* @return FieldInterface
*/
public function createObscuredArrayField(
ElementInterface $label,
ElementInterface $extraLabel
): FieldInterface {
return new ArrayField(
$this->ioFactory->createHiddenReader(),
$label,
$this->createField($extraLabel)
);
}
/**
* Creates a new autocompleting array field.
*
* @param ElementInterface $label
* @param ElementInterface $extraLabel
* @param OptionProviderInterface $optionProvider
* @param string $style
*
* @return FieldInterface
*/
public function createAutocompletingArrayField(
ElementInterface $label,
ElementInterface $extraLabel,
OptionProviderInterface $optionProvider,
string $style = 'autocomplete'
): FieldInterface {
return new ArrayField(
$this->ioFactory->createAutocompletingReader(
$optionProvider,
$this->theme->getStyle($style)
),
$label,
$this->createField($extraLabel)
);
}
}