Passed
Push — master ( 158dd5...473203 )
by Radu
02:49
created

AbstractForm::isValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace WebServCo\Framework;
3
4
abstract class AbstractForm extends \WebServCo\Framework\AbstractLibrary
5
{
6
    protected $errors;
7
8
    protected $filtered;
9
10
    protected $submitFields;
11
12
    protected $submitField;
13
14
    protected $valid;
15
16
    use \WebServCo\Framework\Traits\ExposeLibrariesTrait;
17
18
    /**
19
     * @return bool
20
     */
21
    abstract protected function filter();
22
23
    /**
24
     * @return bool
25
     */
26
    abstract protected function validate();
27
28
    public function __construct($settings, $defaultData = [], $submitFields = [])
29
    {
30
        parent::__construct($settings);
31
32
        $this->submitFields = $submitFields;
33
34
        /**
35
         * Set form data
36
         */
37
        foreach ($this->setting('meta', []) as $field => $title) {
38
            if ($this->isSent()) {
39
                $data = $this->request()->data($field);
40
            } else {
41
                $data = \WebServCo\Framework\Utils\Arrays::get($defaultData, $field, null);
42
            }
43
            $this->setData($field, $data);
44
        }
45
46
        $this->errors = [];
47
48
        $this->filtered = $this->filter();
49
50
        if ($this->isSent()) {
51
            $this->valid = $this->validate();
52
        }
53
    }
54
55
    final public function errors($key, $defaultValue = false)
56
    {
57
        return \WebServCo\Framework\ArrayStorage::get(
58
            $this->errors,
59
            $key,
60
            $defaultValue
61
        );
62
    }
63
64
    final public function isSent()
65
    {
66
        if (!empty($this->submitFields)) {
67
            foreach ($this->submitFields as $field) {
68
                if (false !== $this->request()->data($field)) {
69
                    $this->submitField = $field;
70
                    return true;
71
                }
72
            }
73
            return false;
74
        }
75
        return $this->request()->getMethod() === \WebServCo\Framework\Http\Method::POST;
76
    }
77
78
    final public function getSubmitField()
79
    {
80
        if (!$this->isSent() || empty($this->submitFields)) {
81
            return false;
82
        }
83
        return $this->submitField;
84
    }
85
86
    final public function isValid()
87
    {
88
        return $this->valid;
89
    }
90
91
    final public function clear()
92
    {
93
        $this->data = [];
94
        $this->filtered = [];
95
        $this->errors = [];
96
    }
97
98
    final public function toArray()
99
    {
100
        return [
101
            'meta' => $this->setting('meta', []),
102
            'help' => $this->setting('help', []),
103
            'required' => array_fill_keys($this->setting('required', []), true),
104
            'data' => $this->data,
105
            'errors' => $this->errors,
106
        ];
107
    }
108
}
109