Passed
Push — allow_same_names ( ecc1c2 )
by Enjoys
05:13
created

Container::getElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
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\Element;
30
use Enjoys\Forms\Elements\Button;
31
use Enjoys\Forms\Elements\Captcha;
32
use Enjoys\Forms\Elements\Checkbox;
33
use Enjoys\Forms\Elements\Color;
34
use Enjoys\Forms\Elements\Csrf;
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\Html;
45
use Enjoys\Forms\Elements\Image;
46
use Enjoys\Forms\Elements\Month;
47
use Enjoys\Forms\Elements\Number;
48
use Enjoys\Forms\Elements\Password;
49
use Enjoys\Forms\Elements\Radio;
50
use Enjoys\Forms\Elements\Range;
51
use Enjoys\Forms\Elements\Reset;
52
use Enjoys\Forms\Elements\Search;
53
use Enjoys\Forms\Elements\Select;
54
use Enjoys\Forms\Elements\Submit;
55
use Enjoys\Forms\Elements\Tel;
56
use Enjoys\Forms\Elements\Text;
57
use Enjoys\Forms\Elements\Textarea;
58
use Enjoys\Forms\Elements\Time;
59
use Enjoys\Forms\Elements\Url;
60
use Enjoys\Forms\Elements\Week;
61
use Enjoys\Forms\Interfaces\CaptchaInterface;
62
use Enjoys\Forms\Interfaces\ElementInterface;
63
use Webmozart\Assert\Assert;
64
65
/**
66
 * @method Text text(string $name, string $label = null)
67
 * @method Hidden hidden(string $name, string $value = null)
68
 * @method Password password(string $name, string $label = null)
69
 * @method Submit submit(string $name, string $title = null)
70
 * @method Header header(string $title = null)
71
 * @method Color color(string $name, string $label = null)
72
 * @method Date date(string $name, string $label = null)
73
 * @method Datetime datetime(string $name, string $label = null)
74
 * @method Datetimelocal datetimelocal(string $name, string $label = null)
75
 * @method Email email(string $name, string $label = null)
76
 * @method Number number(string $name, string $label = null)
77
 * @method Range range(string $name, string $label = null)
78
 * @method Search search(string $name, string $label = null)
79
 * @method Tel tel(string $name, string $label = null)
80
 * @method Time time(string $name, string $label = null)
81
 * @method Url url(string $name, string $label = null)
82
 * @method Month month(string $name, string $label = null)
83
 * @method Week week(string $name, string $label = null)
84
 * @method Textarea textarea(string $name, string $label = null)
85
 * @method Select select(string $name, string $label = null)
86
 * @method Button button(string $name, string $title = null)
87
 * @method Datalist datalist(string $name, string $label = null)
88
 * @method Checkbox checkbox(string $name, string $label = null)
89
 * @method Image image(string $name, string $src = null)
90
 * @method Radio radio(string $name, string $title = null)
91
 * @method Reset reset(string $name, string $title = null)
92
 * @method Captcha captcha(CaptchaInterface $captcha)
93
 * @method Group group(string $title = null)
94
 * @method File file(string $name, string $label = null)
95
 * @method Csrf csrf()
96
 * @method Html html(string $html)
97
 *
98
 * @author Enjoys
99
 */
100
trait Container
101
{
102
    /**
103
     * @var Element[]
104
     */
105
    private array $elements = [];
106
107
108 58
    public function __call(string $name, array $arguments): Element
109
    {
110 58
        $className = '\Enjoys\\Forms\\Elements\\' . ucfirst($name);
111 58
        Assert::classExists($className);
112
        /** @var class-string<ElementInterface> $className */
113 56
        $element = new $className(...$arguments);
114
115
        /** @var Element $element */
116 56
        $this->addElement($element);
117 56
        return $element;
118
    }
119
120
    /**
121
     *
122
     * @param Element $element
123
     * @return $this
124
     * @noinspection PhpMissingReturnTypeInspection
125
     */
126 90
    public function addElement(Element $element)
127
    {
128 90
        $element->setRequest($this->getRequest());
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on Enjoys\Forms\Traits\Container. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
        $element->setRequest($this->/** @scrutinizer ignore-call */ getRequest());
Loading history...
Bug introduced by
$this->getRequest() of type Enjoys\Forms\Element is incompatible with the type Enjoys\ServerRequestWrapperInterface|null expected by parameter $request of Enjoys\Forms\Element::setRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

128
        $element->setRequest(/** @scrutinizer ignore-type */ $this->getRequest());
Loading history...
129 90
        if ($element->prepare() === true) {
0 ignored issues
show
introduced by
The condition $element->prepare() === true is always true.
Loading history...
130 14
            return $this;
131
        }
132
133
134 90
        if ($element->isAllowSameNames() === false
135 90
            && false !== $keyElement = $this->getElementKeyByName($element->getName())
136
        ) {
137 87
            $this->elements[$keyElement] = $element;
138 87
            return $this;
139
        }
140
141
142 90
        $this->elements[] = $element;
143 90
        return $this;
144
    }
145
146
147
    /**
148
     * @return Element[]
149
     */
150 15
    public function getElements(): array
151
    {
152 15
        return $this->elements;
153
    }
154
155
    /**
156
     *
157
     * @param string $name
158
     * @return Element|null
159
     */
160 19
    public function getElement(string $name): ?Element
161
    {
162 19
        if (false !== $key = $this->getElementKeyByName($name)) {
163 19
            return $this->elements[$key];
164
        }
165
166 3
        return null;
167
    }
168
169
    /**
170
     *
171
     * @param Element|null $element
172
     * @return $this
173
     */
174 21
    public function removeElement(?Element $element = null): self
175
    {
176 21
        if (null === $element) {
177 1
            return $this;
178
        }
179
180 21
        $key = $this->getElementKeyByName($element->getName());
181
182 21
        if ($key !== false) {
183 12
            unset($this->elements[$key]);
184
        }
185 21
        return $this;
186
    }
187
188 90
    private function getElementKeyByName(string $name): int|false
189
    {
190 90
        foreach ($this->elements as $key => $element) {
191 90
            if ($element->getName() === $name) {
192
                /** @var int $key */
193 87
                return $key;
194
            }
195
        }
196 90
        return false;
197
    }
198
199
200
}
201