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
|
|
|
/** |
19
|
|
|
* @param $name |
20
|
|
|
* @return $this |
21
|
|
|
*/ |
22
|
|
|
public function addModelError($name) |
23
|
|
|
{ |
24
|
|
|
return $this->addError($this->getModelMessage($name)); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param $name |
29
|
|
|
* @param array $variables |
30
|
|
|
* @return mixed |
31
|
|
|
*/ |
32
|
|
|
public function getModelMessage($name, $variables = []) |
33
|
|
|
{ |
34
|
|
|
return $this->getModelManager()->getMessage('form.'.$name, $variables); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return Record |
39
|
|
|
*/ |
40
|
|
|
public function getModel() |
41
|
|
|
{ |
42
|
|
|
return $this->model; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Record $model |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
public function setModel(Record $model) |
50
|
|
|
{ |
51
|
|
|
$this->model = $model; |
52
|
|
|
$this->getDataFromModel(); |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function getDataFromModel() |
58
|
|
|
{ |
59
|
|
|
$elements = $this->getElements(); |
|
|
|
|
60
|
|
|
if (is_array($elements)) { |
61
|
|
|
foreach ($elements as $name => $element) { |
62
|
|
|
if (isset($this->getModel()->{$name})) { |
63
|
|
|
$element->getData($this->getModel()->{$name}, 'model'); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $input |
71
|
|
|
* @param $name |
72
|
|
|
* @param array $variables |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
|
|
public function addInputModelError($input, $name, $variables = []) |
76
|
|
|
{ |
77
|
|
|
return $this->getElement($input)->addError($this->getModelMessage($name, $variables)); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $name |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
public function getModelLabel($name) |
85
|
|
|
{ |
86
|
|
|
return $this->getModelManager()->getLabel($name); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return \Nip\Records\RecordManager |
91
|
|
|
*/ |
92
|
|
|
public function getModelManager() |
93
|
|
|
{ |
94
|
|
|
return $this->getModel()->getManager(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function process() |
98
|
|
|
{ |
99
|
|
|
$this->saveToModel(); |
100
|
|
|
$this->saveModel(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function saveToModel() |
104
|
|
|
{ |
105
|
|
|
$elements = $this->getElements(); |
|
|
|
|
106
|
|
|
if (is_array($elements)) { |
107
|
|
|
foreach ($elements as $name => $element) { |
108
|
|
|
$this->getModel()->{$name} = $element->getValue('model'); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function saveModel() |
114
|
|
|
{ |
115
|
|
|
$this->getModel()->save(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function _addModelFormMessage($form, $model) |
119
|
|
|
{ |
120
|
|
|
$this->_messageTemplates[$form] = $this->getModelMessage($model); |
|
|
|
|
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.