Schema::parseSchemaField()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Webfactor\Laravel\Generators\Schemas;
4
5
use Illuminate\Support\Collection;
6
use Webfactor\Laravel\Generators\Contracts\SchemaFieldTypeInterface;
7
use Webfactor\Laravel\Generators\Helper\RegexParser;
8
9
class Schema
10
{
11
    protected $structure = [];
12
13
    public function __construct(string $schema)
14
    {
15
        $schema = str_replace(', ', ',', $schema);
16
        $this->parseSchemaFields($this->getSchemaFieldsFromSchemaString($schema));
17
    }
18
19
    /**
20
     * @return Collection
21
     */
22
    public function getStructure(): Collection
23
    {
24
        return collect($this->structure);
25
    }
26
27
    private function getSchemaFieldsFromSchemaString(string $schema): array
28
    {
29
        return explode(',', $schema);
30
    }
31
32
    private function parseSchemaFields(array $schemaFields): void
33
    {
34
        foreach ($schemaFields as $schemaField) {
35
            $this->parseSchemaField($schemaField);
36
        }
37
    }
38
39
    private function parseSchemaField(string $schemaField): void
40
    {
41
        $schemaFieldParts = explode(';', $schemaField);
42
43
        $this->setSchemaField($this->parseMigration(array_shift($schemaFieldParts)), $schemaFieldParts);
44
    }
45
46
    private function parseMigration(string $migrationString): array
47
    {
48
        ['left' => $left, 'inside' => $inside] = RegexParser::parseParenthesis($migrationString);
0 ignored issues
show
Bug introduced by
The variable $left does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $inside does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
49
        [$name, $type] = explode(':', $left);
0 ignored issues
show
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $type does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
50
51
        return [
52
            'name' => $name,
53
            'type' => $type,
54
            'options' => $inside,
55
        ];
56
    }
57
58
    private function setSchemaField(array $migrationOptions, array $crudOptions): void
59
    {
60
        if ($schemaFieldType = $this->getSchemaFieldType($migrationOptions, $crudOptions)) {
61
            array_push($this->structure, $schemaFieldType);
62
        }
63
    }
64
65
    protected function getSchemaFieldType(array $migrationOptions, array $crudOptions): ?SchemaFieldTypeInterface
66
    {
67
        $typeClass = config('webfactor.generators.fieldTypes.'. $migrationOptions['type']);
68
69
        if (class_exists($typeClass)) {
70
            return $this->loadMigrationFieldType(new $typeClass($migrationOptions, $crudOptions));
71
        }
72
73
        return null;
74
    }
75
76
    private function loadMigrationFieldType(SchemaFieldTypeInterface $fieldType): SchemaFieldTypeInterface
77
    {
78
        return $fieldType;
79
    }
80
81
    public function getFieldNames()
82
    {
83
        return $this->getStructure()->pluck('name');
84
    }
85
86
    public function getMigrationFields()
87
    {
88
        return $this->getStructure()->pluck('migrationField');
89
    }
90
91
    public function getCrudFields()
92
    {
93
        return $this->getStructure()->pluck('crudField');
94
    }
95
96
    public function getCrudColumns()
97
    {
98
        return $this->getStructure()->pluck('crudColumn');
99
    }
100
101
    public function getValidationRules()
102
    {
103
        return $this->getStructure()->pluck('validationRule', 'name');
104
    }
105
}
106