Issues (138)

src/Traits/HasModelTrait.php (4 issues)

1
<?php
2
3
namespace Nip\Form\Traits;
4
5
use Nip\Records\AbstractModels\Record;
6
7
/**
8
 * Trait HasModelTrait
9
 * @package Nip\Form\Traits
10
 */
11
trait HasModelTrait
12
{
13
    /**
14
     * @var Record
15
     */
16
    protected $model;
17
18
    public function bind()
19
    {
20
21
    }
22
23
    public function initialized()
24
    {
25
        parent::initialized();
26
        $this->getDataFromModel();
27
    }
28
29
    /**
30
     * @param $name
31
     * @return $this
32
     */
33
    public function addModelError($name)
34
    {
35
        return $this->addError($this->getModelMessage($name));
0 ignored issues
show
It seems like addError() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

35
        return $this->/** @scrutinizer ignore-call */ addError($this->getModelMessage($name));
Loading history...
36
    }
37
38
    /**
39
     * @param $name
40
     * @param array $variables
41
     * @return mixed
42
     */
43
    public function getModelMessage($name, $variables = [])
44
    {
45
        return $this->getModelManager()->getMessage('form.' . $name, $variables);
46
    }
47
48
    /**
49
     * @return Record
50
     */
51
    public function getModel()
52
    {
53
        return $this->model;
54
    }
55
56
    /**
57
     * @param Record $model
58
     * @return $this
59
     */
60
    public function setModel(Record $model)
61
    {
62
        $this->model = $model;
63
        return $this;
64
    }
65
66
    protected function getDataFromModel()
67
    {
68
        $elements = $this->getElements();
0 ignored issues
show
It seems like getElements() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

68
        /** @scrutinizer ignore-call */ 
69
        $elements = $this->getElements();
Loading history...
69
        if (is_array($elements)) {
70
            foreach ($elements as $name => $element) {
71
                if (isset($this->getModel()->{$name})) {
72
                    $element->getData($this->getModel()->{$name}, 'model');
73
                }
74
            }
75
        }
76
    }
77
78
    /**
79
     * @param $input
80
     * @param $name
81
     * @param array $variables
82
     * @return $this
83
     */
84
    public function addInputModelError($input, $name, $variables = [])
85
    {
86
        return $this->getElement($input)->addError($this->getModelMessage($name, $variables));
0 ignored issues
show
It seems like getElement() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

86
        return $this->/** @scrutinizer ignore-call */ getElement($input)->addError($this->getModelMessage($name, $variables));
Loading history...
87
    }
88
89
    /**
90
     * @param $name
91
     * @return mixed
92
     */
93
    public function getModelLabel($name)
94
    {
95
        return $this->getModelManager()->getLabel($name);
96
    }
97
98
    /**
99
     * @return \Nip\Records\RecordManager
100
     */
101
    public function getModelManager()
102
    {
103
        return $this->getModel()->getManager();
104
    }
105
106
    public function process()
107
    {
108
        $this->saveToModel();
109
        $this->saveModel();
110
    }
111
112
    public function saveToModel()
113
    {
114
        $elements = $this->getElements();
115
        if (!is_array($elements)) {
116
            return;
117
        }
118
        foreach ($elements as $name => $element) {
119
            if ($element instanceof \Nip_Form_Element_File) {
120
                continue;
121
            }
122
            $this->getModel()->{$name} = $element->getValue('model');
123
        }
124
    }
125
126
    public function saveModel()
127
    {
128
        $this->getModel()->save();
129
    }
130
131
    protected function _addModelFormMessage($form, $model)
132
    {
133
        $this->_messageTemplates[$form] = $this->getModelMessage($model);
0 ignored issues
show
Bug Best Practice introduced by
The property _messageTemplates does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
134
135
        return $this;
136
    }
137
}
138