Passed
Push — master ( 1f7f34...948e88 )
by Thomas
04:12 queued 02:03
created

SchemaFieldAbstract::setOptions()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 13
cp 0
rs 9.8333
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 20
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'];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 characters

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.

Loading history...
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);
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...
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