Issues (138)

src/Elements/AbstractElement.php (4 issues)

1
<?php
2
3
namespace Nip\Form\Elements;
4
5
use Nip\Form\AbstractForm;
6
use Nip\Form\Elements\Traits\HasAttributesTrait;
7
use Nip\Form\Elements\Traits\HasDecoratorsTrait;
8
use Nip\Form\Elements\Traits\HasErrorsTrait;
9
use Nip\Form\Elements\Traits\HasOptionsTrait;
10
use Nip\Form\Elements\Traits\HasRendererTrait;
11
use Nip\Form\Elements\Traits\HasTypeTrait;
12
use Nip\Form\Elements\Traits\HasUniqueIdTrait;
13
use Symfony\Component\Form\AbstractType;
14
15
/**
16
 * Class AbstractElement
17
 * @package Nip\Form\Elements
18
 */
19
abstract class AbstractElement extends AbstractType implements ElementInterface
20
{
21
    use HasUniqueIdTrait;
22
    use HasAttributesTrait;
23
    use HasOptionsTrait;
24
    use HasDecoratorsTrait;
25
    use HasRendererTrait;
26
    use HasTypeTrait;
0 ignored issues
show
The trait Nip\Form\Elements\Traits\HasTypeTrait requires the property $_type which is not provided by Nip\Form\Elements\AbstractElement.
Loading history...
27
    use HasErrorsTrait;
28
29
    protected $_form;
30
31
    protected $_isRequired;
32
    protected $_policies;
33
34
    /**
35
     * AbstractElement constructor.
36
     * @param null $form
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $form is correct as it would always require null to be passed?
Loading history...
37 16
     */
38
    public function __construct($form = null)
39 16
    {
40 16
        if ($form) {
0 ignored issues
show
$form is of type null, thus it always evaluated to false.
Loading history...
41
            $this->setForm($form);
42 16
        }
43 16
        $this->init();
44
    }
45 16
46
    public function init()
47 16
    {
48
    }
49
50
51
    /**
52
     * @return AbstractForm
53 12
     */
54
    public function getForm()
55 12
    {
56
        return $this->_form;
57
    }
58
59
    /**
60
     * @param AbstractForm $form
61
     * @return $this
62 16
     */
63
    public function setForm(AbstractForm $form)
64 16
    {
65
        $this->_form = $form;
66 16
67
        return $this;
68
    }
69
70
    /**
71
     * @param $data
72
     * @param string $source
73
     * @return \Nip\Form\Elements\AbstractElement
74
     */
75
    public function getData($data, $source = 'abstract')
76
    {
77
        if ($source == 'model') {
78
            return $this->getDataFromModel($data);
79
        }
80
81
        return $this->getDataFromRequest($data);
82
    }
83
84
    /**
85
     * @param $data
86
     * @return $this
87
     */
88
    public function getDataFromModel($data)
89
    {
90
        $this->setValue($data);
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param $request
97
     * @return $this
98
     */
99
    public function getDataFromRequest($request)
100
    {
101
        $request = $this->sanitizeDataFromRequest($request);
102
        $this->setValue($request);
103
104
        return $this;
105
    }
106
107
    /**
108
     * @param $request
109
     * @return string
110 5
     */
111
    protected function sanitizeDataFromRequest($request)
112 5
    {
113
        return clean($request);
114 5
    }
115
116
    /**
117
     * @param boolean $isRequired
118
     * @return $this
119
     */
120
    public function setRequired($isRequired)
121
    {
122
        $this->_isRequired = (bool)$isRequired;
123
124
        return $this;
125
    }
126
127
    public function validate()
128
    {
129
        if ($this->isRequired() && !$this->getValue()) {
130
            $message = $this->getForm()->getMessageTemplate('no-' . $this->getName());
0 ignored issues
show
Are you sure the usage of $this->getName() targeting Nip\Form\Elements\AbstractElement::getName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
131
            if (!$message) {
132
                $translateSlug = 'general.form.errors.required';
133
                $message = app('translator')->translate($translateSlug, ['label' => $this->getLabel()]);
134
                if ($message == $translateSlug) {
135 2
                    $message = $message ? $message : 'The field `' . $this->getLabel() . '` is mandatory.';
136
                }
137 2
            }
138
            $this->addError($message);
139
        }
140
    }
141
142
    /**
143 6
     * @return bool
144
     */
145 6
    public function isRequired()
146
    {
147
        return (bool)$this->_isRequired;
148
    }
149
150
    /**
151
     * @return bool
152
     */
153
    public function isGroup()
154
    {
155
        return false;
156
    }
157
158
    /**
159
     * @return bool
160
     */
161
    public function isRequestArray()
162
    {
163
        return false;
164
    }
165
}
166