Passed
Push — master ( 100bd0...da6946 )
by Gabor
05:01
created

ServiceAdapter::initialize()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
nc 2
cc 3
eloc 7
nop 3
crap 12
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\ServiceAdapter\Base;
15
16
use JsonSerializable;
17
use RuntimeException;
18
use WebHemi\Form\ElementInterface;
19
use WebHemi\Form\ServiceInterface;
20
21
/**
22
 * Class ServiceAdapter.
23
 */
24
class ServiceAdapter implements ServiceInterface, JsonSerializable
25
{
26
    /** @var string */
27
    private $name;
28
    /** @var string */
29
    private $action;
30
    /** @var string */
31
    private $method;
32
    /** @var array */
33
    private $formElements = [];
34
35
    /**
36
     * ServiceAdapter constructor.
37
     *
38
     * @param string|null $name
39
     * @param string|null $action
40
     * @param string      $method
41
     */
42 4
    public function __construct(string $name = null, string $action = null, string $method = 'POST')
43
    {
44 4
        $this->name = $name;
45 4
        $this->action = $action;
46 4
        $this->method = $method;
47 4
    }
48
49
    /**
50
     * Initializes the form if it didn't happen in the constructor. (Used mostly in presets).
51
     *
52
     * @param string $name
53
     * @param string $action
54
     * @param string $method
55
     * @throws RuntimeException
56
     * @return ServiceInterface
57
     */
58
    public function initialize(string $name, string $action, string $method = 'POST') : ServiceInterface
59
    {
60
        if (isset($this->name) || isset($this->action)) {
61
            throw new RuntimeException('The form had been already initialized!', 1000);
62
        }
63
64
        $this->name = $name;
65
        $this->action = $action;
66
        $this->method = $method;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Gets form name.
73
     *
74
     * @return string
75
     */
76 1
    public function getName() : string
77
    {
78 1
        return $this->name;
79
    }
80
81
    /**
82
     * Gets form action.
83
     *
84
     * @return string
85
     */
86 1
    public function getAction() : string
87
    {
88 1
        return $this->action;
89
    }
90
91
    /**
92
     * Gets form method.
93
     *
94
     * @return string
95
     */
96 1
    public function getMethod() : string
97
    {
98 1
        return $this->method;
99
    }
100
101
    /**
102
     * Adds an element to the form.
103
     *
104
     * @param ElementInterface $formElement
105
     * @return ServiceInterface
106
     */
107 2
    public function addElement(ElementInterface $formElement) : ServiceInterface
108
    {
109 2
        $elementName = $formElement->getName();
110
111 2
        if (!isset($this->formElements[$elementName])) {
112 2
            $elementName = $this->name.'['.$elementName.']';
113 2
            $formElement->setName($elementName);
114
        }
115
116 2
        $this->formElements[$elementName] = $formElement;
117
118 2
        return $this;
119
    }
120
121
    /**
122
     * Returns all the elements assigned.
123
     *
124
     * @return ElementInterface[]
125
     */
126 3
    public function getElements() : array
127
    {
128 3
        return $this->formElements;
129
    }
130
131
    /**
132
     * Loads data into the form.
133
     *
134
     * @param array $data
135
     * @return ServiceInterface
136
     */
137 1
    public function loadData(array $data) : ServiceInterface
138
    {
139 1
        $formData = $data[$this->name] ?? [];
140
141 1
        foreach ($formData as $elementName => $elementValue) {
142 1
            $fullName = $this->name.'['.$elementName.']';
143
            /** @var ElementInterface $formElement */
144 1
            $formElement = $this->formElements[$fullName] ?? null;
145
146 1
            if ($formElement) {
147 1
                if (!is_array($elementValue)) {
148 1
                    $elementValue = [$elementValue];
149
                }
150 1
                $formElement->setValues($elementValue);
151
            }
152
        }
153
154 1
        return $this;
155
    }
156
157
    /**
158
     * Defines the data which are presented during the json serialization.
159
     *
160
     * @return array
161
     */
162 1
    public function jsonSerialize() : array
163
    {
164
        $formData = [
165 1
            'name' => $this->name,
166 1
            'action' => $this->action,
167 1
            'method' => $this->method,
168
            'data' => [],
169
            'errors' => []
170
        ];
171
172
        /**
173
         * @var string $elementName
174
         * @var ElementInterface $formElement
175
         */
176 1
        foreach ($this->formElements as $formElement) {
177 1
            $formData['data'][$formElement->getId()] = $formElement->getValues();
178
179 1
            $errors = $formElement->getErrors();
180
181 1
            if (!empty($errors)) {
182 1
                $formData['errors'][$formElement->getId()] = $errors;
183
            }
184
        }
185
186 1
        return $formData;
187
    }
188
}
189