Passed
Push — main ( bc4cf3...4d84a2 )
by Garbuz
02:27
created

Field::findFieldClass()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 8
c 3
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.6111
cc 5
nc 5
nop 1
crap 5
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 30
    public function __construct(Configuration $config)
21
    {
22 30
        $this->config = $config;
23 30
    }
24
25
    /**
26
     * Find field class.
27
     *
28
     * @param string $method
29
     * @return bool|string
30
     */
31 30
    public function findFieldClass(string $method)
32
    {
33 30
        if (mb_strlen(trim($method)) == 0) {
34 1
            return false;
35
        }
36 29
        $fields = $this->config->getFields();
37 29
        if (isset($fields[$method])) {
38 27
            $class = $fields[$method];
39
        }
40 29
        if (isset($class) && class_exists($class)) {
41 27
            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 22
    public function __call(string $method, array $args): FieldInterface
55
    {
56 22
        if ($className = $this->findFieldClass($method)) {
57
            /** @scrutinizer ignore-call */
58 21
            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 6
    public function loadFieldFromArray(string $column, array $config): FieldInterface
70
    {
71 6
        $className = $this->findFieldClass($config['field']);
72
        /** @noinspection IsEmptyFunctionUsageInspection */
73 6
        if (!$className) {
1 ignored issue
show
introduced by
The condition $className is always false.
Loading history...
74 1
            throw new Exception('Field ' . $column . ':' . $config['field'] . ' not found and not available for use');
75
        }
76 5
        $field = app($className)->init([$column, $config['label']]);
77 5
        if (isset($config['placeholder'])) {
78 5
            $field->setPlaceholder($config['placeholder']);
79
        }
80 5
        if (isset($config['default'])) {
81
            $field->default($config['default']);
82
        }
83 5
        if (isset($config['index'])) {
84 5
            $field->index($config['index']);
85
        }
86 5
        if (isset($config['fillable'])) {
87 5
            $field->fillable($config['fillable']);
88
        }
89 5
        if (isset($config['hidden'])) {
90 5
            $field->hidden($config['hidden']);
91
        }
92 5
        if (isset($config['references']['table']) && isset($config['references']['field'])) {
93
            $field->references($config['references']['table'], $config['references']['field']);
94
        }
95 5
        if (isset($config['filter']['type'])) {
96 5
            $field->setType($config['filter']['type']);
97
        }
98 5
        if (isset($config['filter']['light'])) {
99 5
            $field->setLight($config['filter']['light']);
100
        }
101 5
        if (isset($config['filter']['light'])) {
102 5
            $field->setLight($config['filter']['light']);
103
        }
104 5
        if (isset($config['filter']['nullable'])) {
105 5
            $field->nullable($config['filter']['nullable']);
106
        }
107 5
        if (isset($config['filter']['unique'])) {
108 5
            $field->nullable($config['filter']['unique']);
109
        }
110 5
        if (isset($config['filter']['required'])) {
111 5
            $field->required($config['filter']['required']);
112
        }
113 5
        if (isset($config['filter']['max'])) {
114
            $field->max($config['filter']['max']);
115
        }
116 5
        if (isset($config['filter']['min'])) {
117
            $field->min($config['filter']['min']);
118
        }
119 5
        if (isset($config['filter']['mask'])) {
120
            $field->setMask($config['filter']['mask']);
121
        }
122 5
        return $field;
123
    }
124
}
125