Passed
Push — master ( ab2dc9...db3995 )
by Gabriel
14:15 queued 10s
created

AbstractElement::sanitizeDataFromRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
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
14
/**
15
 * Class AbstractElement
16
 * @package Nip\Form\Elements
17
 */
18
abstract class AbstractElement implements ElementInterface
19
{
20
    use HasUniqueIdTrait;
21
    use HasAttributesTrait;
22
    use HasOptionsTrait;
23
    use HasDecoratorsTrait;
24
    use HasRendererTrait;
25
    use HasTypeTrait;
0 ignored issues
show
Bug introduced by
The trait Nip\Form\Elements\Traits\HasTypeTrait requires the property $_type which is not provided by Nip\Form\Elements\AbstractElement.
Loading history...
26
    use HasErrorsTrait;
27
28
    protected $_form;
29
30
    protected $_isRequired;
31
    protected $_policies;
32
33
    /**
34
     * AbstractElement constructor.
35
     * @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...
36
     */
37 16
    public function __construct($form = null)
38
    {
39 16
        if ($form) {
0 ignored issues
show
introduced by
$form is of type null, thus it always evaluated to false.
Loading history...
40 16
            $this->setForm($form);
41
        }
42 16
        $this->init();
43 16
    }
44
45 16
    public function init()
46
    {
47 16
    }
48
49
50
    /**
51
     * @return AbstractForm
52
     */
53 12
    public function getForm()
54
    {
55 12
        return $this->_form;
56
    }
57
58
    /**
59
     * @param AbstractForm $form
60
     * @return $this
61
     */
62 16
    public function setForm(AbstractForm $form)
63
    {
64 16
        $this->_form = $form;
65
66 16
        return $this;
67
    }
68
69
    /**
70
     * @param $data
71
     * @param string $source
72
     * @return \Nip\Form\Elements\AbstractElement
73
     */
74
    public function getData($data, $source = 'abstract')
75
    {
76
        if ($source == 'model') {
77
            return $this->getDataFromModel($data);
78
        }
79
80
        return $this->getDataFromRequest($data);
81
    }
82
83
    /**
84
     * @param $data
85
     * @return $this
86
     */
87
    public function getDataFromModel($data)
88
    {
89
        $this->setValue($data);
90
91
        return $this;
92
    }
93
94
    /**
95
     * @param $request
96
     * @return $this
97
     */
98
    public function getDataFromRequest($request)
99
    {
100
        $request = $this->sanitizeDataFromRequest($request);
101
        $this->setValue($request);
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param $request
108
     * @return string
109
     */
110 5
    protected function sanitizeDataFromRequest($request)
111
    {
112 5
        return clean($request);
113
    }
114 5
115
    /**
116
     * @param boolean $isRequired
117
     * @return $this
118
     */
119
    public function setRequired($isRequired)
120
    {
121
        $this->_isRequired = (bool)$isRequired;
122
123
        return $this;
124
    }
125
126
    public function validate()
127
    {
128
        if ($this->isRequired() && !$this->getValue()) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getValue() targeting Nip\Form\Elements\AbstractElement::getValue() 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...
129
            $message = $this->getForm()->getMessageTemplate('no-' . $this->getName());
0 ignored issues
show
Bug introduced by
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...
130
            if (!$message) {
131
                $translateSlug = 'general.form.errors.required';
132
                $message = app('translator')->translate($translateSlug, ['label' => $this->getLabel()]);
133
                if ($message == $translateSlug) {
134
                    $message = $message ? $message : 'The field `' . $this->getLabel() . '` is mandatory.';
135 2
                }
136
            }
137 2
            $this->addError($message);
138
        }
139
    }
140
141
    /**
142
     * @return bool
143 6
     */
144
    public function isRequired()
145 6
    {
146
        return (bool)$this->_isRequired;
147
    }
148
149
    /**
150
     * @return bool
151
     */
152
    public function isGroup()
153
    {
154
        return false;
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    public function isRequestArray()
161
    {
162
        return false;
163
    }
164
}
165