Passed
Push — main ( 575a83...1e5a49 )
by Garbuz
02:19
created

Field   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 96.88%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
eloc 33
c 3
b 0
f 0
dl 0
loc 81
ccs 31
cts 32
cp 0.9688
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 7 2
A __construct() 0 3 1
A findFieldClass() 0 13 5
A loadFieldFromArray() 0 24 4
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 42
    public function __construct(Configuration $config)
21
    {
22 42
        $this->config = $config;
23 42
    }
24
25
    /**
26
     * Find field class.
27
     *
28
     * @param string $method
29
     * @return bool|string
30
     */
31 42
    public function findFieldClass(string $method)
32
    {
33 42
        if (mb_strlen(trim($method)) == 0) {
34 1
            return false;
35
        }
36 41
        $fields = $this->config->getFields();
37 41
        if (isset($fields[$method])) {
38 39
            $class = $fields[$method];
39
        }
40 41
        if (isset($class) && class_exists($class)) {
41 39
            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 24
    public function __call(string $method, array $args): FieldInterface
55
    {
56 24
        if ($className = $this->findFieldClass($method)) {
57
            /** @scrutinizer ignore-call */
58 23
            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 16
    public function loadFieldFromArray(string $column, array $config): FieldInterface
70
    {
71 16
        $className = $this->findFieldClass($config['field']);
72
        /** @noinspection IsEmptyFunctionUsageInspection */
73 16
        if (!$className) {
74 1
            throw new Exception('Field ' . $column . ':' . $config['field'] . ' not found and not available for use');
75
        }
76 15
        $field = app($className)->init([$column, $config['label']]);
77 15
        $field->setLabel($config['label'] ?? '');
78 15
        $field->setPlaceholder($config['placeholder'] ?? '');
79
        $field->default($config['default'] ?? null);
80 15
        $field->index($config['index'] ?? false);
81 15
        $field->fillable($config['fillable'] ?? true);
82
        $field->hidden($config['hidden'] ?? false);
83 15
        $field->nullable($config['filter']['nullable'] ?? true);
84
        $field->nullable($config['filter']['unique'] ?? false);
85
        $field->required($config['filter']['required'] ?? false);
86 15
        $field->max($config['filter']['max'] ?? null);
87 15
        $field->min($config['filter']['min'] ?? null);
88
        $field->setMask($config['filter']['mask'] ?? null);
89 15
        if (isset($config['references']['table']) && isset($config['references']['field'])) {
90 15
            $field->references($config['references']['table'], $config['references']['field']);
91
        }
92 15
        return $field;
93 15
    }
94
}
95