Repository navigation
Expand file tree
/
Copy pathModelFormModel.php
More file actions
93 lines (76 loc) · 2.05 KB
/
Copy pathModelFormModel.php
File metadata and controls
93 lines (76 loc) · 2.05 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
<?php namespace RainLab\Builder\Classes;
use SystemException;
use ValidationException;
/**
* Represents and manages model forms.
*
* @package rainlab\builder
* @author Alexey Bobkov, Samuel Georges
*/
class ModelFormModel extends ModelYamlModel
{
public $controls;
protected static $fillable = [
'fileName',
'controls'
];
protected $validationRules = [
'fileName' => ['required', 'regex:/^[a-z0-9\.\-_]+$/i']
];
public function loadForm($path)
{
$this->fileName = $path;
return parent::load($this->getFilePath());
}
public function fill(array $attributes)
{
if (!is_array($attributes['controls'])) {
$attributes['controls'] = json_decode($attributes['controls'], true);
if ($attributes['controls'] === null) {
throw new SystemException('Cannot decode controls JSON string.');
}
}
return parent::fill($attributes);
}
public static function validateFileIsModelType($fileContentsArray)
{
$modelRootNodes = [
'fields',
'tabs',
'secondaryTabs'
];
foreach ($modelRootNodes as $node) {
if (array_key_exists($node, $fileContentsArray)) {
return true;
}
}
return false;
}
public function validate()
{
parent::validate();
if (!$this->controls) {
throw new ValidationException(['controls' => 'Please create at least one field.']);
}
}
public function initDefaults()
{
$this->fileName = 'fields.yaml';
}
/**
* Converts the model's data to an array before it's saved to a YAML file.
* @return array
*/
protected function modelToYamlArray()
{
return $this->controls;
}
/**
* Load the model's data from an array.
* @param array $array An array to load the model fields from.
*/
protected function yamlArrayToModel($array)
{
$this->controls = $array;
}
}