Passed
Push — main ( 25ff95...174661 )
by Garbuz
13:35
created

Field::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
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 45
    public function __construct(Configuration $config)
21
    {
22 45
        $this->config = $config;
23 45
    }
24
25
    /**
26
     * Find field class.
27
     *
28
     * @param string $method
29
     * @return bool|string
30
     */
31 45
    public function findFieldClass(string $method)
32
    {
33 45
        if (mb_strlen(trim($method)) == 0) {
34 1
            return false;
35
        }
36 44
        $fields = $this->config->getFields();
37 44
        if (isset($fields[$method])) {
38 42
            $class = $fields[$method];
39
        }
40 44
        if (isset($class) && class_exists($class)) {
41 42
            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 26
    public function __call(string $method, array $args): FieldInterface
55
    {
56 26
        if ($className = $this->findFieldClass($method)) {
57
            /** @scrutinizer ignore-call */
58 25
            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 17
    public function loadFieldFromArray(string $column, array $config): FieldInterface
70
    {
71 17
        $className = $this->findFieldClass($config['field']);
72
        /** @noinspection IsEmptyFunctionUsageInspection */
73 17
        if (!$className) {
74 1
            throw new Exception('Field ' . $column . ':' . $config['field'] . ' not found and not available for use');
75
        }
76 16
        $field = app($className)->init([$column, $config['label']]);
77 16
        $field->setLabel($config['label'] ?? '');
78 16
        $field->setPlaceholder($config['placeholder'] ?? '');
79 16
        $field->default($config['default'] ?? null);
80 16
        $field->index($config['index'] ?? false);
81 16
        $field->fillable($config['fillable'] ?? true);
82 16
        $field->hidden($config['hidden'] ?? false);
83 16
        $field->nullable($config['filter']['nullable'] ?? true);
84 16
        $field->unique($config['filter']['unique'] ?? false);
85 16
        $field->required($config['filter']['required'] ?? false);
86 16
        $field->max($config['filter']['max'] ?? null);
87 16
        $field->min($config['filter']['min'] ?? null);
88 16
        $field->setMask($config['filter']['mask'] ?? null);
89
        if (
90 16
            isset($config['references']['model'])
91 16
            && isset($config['references']['table'])
92 16
            && isset($config['references']['field'])
93
        ) {
94
            $field->references(
95
                $config['references']['model'],
96
                $config['references']['table'],
97
                $config['references']['field'],
98
                $config['references']['has'] ?? 'hasMany',
99
            );
100
        }
101 16
        return $field;
102
    }
103
}
104