Passed
Branch master (249862)
by Adam
07:51
created

PastebinForm::buildForm()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 50
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 37
nc 3
nop 0
dl 0
loc 50
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Http\Forms;
4
5
use Coyote\Services\FormBuilder\Form;
6
use Coyote\Services\FormBuilder\ValidatesWhenSubmitted;
7
8
class PastebinForm extends Form implements ValidatesWhenSubmitted
9
{
10
    public function buildForm()
11
    {
12
        $this
13
            ->add('text', 'textarea', [
14
                'rules' => 'required|string',
15
                'theme' => self::THEME_INLINE,
16
                'attr' => [
17
                    'id' => 'code'
18
                ],
19
                'label_attr' => [
20
                    'style' => 'display: none'
21
                ]
22
            ])
23
            ->add('title', 'text', [
24
                'label' => 'Nazwa',
25
                'rules' => 'required|string|min:2|max:100',
26
                'help' => 'Nazwa, tytuł wpisu. Może to być po prostu Twój nick.'
27
            ])
28
            ->add('mode', 'select', [
29
                'choices' => $this->getModeList(),
30
                'label' => 'Kolorowanie składni',
31
                'empty_value' => '--',
32
                'rules' => 'sometimes|in:' . implode(',', array_keys($this->getModeList()))
33
            ])
34
            ->add('expires', 'select', [
35
                'choices' => $this->getExpiresList(),
36
                'label' => 'Wygaśnie',
37
                'empty_value' => 'Nigdy',
38
                'rules' => 'sometimes|in:' . implode(',', array_keys($this->getExpiresList())),
39
                'value' => 72,
40
                'help' => 'Po upływie tego czasu, ten wpis zostanie automatycznie usunięty.'
41
            ])
42
            ->add('submit', 'submit', [
43
                'label' => 'Zapisz',
44
                'attr' => [
45
                    'data-submit-state' => 'Zapisywanie...'
46
                ]
47
            ])
48
            ->add('human_email', 'honeypot');
49
50
        if (!empty($this->request->user())) {
51
            // user's login as default title
52
            $this->get('title')->setValue($this->request->user()->name);
53
54
            if (!empty($this->getData()->id) && $this->request->user()->can('pastebin-delete')) {
55
                $this->add('del', 'button', [
56
                    'label' => 'Usuń',
57
                    'attr' => [
58
                        'id' => 'btn-delete',
59
                        'class' => 'btn btn-danger'
60
                    ]
61
                ]);
62
            }
63
        }
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    private function getModeList()
70
    {
71
        return [
72
            'c_cpp' => 'C++',
73
            'csharp' => 'C#',
74
            'css' => 'CSS',
75
            'pascal' => 'Delphi',
76
            'diff' => 'Diff',
77
            'java' => 'Java',
78
            'jsx' => 'JavaFX',
79
            'javascript' => 'JavaScript',
80
            'perl' => 'Perl',
81
            'powershell' => 'PowerShell',
82
            'php' => 'PHP',
83
            'python' => 'Python',
84
            'ruby' => 'Ruby',
85
            'scala' => 'Scala',
86
            'sql' => 'SQL',
87
            'xml' => 'XML'
88
        ];
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    private function getExpiresList()
95
    {
96
        return [72 => '72 godz.', 48 => '48 godz.', 24 => '24 godz.', 1 => '1 godz.'];
97
    }
98
}
99