1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GarbuzIvan\LaravelGeneratorPackage\Form; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use GarbuzIvan\LaravelGeneratorPackage\Configuration; |
9
|
|
|
use GarbuzIvan\LaravelGeneratorPackage\Contracts\FieldInterface; |
10
|
|
|
use GarbuzIvan\LaravelGeneratorPackage\Exceptions\FieldDoesNotExistsException; |
11
|
|
|
|
12
|
|
|
class Field |
13
|
|
|
{ |
14
|
|
|
protected Configuration $config; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Form constructor. |
18
|
|
|
* @param Configuration $config |
19
|
|
|
*/ |
20
|
42 |
|
public function __construct(Configuration $config) |
21
|
|
|
{ |
22
|
42 |
|
$this->config = $config; |
23
|
42 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Find field class. |
27
|
|
|
* |
28
|
|
|
* @param string $method |
29
|
|
|
* @return bool|string |
30
|
|
|
*/ |
31
|
42 |
|
public function findFieldClass(string $method) |
32
|
|
|
{ |
33
|
42 |
|
if (mb_strlen(trim($method)) == 0) { |
34
|
1 |
|
return false; |
35
|
|
|
} |
36
|
41 |
|
$fields = $this->config->getFields(); |
37
|
41 |
|
if (isset($fields[$method])) { |
38
|
39 |
|
$class = $fields[$method]; |
39
|
|
|
} |
40
|
41 |
|
if (isset($class) && class_exists($class)) { |
41
|
39 |
|
return $class; |
42
|
|
|
} |
43
|
3 |
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Generate a Field object and add to form builder if Field exists. |
48
|
|
|
* |
49
|
|
|
* @param string $method |
50
|
|
|
* @param $args |
51
|
|
|
* @return FieldInterface |
52
|
|
|
* @throws FieldDoesNotExistsException |
53
|
|
|
*/ |
54
|
24 |
|
public function __call(string $method, array $args): FieldInterface |
55
|
|
|
{ |
56
|
24 |
|
if ($className = $this->findFieldClass($method)) { |
57
|
|
|
/** @scrutinizer ignore-call */ |
58
|
23 |
|
return app($className)->init($args); |
59
|
|
|
} |
60
|
1 |
|
throw new FieldDoesNotExistsException(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $column |
65
|
|
|
* @param array $config |
66
|
|
|
* @return FieldInterface |
67
|
|
|
* @throws Exception |
68
|
|
|
*/ |
69
|
16 |
|
public function loadFieldFromArray(string $column, array $config): FieldInterface |
70
|
|
|
{ |
71
|
16 |
|
$className = $this->findFieldClass($config['field']); |
72
|
|
|
/** @noinspection IsEmptyFunctionUsageInspection */ |
73
|
16 |
|
if (!$className) { |
74
|
1 |
|
throw new Exception('Field ' . $column . ':' . $config['field'] . ' not found and not available for use'); |
75
|
|
|
} |
76
|
15 |
|
$field = app($className)->init([$column, $config['label']]); |
77
|
15 |
|
if (isset($config['label'])) { |
78
|
15 |
|
$field->setLabel($config['label']); |
79
|
|
|
} |
80
|
15 |
|
if (isset($config['placeholder'])) { |
81
|
15 |
|
$field->setPlaceholder($config['placeholder']); |
82
|
|
|
} |
83
|
15 |
|
if (isset($config['default'])) { |
84
|
|
|
$field->default($config['default']); |
85
|
|
|
} |
86
|
15 |
|
if (isset($config['index'])) { |
87
|
15 |
|
$field->index($config['index']); |
88
|
|
|
} |
89
|
15 |
|
if (isset($config['fillable'])) { |
90
|
15 |
|
$field->fillable($config['fillable']); |
91
|
|
|
} |
92
|
15 |
|
if (isset($config['hidden'])) { |
93
|
15 |
|
$field->hidden($config['hidden']); |
94
|
|
|
} |
95
|
15 |
|
if (isset($config['references']['table']) && isset($config['references']['field'])) { |
96
|
|
|
$field->references($config['references']['table'], $config['references']['field']); |
97
|
|
|
} |
98
|
15 |
|
if (isset($config['filter']['type'])) { |
99
|
15 |
|
$field->setType($config['filter']['type']); |
100
|
|
|
} |
101
|
15 |
|
if (isset($config['filter']['light'])) { |
102
|
15 |
|
$field->setLight($config['filter']['light']); |
103
|
|
|
} |
104
|
15 |
|
if (isset($config['filter']['light'])) { |
105
|
15 |
|
$field->setLight($config['filter']['light']); |
106
|
|
|
} |
107
|
15 |
|
if (isset($config['filter']['nullable'])) { |
108
|
15 |
|
$field->nullable($config['filter']['nullable']); |
109
|
|
|
} |
110
|
15 |
|
if (isset($config['filter']['unique'])) { |
111
|
15 |
|
$field->nullable($config['filter']['unique']); |
112
|
|
|
} |
113
|
15 |
|
if (isset($config['filter']['required'])) { |
114
|
15 |
|
$field->required($config['filter']['required']); |
115
|
|
|
} |
116
|
15 |
|
if (isset($config['filter']['max'])) { |
117
|
|
|
$field->max($config['filter']['max']); |
118
|
|
|
} |
119
|
15 |
|
if (isset($config['filter']['min'])) { |
120
|
|
|
$field->min($config['filter']['min']); |
121
|
|
|
} |
122
|
15 |
|
if (isset($config['filter']['mask'])) { |
123
|
|
|
$field->setMask($config['filter']['mask']); |
124
|
|
|
} |
125
|
15 |
|
return $field; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|