GroupField::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Form\Former\Fields;
13
14
use Former\Traits\Field;
15
16
/**
17
 * GroupField is a Former field class to generate a group of fields as one field.
18
 *
19
 * @author Mohamed Alsharaf <[email protected]>
20
 */
21
class GroupField extends Field
22
{
23
    /**
24
     * A list of class properties to be added to attributes.
25
     *
26
     * @var array
27
     */
28
    protected $injectedProperties = [];
29
30
    /**
31
     * The field's default element.
32
     *
33
     * @var string
34
     */
35
    protected $element = 'div';
36
37
    /**
38
     * List of managed fields.
39
     *
40
     * @var array
41
     */
42
    protected $fields = [];
43
44
    /**
45
     * Set managed fields.
46
     *
47
     * @param array $fields
48
     *
49
     * @return $this
50
     */
51 8
    public function fields(array $fields)
52
    {
53
        array_walk($fields, function (&$field, $name) {
54 8
            $field = \Form::element($this->name . '[' . $name . ']', $field);
55 8
            $field->setAttribute('id', $this->name . '-' . $name);
56 8
        });
57 8
        $this->fields = $fields;
58
59 8
        return $this;
60
    }
61
62
    /**
63
     * Render the field.
64
     *
65
     * @return string
66
     */
67 8
    public function render()
68
    {
69 8
        $this->addClass('group-fields');
70 8
        $this->setId();
71 8
        $this->removeAttribute('value');
72
73 8
        return $this->open() . $this->getContent() . $this->close();
74
    }
75
76
    /**
77
     * Render the field content. Rendering the managed fields.
78
     *
79
     * @return string
80
     */
81 8
    public function getContent()
82
    {
83 8
        $currentLabelWidths = \Former::getOption('TwitterBootstrap3.labelWidths');
84 8
        \Former::setOption('TwitterBootstrap3.labelWidths', [
85 8
            'large' => 0,
86
        ]);
87
88 8
        $output = '';
89 8
        foreach ($this->fields as $field) {
90 8
            $output .= $field->__toString();
91
        }
92
93 8
        \Former::setOption('TwitterBootstrap3.labelWidths', $currentLabelWidths);
94
95 8
        return $output;
96
    }
97
98
    /**
99
     * Returns values stored in managed fields.
100
     *
101
     * @return array
102
     */
103
    public function getValue()
104
    {
105
        if (!is_array($this->fields)) {
106
            return [];
107
        }
108
109
        return array_map(function (Field $field) {
110
            return $field->getValue();
111
        }, $this->fields);
112
    }
113
114
    /**
115
     * Set the matching ID on a field if possible
116
     * Override to prefix the id with group-.
117
     *
118
     * @param string $name
119
     *
120
     * @return string
121
     */
122 8
    protected function getUniqueId($name)
123
    {
124 8
        return 'group-' . parent::getUniqueId($name);
125
    }
126
}
127