Passed
Push — 3.x ( afbb8c...a19ccf )
by Enjoys
08:15
created

Container::addElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2020 Enjoys.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace Enjoys\Forms\Traits;
28
29
use Enjoys\Forms\Captcha\CaptchaInterface;
30
use Enjoys\Forms\Element;
31
use Enjoys\Forms\Elements\Button;
32
use Enjoys\Forms\Elements\Captcha;
33
use Enjoys\Forms\Elements\Checkbox;
34
use Enjoys\Forms\Elements\Color;
35
use Enjoys\Forms\Elements\Datalist;
36
use Enjoys\Forms\Elements\Date;
37
use Enjoys\Forms\Elements\Datetime;
38
use Enjoys\Forms\Elements\Datetimelocal;
39
use Enjoys\Forms\Elements\Email;
40
use Enjoys\Forms\Elements\File;
41
use Enjoys\Forms\Elements\Group;
42
use Enjoys\Forms\Elements\Header;
43
use Enjoys\Forms\Elements\Hidden;
44
use Enjoys\Forms\Elements\Image;
45
use Enjoys\Forms\Elements\Month;
46
use Enjoys\Forms\Elements\Number;
47
use Enjoys\Forms\Elements\Password;
48
use Enjoys\Forms\Elements\Radio;
49
use Enjoys\Forms\Elements\Range;
50
use Enjoys\Forms\Elements\Reset;
51
use Enjoys\Forms\Elements\Search;
52
use Enjoys\Forms\Elements\Select;
53
use Enjoys\Forms\Elements\Submit;
54
use Enjoys\Forms\Elements\Tel;
55
use Enjoys\Forms\Elements\Text;
56
use Enjoys\Forms\Elements\Textarea;
57
use Enjoys\Forms\Elements\Time;
58
use Enjoys\Forms\Elements\Url;
59
use Enjoys\Forms\Elements\Week;
60
use Enjoys\Forms\Exception\ExceptionElement;
61
62
/**
63
 * @method Text text(string $name, string $label = null)
64
 * @method Hidden hidden(string $name, string $value = null)
65
 * @method Password password(string $name, string $label = null)
66
 * @method Submit submit(string $name, string $title = null)
67
 * @method Header header(string $title = null)
68
 * @method Color color(string $name, string $label = null)
69
 * @method Date date(string $name, string $label = null)
70
 * @method Datetime datetime(string $name, string $label = null)
71
 * @method Datetimelocal datetimelocal(string $name, string $label = null)
72
 * @method Email email(string $name, string $label = null)
73
 * @method Number number(string $name, string $label = null)
74
 * @method Range range(string $name, string $label = null)
75
 * @method Search search(string $name, string $label = null)
76
 * @method Tel tel(string $name, string $label = null)
77
 * @method Time time(string $name, string $label = null)
78
 * @method Url url(string $name, string $label = null)
79
 * @method Month month(string $name, string $label = null)
80
 * @method Week week(string $name, string $label = null)
81
 * @method Textarea textarea(string $name, string $label = null)
82
 * @method Select select(string $name, string $label = null)
83
 * @method Button button(string $name, string $title = null)
84
 * @method Datalist datalist(string $name, string $label = null)
85
 * @method Checkbox checkbox(string $name, string $label = null)
86
 * @method Image image(string $name, string $src = null)
87
 * @method Radio radio(string $name, string $title = null)
88
 * @method Reset reset(string $name, string $title = null)
89
 * @method Captcha captcha(CaptchaInterface $captcha)
90
 * @method Group group(string $title = null)
91
 * @method File file(string $name, string $label = null)
92
 *
93
 * @author Enjoys
94
 */
95
trait Container
96
{
97
98
    /**
99
     *
100
     * @var array objects \Enjoys\Forms\Element
101
     */
102
    private array $elements = [];
103
104
    /**
105
     * @param string $name
106
     * @param array $arguments
107
     *
108
     * @return Element
109
     * @throws ExceptionElement
110
     */
111 78
    public function __call(string $name, array $arguments)
112
    {
113 78
        $class_name = '\Enjoys\\Forms\\Elements\\' . ucfirst($name);
114 78
        if (!class_exists($class_name)) {
115 2
            throw new ExceptionElement("Class <b>{$class_name}</b> not found");
116
        }
117
        /** @var Element $element */
118 77
        $element = new $class_name(...$arguments);
119 77
        $this->addElement($element);
120 77
        return $element;
121
    }
122
123
    /**
124
     *
125
     * @param Element $element
126
     * @return $this
127
     */
128 77
    public function addElement(Element $element): self
129
    {
130 77
        $element->setRequest($this->request);
131 77
        $this->elements[$element->getName()] = $element;
132 77
        return $this;
133
    }
134
135
    /**
136
     * @return array<object>
137
     */
138 26
    public function getElements(): array
139
    {
140 26
        return $this->elements;
141
    }
142
143
    /**
144
     *
145
     * @param string $name
146
     * @return Element|null
147
     */
148 2
    public function getElement(string $name): ?Element
149
    {
150 2
        if ($this->elementExists($name)) {
151 2
            return $this->elements[$name];
152
        }
153
154 2
        return null;
155
    }
156
157
    /**
158
     *
159
     * @param Element|null $element
160
     * @return $this
161
     */
162 20
    public function removeElement(?Element $element = null): self
163
    {
164 20
        if (null === $element) {
165 1
            return $this;
166
        }
167
168 20
        if ($this->elementExists($element->getName())) {
169 13
            unset($this->elements[$element->getName()]);
170
        }
171 20
        return $this;
172
    }
173
174
    /**
175
     *
176
     * @param string $name
177
     * @return bool
178
     */
179 20
    private function elementExists(string $name): bool
180
    {
181 20
        if (array_key_exists($name, $this->elements) && $this->elements[$name] instanceof Element) {
182 13
            return true;
183
        }
184 9
        return false;
185
    }
186
}
187