Form::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 20
rs 9.7998
cc 1
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Contact\Form\Factory;
6
7
use AbterPhp\Admin\Form\Factory\Base;
8
use AbterPhp\Contact\Domain\Entities\Form as Entity;
9
use AbterPhp\Framework\Constant\Html5;
10
use AbterPhp\Framework\Form\Container\FormGroup;
11
use AbterPhp\Framework\Form\Element\Input;
12
use AbterPhp\Framework\Form\Extra\Help;
13
use AbterPhp\Framework\Form\IForm;
14
use AbterPhp\Framework\Form\Label\Label;
15
use AbterPhp\Framework\I18n\ITranslator;
16
use Opulence\Orm\IEntity;
17
use Opulence\Sessions\ISession;
18
19
class Form extends Base
20
{
21
    /**
22
     * Form constructor.
23
     *
24
     * @param ISession    $session
25
     * @param ITranslator $translator
26
     */
27
    public function __construct(ISession $session, ITranslator $translator)
28
    {
29
        parent::__construct($session, $translator);
30
    }
31
32
    /**
33
     * @param string       $action
34
     * @param string       $method
35
     * @param string       $showUrl
36
     * @param IEntity|null $entity
37
     *
38
     * @return IForm
39
     */
40
    public function create(string $action, string $method, string $showUrl, ?IEntity $entity = null): IForm
41
    {
42
        assert($entity instanceof Entity, new \InvalidArgumentException());
43
44
        $this->createForm($action, $method)
45
            ->addDefaultElements()
46
            ->addName($entity)
47
            ->addIdentifier($entity)
48
            ->addToName($entity)
49
            ->addToEmail($entity)
50
            ->addSuccessUrl($entity)
51
            ->addFailureUrl($entity)
52
            ->addMaxBodyLength($entity)
53
            ->addDefaultButtons($showUrl);
54
55
        $form = $this->form;
56
57
        $this->form = null;
58
59
        return $form;
60
    }
61
62
    /**
63
     * @param Entity $entity
64
     *
65
     * @return $this
66
     */
67
    protected function addName(Entity $entity): Form
68
    {
69
        $input = new Input('name', 'name', $entity->getName());
70
        $label = new Label('name', 'contact:formName');
71
72
        $this->form[] = new FormGroup($input, $label);
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param Entity $entity
79
     *
80
     * @return $this
81
     */
82
    protected function addIdentifier(Entity $entity): Form
83
    {
84
        $input = new Input(
85
            'identifier',
86
            'identifier',
87
            $entity->getIdentifier(),
88
            [],
89
            [Html5::ATTR_CLASS => 'semi-auto']
90
        );
91
        $label = new Label('identifier', 'files:fileCategoryIdentifier');
92
        $help  = new Help('files:fileCategoryIdentifierHelp');
93
94
        $this->form[] = new FormGroup($input, $label, $help);
95
96
        return $this;
97
    }
98
99
    /**
100
     * @param Entity $entity
101
     *
102
     * @return $this
103
     */
104
    protected function addToName(Entity $entity): Form
105
    {
106
        $input = new Input('to_name', 'to_name', $entity->getToName());
107
        $label = new Label('to_name', 'contact:formToName');
108
109
        $this->form[] = new FormGroup($input, $label);
110
111
        return $this;
112
    }
113
114
    /**
115
     * @param Entity $entity
116
     *
117
     * @return $this
118
     */
119
    protected function addToEmail(Entity $entity): Form
120
    {
121
        $input = new Input('to_email', 'to_email', $entity->getToEmail(), [], [Html5::ATTR_TYPE => Input::TYPE_EMAIL]);
122
        $label = new Label('to_email', 'contact:formToEmail');
123
124
        $this->form[] = new FormGroup($input, $label);
125
126
        return $this;
127
    }
128
129
    /**
130
     * @param Entity $entity
131
     *
132
     * @return $this
133
     */
134
    protected function addSuccessUrl(Entity $entity): Form
135
    {
136
        $input = new Input(
137
            'success_url',
138
            'success_url',
139
            $entity->getSuccessUrl()
140
        );
141
        $label = new Label('success_url', 'contact:formSuccessUrl');
142
        $help  = new Help('contact:hintSuccessUrl');
143
144
        $this->form[] = new FormGroup($input, $label, $help);
145
146
        return $this;
147
    }
148
149
    /**
150
     * @param Entity $entity
151
     *
152
     * @return $this
153
     */
154
    protected function addFailureUrl(Entity $entity): Form
155
    {
156
        $input = new Input(
157
            'failure_url',
158
            'failure_url',
159
            $entity->getFailureUrl()
160
        );
161
        $label = new Label('failure_url', 'contact:formFailureUrl');
162
        $help  = new Help('contact:hintFailureUrl');
163
164
        $this->form[] = new FormGroup($input, $label, $help);
165
166
        return $this;
167
    }
168
169
    /**
170
     * @param Entity $entity
171
     *
172
     * @return $this
173
     */
174
    protected function addMaxBodyLength(Entity $entity): Form
175
    {
176
        $input = new Input(
177
            'max_body_length',
178
            'max_body_length',
179
            (string)$entity->getMaxBodyLength(),
180
            [],
181
            [
182
                Html5::ATTR_TYPE => Input::TYPE_NUMBER,
183
                Html5::ATTR_MIN  => 0,
184
                Html5::ATTR_MAX  => 10000,
185
                Html5::ATTR_STEP => 100,
186
            ]
187
        );
188
        $label = new Label('max_body_length', 'contact:formMaxBodyLength');
189
        $help  = new Help('contact:hintMaxBodyLength');
190
191
        $this->form[] = new FormGroup($input, $label, $help);
192
193
        return $this;
194
    }
195
}
196