|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Zebrainsteam\LaravelGeneratorPackage\Form; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Zebrainsteam\LaravelGeneratorPackage\Configuration; |
|
9
|
|
|
use Zebrainsteam\LaravelGeneratorPackage\Contracts\FieldInterface; |
|
10
|
|
|
use Zebrainsteam\LaravelGeneratorPackage\Exceptions\FieldDoesNotExistsException; |
|
11
|
|
|
|
|
12
|
|
|
class Field |
|
13
|
|
|
{ |
|
14
|
|
|
protected Configuration $config; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Form constructor. |
|
18
|
|
|
* @param Configuration $config |
|
19
|
|
|
*/ |
|
20
|
48 |
|
public function __construct(Configuration $config) |
|
21
|
|
|
{ |
|
22
|
48 |
|
$this->config = $config; |
|
23
|
48 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Find field class. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $method |
|
29
|
|
|
* @return bool|string |
|
30
|
|
|
*/ |
|
31
|
48 |
|
public function findFieldClass(string $method) |
|
32
|
|
|
{ |
|
33
|
48 |
|
if (mb_strlen(trim($method)) == 0) { |
|
34
|
1 |
|
return false; |
|
35
|
|
|
} |
|
36
|
47 |
|
$fields = $this->config->getFields(); |
|
37
|
47 |
|
if (isset($fields[$method])) { |
|
38
|
45 |
|
$class = $fields[$method]; |
|
39
|
|
|
} |
|
40
|
47 |
|
if (isset($class) && class_exists($class)) { |
|
41
|
45 |
|
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
|
28 |
|
public function __call(string $method, array $args): FieldInterface |
|
55
|
|
|
{ |
|
56
|
28 |
|
if ($className = $this->findFieldClass($method)) { |
|
57
|
|
|
/** @scrutinizer ignore-call */ |
|
58
|
27 |
|
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
|
18 |
|
public function loadFieldFromArray(string $column, array $config): FieldInterface |
|
70
|
|
|
{ |
|
71
|
18 |
|
$className = $this->findFieldClass($config['field']); |
|
72
|
|
|
/** @noinspection IsEmptyFunctionUsageInspection */ |
|
73
|
18 |
|
if (!$className) { |
|
|
|
|
|
|
74
|
1 |
|
throw new Exception('Field ' . $column . ':' . $config['field'] . ' not found and not available for use'); |
|
75
|
|
|
} |
|
76
|
17 |
|
$field = app($className)->init([$column, $config['label']]); |
|
77
|
17 |
|
$field->setLabel($config['label'] ?? ''); |
|
78
|
17 |
|
$field->setPlaceholder($config['placeholder'] ?? ''); |
|
79
|
17 |
|
$field->default($config['default'] ?? null); |
|
80
|
17 |
|
$field->index($config['index'] ?? false); |
|
81
|
17 |
|
$field->fillable($config['fillable'] ?? true); |
|
82
|
17 |
|
$field->hidden($config['hidden'] ?? false); |
|
83
|
17 |
|
$field->nullable($config['filter']['nullable'] ?? true); |
|
84
|
17 |
|
$field->unique($config['filter']['unique'] ?? false); |
|
85
|
17 |
|
$field->required($config['filter']['required'] ?? false); |
|
86
|
17 |
|
$field->max($config['filter']['max'] ?? null); |
|
87
|
17 |
|
$field->min($config['filter']['min'] ?? null); |
|
88
|
17 |
|
$field->setParam($config['param'] ?? null); |
|
89
|
17 |
|
$field->setMask($config['filter']['mask'] ?? null); |
|
90
|
|
|
if ( |
|
91
|
17 |
|
isset($config['references']['model']) |
|
92
|
17 |
|
&& isset($config['references']['table']) |
|
93
|
17 |
|
&& isset($config['references']['field']) |
|
94
|
|
|
) { |
|
95
|
1 |
|
$field->references( |
|
96
|
1 |
|
$config['references']['model'], |
|
97
|
1 |
|
$config['references']['table'], |
|
98
|
1 |
|
$config['references']['field'], |
|
99
|
1 |
|
$config['references']['has'] ?? 'hasMany', |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
17 |
|
return $field; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|