Passed
Push — master ( 749b16...167cfa )
by Gabor
04:47
created

HtmlForm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 2
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types=1);
13
14
namespace WebHemi\Form\Html;
15
16
use WebHemi\Form\FormElementInterface;
17
use WebHemi\Form\FormInterface;
18
19
/**
20
 * Class HtmlForm
21
 */
22
class HtmlForm implements FormInterface
23
{
24
    /** @var string */
25
    private $name;
26
    /** @var string */
27
    private $action;
28
    /** @var string */
29
    private $method;
30
    /** @var array */
31
    private $formElements = [];
32
33
    /**
34
     * FormInterface constructor.
35
     *
36
     * @param string $name
37
     * @param string $action
38
     * @param string $method
39
     */
40
    public function __construct(string $name, string $action, string $method = 'POST')
41
    {
42
        $this->name = $name;
43
        $this->action = $action;
44
        $this->method = $method;
45
    }
46
47
    /**
48
     * Gets form name.
49
     *
50
     * @return string
51
     */
52
    public function getName() : string
53
    {
54
        return $this->name;
55
    }
56
57
    /**
58
     * Gets form action.
59
     *
60
     * @return string
61
     */
62
    public function getAction() : string
63
    {
64
        return $this->action;
65
    }
66
67
    /**
68
     * Gets form method.
69
     *
70
     * @return string
71
     */
72
    public function getMethod() : string
73
    {
74
        return $this->method;
75
    }
76
77
    /**
78
     * Adds an element to the form.
79
     *
80
     * @param FormElementInterface $formElement
81
     * @return FormInterface
82
     */
83
    public function addElement(FormElementInterface $formElement) : FormInterface
84
    {
85
        $elementName = $formElement->getName();
86
        $elementName = $this->name.'['.$elementName.']';
87
        $formElement->setName($elementName);
88
89
        $this->formElements[$elementName] = $formElement;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Returns all the elements assigned.
96
     *
97
     * @return array<FormElementInterface>
98
     */
99
    public function getElements() : array
100
    {
101
        return $this->formElements;
102
    }
103
}
104