Passed
Push — master ( 321c34...acac16 )
by Radu
01:43
created

AbstractForm::asArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
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 $valid;
11
    
12
    use \WebServCo\Framework\Traits\ExposeLibrariesTrait;
13
    
14
    public function __construct($settings, $defaultData = [])
15
    {
16
        parent::__construct($settings);
17
        
18
        /**
19
         * Set form data
20
         */
21
        foreach ($this->setting('meta', []) as $field => $title) {
22
            $this->setData(
23
                $field,
24
                $this->request()->data( // from POST
25
                    $field,
26
                    \WebServCo\Framework\Utils::isA($defaultData, $field) // default data
27
                )
28
            );
29
        }
30
        
31
        $this->errors = [];
32
        
33
        $this->filtered = $this->filter();
34
        
35
        if ($this->isSent()) {
36
            $this->valid = $this->validate();
37
        }
38
    }
39
    
40
    abstract protected function db();
41
    
42
    /**
43
     * @return bool
44
     */
45
    abstract protected function filter();
46
    
47
    /**
48
     * @return bool
49
     */
50
    abstract protected function validate();
51
    
52
    final public function isSent()
53
    {
54
        return $this->request()->getMethod() === \WebServCo\Framework\Http::METHOD_POST;
55
    }
56
    
57
    final public function isValid()
58
    {
59
        return $this->valid;
60
    }
61
    
62
    final public function toArray()
63
    {
64
        return [
65
            'meta' => $this->setting('meta', []),
66
            'data' => $this->data,
67
            'errors' => $this->errors,
68
        ];
69
    }
70
}
71