Passed
Push — main ( 2e28d4...91b310 )
by James Ekow Abaka
10:53
created

FormHelper::setErrors()   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
    /**
55
     * Set the ID of the form.
56
     * @return string
57
     */
58
    public function setId($id)
59
    {
60
        $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

60
        $this->/** @scrutinizer ignore-call */ 
61
               getContainer()->setId($id);
Loading history...
61
    }
62
63
    public function open()
64
    {
65
        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

65
        return $this->/** @scrutinizer ignore-call */ open_form();
Loading history...
66
    }
67
68
    public function close($submit = 'Submit')
69
    {
70
        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

70
        return $this->/** @scrutinizer ignore-call */ close_form($submit);
Loading history...
71
    }
72
73
    public function pushContainer(string $name, Container $container)
74
    {
75
        if ($this->baseContainer === null && empty($this->containers)) {
76
            // If a container is opened and there is no base container, we can assume its the base container.
77
            $this->baseContainer = $container;
78
        } else if ($this->baseContainer !== null && empty($this->containers)) {
79
            // If the container is stack is empty while we still have a base container, something is wrong.
80
            throw new HonamException("Cannot add a container to a form that has already been closed.");
81
        }
82
        array_push($this->containers, ['name' => $name, 'instance' => $container]);
83
    }
84
85
    public function popContainer(string $name)
86
    {
87
        $container = array_pop($this->containers);
88
        if ($name === $container['name']) {
89
            // Add this container to a parent if containers are nested in any way.
90
            $currentContainer = end($this->containers);
91
            if ($currentContainer === false) {
92
                $this->baseContainer = null;
93
            } else {
94
                $currentContainer['instance']->add($container['instance']);
95
            }
96
            return $container['instance'];
97
        } else {
98
            throw new HonamException("You cannot close the $name container while {$container['name']} is open.");
99
        }
100
    }
101
102
    public function getActiveContainer(): Container
103
    {
104
        return end($this->containers)['instance'];
105
    }
106
}
107