Issues (61)

src/WebServCo/Framework/AbstractForm.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework;
6
7
abstract class AbstractForm extends \WebServCo\Framework\AbstractLibrary
8
{
9
    use \WebServCo\Framework\Traits\ExposeLibrariesTrait;
10
11
    /**
12
     * Errors.
13
     *
14
     * @var array<string,array<int,string>>
15
     */
16
    protected array $errors;
17
18
    protected bool $filtered;
19
20
    /**
21
     * Submit fields.
22
     *
23
     * @var array<int,string>
24
     */
25
    protected array $submitFields;
26
27
    protected string $submitField;
28
29
    protected bool $valid;
30
31
    abstract public function validate(): bool;
32
33
    abstract protected function filter(): bool;
34
35
    /**
36
    * @param array<string,string|array<mixed>> $settings
37
    * @param array<string,bool|int|float|string|null> $defaultData
38
    * @param array<int,string> $submitFields
39
    */
40
    public function __construct(array $settings, array $defaultData = [], array $submitFields = [])
41
    {
42
        parent::__construct($settings);
43
44
        $this->submitFields = $submitFields;
45
46
        /**
47
         * Set form data
48
         */
49
        foreach ($this->setting('meta', []) as $field => $title) {
50
            $data = $this->isSent()
51
                ? $this->request()->data($field, null)
52
                : \WebServCo\Framework\Helpers\ArrayHelper::get($defaultData, $field, null);
53
            $this->setData($field, $data);
54
        }
55
56
        $this->errors = [];
57
58
        $this->filtered = $this->filter();
59
60
        $this->valid = false;
61
62
        if (!$this->isSent()) {
63
            return;
64
        }
65
66
        $this->valid = $this->validate();
67
    }
68
69
    final public function clear(): bool
70
    {
71
        $this->clearData();
72
        $this->filtered = false;
73
        $this->errors = [];
74
        return true;
75
    }
76
77
    /**
78
     * @param mixed $key
79
     * @param mixed $defaultValue
80
     * @return mixed
81
     */
82
    final public function errors($key, $defaultValue = null)
83
    {
84
        return \WebServCo\Framework\ArrayStorage::get($this->errors, $key, $defaultValue);
85
    }
86
87
    /**
88
     * Get all errors.
89
     *
90
     * @return array<string,array<int,string>>
91
     */
92
    final public function getAllErrors(): array
93
    {
94
        return $this->errors;
95
    }
96
97
    /**
98
    * @return mixed
99
    */
100
    final public function getSubmitField()
101
    {
102
        if (!$this->isSent() || !$this->submitFields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->submitFields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
103
            return false;
104
        }
105
        return $this->submitField;
106
    }
107
108
    /**
109
     * @param mixed $key
110
     * @param mixed $defaultValue
111
     * @return mixed
112
     */
113
    final public function help($key, $defaultValue = null)
114
    {
115
        return $this->setting(
116
            \sprintf('help/%s', $key),
117
            $defaultValue,
118
        );
119
    }
120
121
    final public function isSent(): bool
122
    {
123
        if ($this->submitFields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->submitFields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
124
            foreach ($this->submitFields as $field) {
125
                if (null !== $this->request()->data($field)) {
126
                    $this->submitField = $field;
127
                    return true;
128
                }
129
            }
130
            return false;
131
        }
132
        return \WebServCo\Framework\Http\Method::POST === $this->request()->getMethod();
133
    }
134
135
    final public function isValid(): bool
136
    {
137
        return $this->valid;
138
    }
139
140
    /**
141
     * @param mixed $key
142
     * @param mixed $defaultValue
143
     * @return mixed
144
     */
145
    final public function meta($key, $defaultValue = null)
146
    {
147
        return $this->setting(
148
            \sprintf('meta/%s', $key),
149
            $defaultValue,
150
        );
151
    }
152
153
    /**
154
     * @param mixed $key
155
     */
156
    final public function required($key): bool
157
    {
158
        // Retrieve list of required fields
159
        $required = $this->setting('required', []);
160
        // If exists, it's required.
161
        return \in_array($key, $required, true);
162
    }
163
164
    /**
165
    * @return array<string, array<mixed>>
166
    */
167
    final public function toArray(): array
168
    {
169
        return [
170
            'custom' => $this->setting('custom', []),
171
            'data' => $this->getData(),
172
            'errors' => $this->errors,
173
            'help' => $this->setting('help', []),
174
            'meta' => $this->setting('meta', []),
175
            'required' => \array_fill_keys($this->setting('required', []), true),
176
        ];
177
    }
178
}
179