Passed
Push — master ( 4c5faf...429320 )
by Chris
04:23
created

FormFieldFactory::create()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.5021

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 16
ccs 6
cts 11
cp 0.5455
crap 5.5021
rs 9.9
1
<?php
2
3
namespace WebTheory\Saveyour\Factories;
4
5
use Exception;
6
use WebTheory\GuctilityBelt\Traits\ClassResolverTrait;
7
use WebTheory\GuctilityBelt\Traits\SmartFactoryTrait;
8
use WebTheory\Html\TagSage;
9
use WebTheory\Saveyour\Contracts\FormFieldInterface;
10
use WebTheory\Saveyour\Contracts\FormFieldResolverFactoryInterface;
11
use WebTheory\Saveyour\Fields\Input;
12
13
class FormFieldFactory implements FormFieldResolverFactoryInterface
14
{
15
    use SmartFactoryTrait;
0 ignored issues
show
Bug introduced by
The trait WebTheory\GuctilityBelt\Traits\SmartFactoryTrait requires the property $name which is not provided by WebTheory\Saveyour\Factories\FormFieldFactory.
Loading history...
16
    use ClassResolverTrait;
17
18
    /**
19
     *
20
     */
21
    private $fields = [];
22
23
    /**
24
     *
25
     */
26
    protected $namespaces = [];
27
28
    /**
29
     *
30
     */
31
    protected $rules = [];
32
33
    public const NAMESPACES = [
34
        'webtheory.saveyour' =>  "WebTheory\\Saveyour\\Fields"
35
    ];
36
37
    public const FIELDS = [];
38
39
    protected const CONVENTION = null;
40
41
    /**
42
     *
43
     */
44 9
    public function __construct(array $namespaces = [], array $fields = [])
45
    {
46 9
        $this->namespaces = $namespaces + static::NAMESPACES;
47 9
        $this->fields = $fields + static::FIELDS;
48 9
    }
49
50
    /**
51
     * Get the value of managers
52
     *
53
     * @return mixed
54
     */
55
    public function getFields()
56
    {
57
        return $this->fields;
58
    }
59
60
    /**
61
     * Set the value of managers
62
     *
63
     * @param mixed $managers
64
     *
65
     * @return self
66
     */
67
    public function addField(string $arg, string $field)
68
    {
69
        $this->fields[$arg] = $field;
70
71
        return $this;
72
    }
73
74
    /**
75
     *
76
     */
77
    public function addFields(array $fields)
78
    {
79
        $this->fields = $fields + $this->fields;
80
81
        return $this;
82
    }
83
84
    /**
85
     *
86
     */
87 9
    public function create(string $field, array $args = []): FormFieldInterface
88
    {
89 9
        $class = $this->getClass($field);
90
91 9
        if (isset($this->fields[$field])) {
92
            $field = $this->build($this->fields[$field], $args);
93 9
        } elseif (false !== $class) {
94 9
            $field = $this->build($class, $args);
95
        } elseif (TagSage::isIt('standard_input_type', $field)) {
96
            $args['type'] = $field;
97
            $field = $this->build(Input::class, $args);
98
        } else {
99
            throw new Exception("{$field} is not a recognized field type");
100
        }
101
102 9
        return $field;
103
    }
104
}
105