Passed
Branch main (e68017)
by James Ekow Abaka
10:11
created

FormHelper::data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ntentan\honam\engines\php\helpers;
4
5
use ntentan\honam\engines\php\Helper;
6
use ntentan\honam\engines\php\helpers\form\Container;
7
use ntentan\honam\engines\php\helpers\form\ElementFactory;
8
use ntentan\honam\exceptions\HonamException;
9
10
/**
11
 * A helper for rendering forms.
12
 */
13
class FormHelper extends Helper
14
{
15
16
    use ElementFactory;
17
18
    /**
19
     * An instance of the container for the Form.
20
     * @var array
21
     */
22
    private $containers = [];
23
24
    /**
25
     * An instance of the first container added to the form.
26
     * This is saved so that methods that need to access the first container can still work after the form is closed.
27
     */
28
    private $baseContainer;
29
30
    /**
31
     * Data to be rendered into the form.
32
     * @var array
33
     */
34
    private $data = [];
35
36
    /**
37
     * Any errors that should be rendered with the form.
38
     */
39
    private $errors = [];
40
41
    /**
42
     * Renders the form when the value is used as a string
43
     * @return string
44
     */
45
    public function __toString()
46
    {
47
        $this->baseContainer->setData($this->data);
48
        $this->baseContainer->setErrors($this->errors);
49
        $rendered = (string) $this->baseContainer;
50
        $this->baseContainer = null;
51
        return $rendered;
52
    }
53
54
    public function setId($id)
55
    {
56
        $this->getContainer()->setId($id);
0 ignored issues
show
Bug introduced by
The method getContainer() does not exist on ntentan\honam\engines\php\helpers\FormHelper. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        $this->/** @scrutinizer ignore-call */ 
57
               getContainer()->setId($id);
Loading history...
57
    }
58
59
    public function setErrors($errors)
60
    {
61
        $this->errors = $errors;
62
        return $this->container;
0 ignored issues
show
Bug introduced by
The property container does not exist on ntentan\honam\engines\php\helpers\FormHelper. Did you mean containers?
Loading history...
63
    }
64
65
    public function setData($data)
66
    {
67
        $this->data = $data;
68
        return $this->container;
0 ignored issues
show
Bug introduced by
The property container does not exist on ntentan\honam\engines\php\helpers\FormHelper. Did you mean containers?
Loading history...
69
    }
70
71
    public function open()
72
    {
73
        return $this->open_form();
0 ignored issues
show
Bug introduced by
The method open_form() does not exist on ntentan\honam\engines\php\helpers\FormHelper. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        return $this->/** @scrutinizer ignore-call */ open_form();
Loading history...
74
    }
75
76
    public function close($submit = 'Submit')
77
    {
78
        return $this->close_form($submit);
0 ignored issues
show
Bug introduced by
The method close_form() does not exist on ntentan\honam\engines\php\helpers\FormHelper. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        return $this->/** @scrutinizer ignore-call */ close_form($submit);
Loading history...
79
    }
80
81
    public function data(array $data)
82
    {
83
        $this->data = $data;
84
        return $this;
85
    }
86
87
    public function errors(array $errors)
88
    {
89
        $this->errors = $errors;
90
        return $this;
91
    }
92
93
    public function pushContainer(string $name, Container $container)
94
    {
95
        if($this->baseContainer === null && empty($this->containers)) {
96
            $this->baseContainer = $container;
97
        } else if ($this->baseContainer !== null && empty($this->containers)) {
98
            throw new HonamException("Cannot add a container to a form that has already been closed.");
99
        }
100
        array_push($this->containers, [$name, $container]);
101
    }
102
103
    public function popContainer(string $name)
104
    {
105
        $container = array_pop($this->containers);
106
        if($name === $container[0]) {
107
            return $container[1];
108
        } else {
109
            throw new HonamException("You cannot close the $name container while {$container[0]} is open.");
110
        }
111
    }
112
113
    public function getActiveContainer(): Container
114
    {
115
        return end($this->containers)[1];
116
    }
117
}
118