|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webfactor\Laravel\Generators\Contracts; |
|
4
|
|
|
|
|
5
|
|
|
use Webfactor\Laravel\Generators\Helper\RegexParser; |
|
6
|
|
|
use Webfactor\Laravel\Generators\Traits\CrudColumn; |
|
7
|
|
|
use Webfactor\Laravel\Generators\Traits\CrudField; |
|
8
|
|
|
use Webfactor\Laravel\Generators\Traits\MigrationField; |
|
9
|
|
|
use Webfactor\Laravel\Generators\Traits\ValidationRule; |
|
10
|
|
|
|
|
11
|
|
|
abstract class SchemaFieldAbstract implements SchemaFieldTypeInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use MigrationField, CrudColumn, CrudField, ValidationRule; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The name of the entity |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
public $name; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Keywords that can be used in schema string |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private $availableMethods = [ |
|
28
|
|
|
'field' => 'setCrudFieldOptions', |
|
29
|
|
|
'column' => 'setCrudColumnOptions', |
|
30
|
|
|
'rule' => 'setValidationRule', |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* SchemaFieldAbstract constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $fieldOptions |
|
37
|
|
|
* @param array $crudOptions |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(array $fieldOptions, array $crudOptions = []) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->name = $this->crudField['name'] = $this->crudColumn['name'] = $this->migrationField['name'] = $fieldOptions['name']; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
$this->setMigrationField($fieldOptions['options']); |
|
44
|
|
|
$this->parseCrudOptions($crudOptions); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Go through any additionally provided CRUD (field, column or rule |
|
49
|
|
|
* |
|
50
|
|
|
* @param array $crudOptions |
|
51
|
|
|
*/ |
|
52
|
|
|
private function parseCrudOptions(array $crudOptions): void |
|
53
|
|
|
{ |
|
54
|
|
|
foreach ($crudOptions as $crudOption) { |
|
55
|
|
|
$this->parseCrudOption($crudOption); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Parse the given string and call the corresponding method if exists |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $crudOption |
|
63
|
|
|
*/ |
|
64
|
|
|
private function parseCrudOption(string $crudOption) |
|
65
|
|
|
{ |
|
66
|
|
|
['left' => $left, 'inside' => $inside] = RegexParser::parseParenthesis($crudOption); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
if (key_exists($left, $this->availableMethods)) { |
|
69
|
|
|
call_user_func([$this, $this->availableMethods[$left]], $inside); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set options coming from inside the parenthesises |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $variableName |
|
77
|
|
|
* @param string $options |
|
78
|
|
|
*/ |
|
79
|
|
|
private function setOptions(string $variableName, string $options): void |
|
80
|
|
|
{ |
|
81
|
|
|
if ($options) { |
|
82
|
|
|
foreach (explode('|', $options) as $option) { |
|
83
|
|
|
if (str_contains($option, ':')) { |
|
84
|
|
|
$option = explode(':', $option); |
|
85
|
|
|
$this->{$variableName}[$option[0]] = $option[1]; |
|
86
|
|
|
} else { |
|
87
|
|
|
$this->{$variableName}[$option] = true; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.