Passed
Pull Request — 3.x (#71)
by Hari
06:16
created

Builder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A newField() 0 9 2
A newCollection() 0 9 2
A newFieldset() 0 9 2
1
<?php
2
/**
3
 * 
4
 * This file is part of the Aura project for PHP.
5
 * 
6
 * @package Aura.Input
7
 * 
8
 * @license http://opensource.org/licenses/MIT-license.php MIT
9
 * 
10
 */
11
namespace Aura\Input;
12
13
/**
14
 * 
15
 * A builder to create Field, Fieldset, and fieldset Collection objects.
16
 * 
17
 * @package Aura.Input
18
 * 
19
 */
20
class Builder implements BuilderInterface
21
{
22
    /**
23
     * 
24
     * The class to use for creating fieldset collections.
25
     * 
26
     * @var string
27
     * 
28
     */
29
    protected $collection_class = 'Aura\Input\Collection';
30
    
31
    /**
32
     * 
33
     * The class to use for creating fields.
34
     * 
35
     * @var string
36
     * 
37
     */
38
    protected $field_class = 'Aura\Input\Field';
39
    
40
    /**
41
     * 
42
     * A map of fieldset types to *callables that create objects* (as
43
     * vs class names).
44
     * 
45
     * @var array
46
     * 
47
     */
48
    protected $fieldset_map;
49
    
50
    /**
51
     * 
52
     * Constructor.
53
     * 
54
     * @param array $fieldset_map A map of fieldset types to *callables that
55
     * create objects* (as vs class names).
56
     * 
57
     */
58
    public function __construct(array $fieldset_map = [])
59
    {
60
        $this->fieldset_map = $fieldset_map;
61
    }
62
    
63
    /**
64
     * 
65
     * Creates a new Field object.
66
     * 
67
     * @param string $name The field name.
68
     * 
69
     * @param string $type The field type.
70
     * 
71
     * @return Field
72
     * 
73
     */
74
    public function newField($name, $type = null)
75
    {
76
        if (! $type) {
77
            $type = 'text';
78
        }
79
        $class = $this->field_class;
80
        $field = new $class($type);
81
        $field->setName($name);
82
        return $field;
83
    }
84
    
85
    /**
86
     * 
87
     * Creates a new Fieldset object.
88
     * 
89
     * @param string $name The fieldset name.
90
     * 
91
     * @param string $type The fieldset type.
92
     *
93
     * @param mixed $options Optional: configuration options for the fieldset.
94
     * 
95
     * @return Fieldset
96
     * 
97
     */
98
    public function newFieldset($name, $type = null, $options = null)
99
    {
100
        if (! $type) {
101
            $type = $name;
102
        }
103
        $factory = $this->fieldset_map[$type];
104
        $fieldset = $factory($options);
105
        $fieldset->setName($name);
106
        return $fieldset;
107
    }
108
    
109
    /**
110
     * 
111
     * Creates a new Collection object.
112
     * 
113
     * @param string $name The collection name.
114
     * 
115
     * @param string $type The collection type.
116
     * 
117
     * @return Collection
118
     * 
119
     */
120
    public function newCollection($name, $type = null)
121
    {
122
        if (! $type) {
123
            $type = $name;
124
        }
125
        $class = $this->collection_class;
126
        $collection = new $class($this->fieldset_map[$type]);
127
        $collection->setName($name);
128
        return $collection;
129
    }
130
}
131