Passed
Push — master ( 0d72e8...44be60 )
by Gabor
04:58
created

ServiceAdapter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 140
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getName() 0 4 1
A getAction() 0 4 1
A getMethod() 0 4 1
A addElement() 0 10 1
A getElements() 0 4 1
A loadData() 0 19 4
B jsonSerialize() 0 26 3
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 WebHemi\Form\ElementInterface;
18
use WebHemi\Form\ServiceInterface;
19
20
/**
21
 * Class ServiceAdapter.
22
 */
23
class ServiceAdapter implements ServiceInterface, JsonSerializable
24
{
25
    /** @var string */
26
    private $name;
27
    /** @var string */
28
    private $action;
29
    /** @var string */
30
    private $method;
31
    /** @var array */
32
    private $formElements = [];
33
34
    /**
35
     * ServiceAdapter constructor.
36
     *
37
     * @param string $name
38
     * @param string $action
39
     * @param string $method
40
     */
41 4
    public function __construct(string $name, string $action, string $method = 'POST')
42
    {
43 4
        $this->name = $name;
44 4
        $this->action = $action;
45 4
        $this->method = $method;
46 4
    }
47
48
    /**
49
     * Gets form name.
50
     *
51
     * @return string
52
     */
53 1
    public function getName() : string
54
    {
55 1
        return $this->name;
56
    }
57
58
    /**
59
     * Gets form action.
60
     *
61
     * @return string
62
     */
63 1
    public function getAction() : string
64
    {
65 1
        return $this->action;
66
    }
67
68
    /**
69
     * Gets form method.
70
     *
71
     * @return string
72
     */
73 1
    public function getMethod() : string
74
    {
75 1
        return $this->method;
76
    }
77
78
    /**
79
     * Adds an element to the form.
80
     *
81
     * @param ElementInterface $formElement
82
     * @return ServiceInterface
83
     */
84 2
    public function addElement(ElementInterface $formElement) : ServiceInterface
85
    {
86 2
        $elementName = $formElement->getName();
87 2
        $elementName = $this->name.'['.$elementName.']';
88 2
        $formElement->setName($elementName);
89
90 2
        $this->formElements[$elementName] = $formElement;
91
92 2
        return $this;
93
    }
94
95
    /**
96
     * Returns all the elements assigned.
97
     *
98
     * @return array<ElementInterface>
99
     */
100 3
    public function getElements() : array
101
    {
102 3
        return $this->formElements;
103
    }
104
105
    /**
106
     * Loads data into the form.
107
     *
108
     * @param array $data
109
     * @return ServiceInterface
110
     */
111 1
    public function loadData(array $data) : ServiceInterface
112
    {
113 1
        $formData = $data[$this->name] ?? [];
114
115 1
        foreach ($formData as $elementName => $elementValue) {
116 1
            $fullName = $this->name.'['.$elementName.']';
117
            /** @var ElementInterface $formElement */
118 1
            $formElement = $this->formElements[$fullName] ?? null;
119
120 1
            if ($formElement) {
121 1
                if (!is_array($elementValue)) {
122 1
                    $elementValue = [$elementValue];
123
                }
124 1
                $formElement->setValues($elementValue);
125
            }
126
        }
127
128 1
        return $this;
129
    }
130
131
    /**
132
     * Defines the data which are presented during the json serialization.
133
     *
134
     * @return array
135
     */
136 1
    public function jsonSerialize() : array
137
    {
138
        $formData = [
139 1
            'name' => $this->name,
140 1
            'action' => $this->action,
141 1
            'method' => $this->method,
142
            'data' => [],
143
            'errors' => []
144
        ];
145
146
        /**
147
         * @var string $elementName
148
         * @var ElementInterface $formElement
149
         */
150 1
        foreach ($this->formElements as $formElement) {
151 1
            $formData['data'][$formElement->getId()] = $formElement->getValues();
152
153 1
            $errors = $formElement->getErrors();
154
155 1
            if (!empty($errors)) {
156 1
                $formData['errors'][$formElement->getId()] = $errors;
157
            }
158
        }
159
160 1
        return $formData;
161
    }
162
}
163