Completed
Push — master ( 442463...d4b8b4 )
by Kristijan
18:40 queued 17:26
created

RepeatedType   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 10
c 0
b 0
f 0
ccs 17
cts 19
cp 0.8947
wmc 5
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTemplate() 0 4 1
A getDefaults() 0 9 1
A getAllAttributes() 0 5 1
A createChildren() 0 19 2
1
<?php
2
3
namespace Kris\LaravelFormBuilder\Fields;
4
5
class RepeatedType extends ParentType
6
{
7
8
    /**
9
     * Get the template, can be config variable or view path.
10
     *
11
     * @return string
12
     */
13 2
    protected function getTemplate()
14
    {
15 2
        return 'repeated';
16
    }
17
18
    /**
19
     * @inheritdoc
20
     */
21 2
    protected function getDefaults()
22
    {
23
        return [
24 2
            'type' => 'password',
25
            'second_name' => null,
26
            'first_options' => ['label' => 'Password'],
27
            'second_options' => ['label' => 'Password confirmation']
28
        ];
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function getAllAttributes()
35
    {
36
        // Collect all children's attributes.
37
        return $this->parent->getFormHelper()->mergeAttributes($this->children);
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 2
    protected function createChildren()
44
    {
45 2
        $firstName = $this->getRealName();
46 2
        $secondName = $this->getOption('second_name');
47
48 2
        if (is_null($secondName)) {
49 2
            $secondName = $firstName.'_confirmation';
50
        }
51
52 2
        $form = $this->parent->getFormBuilder()->plain([
53 2
            'name' => $this->parent->getName(),
54 2
            'model' => $this->parent->getModel()
55
        ])
56 2
        ->add($firstName, $this->getOption('type'), $this->getOption('first_options'))
57 2
        ->add($secondName, $this->getOption('type'), $this->getOption('second_options'));
58
59 2
        $this->children['first'] = $form->getField($firstName);
60 2
        $this->children['second'] = $form->getField($secondName);
61 2
    }
62
}
63