Repository navigation
Expand file tree
/
Copy pathBaseModel.php
More file actions
131 lines (106 loc) · 3.09 KB
/
Copy pathBaseModel.php
File metadata and controls
131 lines (106 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php namespace RainLab\Builder\Classes;
use ValidationException;
use SystemException;
use Validator;
/**
* Base class for Builder models.
*
* Builder models manage various types of records - database metadata objects,
* YAML files, etc.
*
* @package rainlab\builder
* @author Alexey Bobkov, Samuel Georges
*/
abstract class BaseModel
{
/**
* @var boolean This property is used by the system internally.
*/
public $exists = false;
protected $validationRules = [];
protected $validationMessages = [];
protected static $fillable = [];
protected $updatedData = [];
/**
* @var PluginCode The plugin code object the model is associated with.
*/
protected $pluginCodeObj = null;
public function fill(array $attributes)
{
$this->updatedData = [];
foreach ($attributes as $key => $value) {
if (!in_array($key, static::$fillable)) {
continue;
}
$methodName = 'set'.ucfirst($key);
if (method_exists($this, $methodName)) {
$this->$methodName($value);
}
else {
if (is_scalar($value) && strpos($value, ' ') !== false) {
$value = trim($value);
}
$this->$key = $value;
}
$this->updatedData[$key] = $value;
}
}
public function validate()
{
$existingData = [];
foreach (static::$fillable as $field) {
$existingData[$field] = $this->$field;
}
$validation = Validator::make(
array_merge($existingData, $this->updatedData),
$this->validationRules,
$this->validationMessages
);
if ($validation->fails()) {
throw new ValidationException($validation);
}
if (!$this->isNewModel()) {
$this->validateBeforeCreate();
}
}
public function isNewModel()
{
return $this->exists === false;
}
/**
* Sets a string code of a plugin the model is associated with
* @param string $code Specifies the plugin code
*/
public function setPluginCode($code)
{
$this->pluginCodeObj = new PluginCode($code);
}
/**
* Sets a code object of a plugin the model is associated with
* @param PluginCode $obj Specifies the plugin code object
*/
public function setPluginCodeObj($obj)
{
$this->pluginCodeObj = $obj;
}
protected function validateBeforeCreate()
{
}
public function getModelPluginName()
{
$pluginCodeObj = $this->getPluginCodeObj();
$pluginCode = $pluginCodeObj->toCode();
$vector = PluginVector::createFromPluginCode($pluginCode);
if ($vector) {
return $vector->getPluginName();
}
return null;
}
public function getPluginCodeObj()
{
if (!$this->pluginCodeObj) {
throw new SystemException(sprintf('The active plugin is not set in the %s object.', get_class($this)));
}
return $this->pluginCodeObj;
}
}