Completed
Push — master ( a68940...ed2b3e )
by Mikael
02:38
created

FormModel::getHTML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\HTMLForm;
4
5
use Anax\DI\DIInterface;
6
7
/**
8
 * Base class for form model classes.
9
 */
10
abstract class FormModel
11
{
12
    /**
13
     * @var Anax\DI\DIInterface $di   the DI/service container.
14
     * @var class               $form the main form class.
15
     */
16
    protected $di;
17
    protected $form;
18
19
20
21
    /**
22
     * Constructor injects with DI container.
23
     *
24
     * @param Anax\DI\DIInterface $di a service container
25
     */
26
    public function __construct(DIInterface $di)
27
    {
28
        $this->di = $di;
0 ignored issues
show
Documentation Bug introduced by
It seems like $di of type object<Anax\DI\DIInterface> is incompatible with the declared type object<Anax\HTMLForm\Anax\DI\DIInterface> of property $di.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
        $this->form = new Form($di);
30
    }
31
32
33
34
    /**
35
     * Customise the check() method to use own methods.
36
     *
37
     * @return boolean|null $callbackStatus if submitted&validates, false if
38
     *                                      not validate, null if not submitted.
39
     *                                      If submitted the callback function
40
     *                                      will return the actual value which
41
     *                                      should be true or false.
42
     */
43
    public function check()
44
    {
45
        return $this->form->check(
46
            [$this, "callbackSuccess"],
47
            [$this, "callbackFail"]
48
        );
49
    }
50
51
52
53
    /**
54
     * Return HTML for the form.
55
     *
56
     * @param array $options with options affecting the form output.
57
     *
58
     * @return string with HTML for the form.
59
     */
60
    public function getHTML($options = [])
61
    {
62
        return $this->form->getHTML($options);
63
    }
64
65
66
67
    /**
68
     * Callback what to do if the form was submitted successfully?
69
     */
70
    public function callbackSuccess()
71
    {
72
        $this->di->get("response")->redirectSelf();
73
    }
74
75
76
77
    /**
78
     * Callback what to do when submitted form could not be processed?
79
     */
80
    public function callbackFail()
81
    {
82
        $this->di->get("response")->redirectSelf();
83
    }
84
}
85