CaptchaForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license.
18
 */
19
namespace LearnZF2Captcha\Form;
20
21
use Zend\Form\Form;
22
use Zend\InputFilter\InputFilterProviderInterface;
23
24
/**
25
 * Form class that shows captchas.
26
 */
27
class CaptchaForm extends Form implements InputFilterProviderInterface
28
{
29
    /** @var array */
30
    private $captchaConfig;
31
32
    /** @var int captchaAdapterKey */
33
    private $captchaAdapterKey;
34
35
    /**
36
     * Constructor.
37
     *
38
     * Setup Captcha Config
39
     *
40
     * @param array $captchaConfig
41
     */
42
    public function __construct(array $captchaConfig, $captchaAdapterKey)
43
    {
44
        $this->captchaConfig = $captchaConfig;
45
        $this->captchaAdapterKey = $captchaAdapterKey;
46
47
        parent::__construct('Captcha Form');
48
    }
49
50
    /**
51
     * Collect Captcha options.
52
     *
53
     * @return array
54
     */
55
    private function collectValueOptions()
56
    {
57
        $options = [];
58
        foreach ($this->captchaConfig as $key => $config) {
59
            $options[$key] = $config['adapter_name'];
60
        }
61
62
        return $options;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function init()
69
    {
70
        $this->setAttribute('id', 'captchaForm');
71
        $this->setAttribute('method', 'get');
72
73
        $this->add([
74
            'type' => 'Zend\Form\Element\Select',
75
            'name' => 'captcha_adapter',
76
            'options' => [
77
                'label' => 'Choose Captcha Adapter',
78
                'value_options' => $this->collectValueOptions(),
79
            ],
80
            'attributes' => [
81
                'class' => 'form-control',
82
                'value' => $this->captchaAdapterKey,
83
            ],
84
        ]);
85
86
        $this->add([
87
            'type' => 'Zend\Form\Element\Captcha',
88
            'name' => 'captcha',
89
            'options' => [
90
                'label' => $this->captchaConfig[$this->captchaAdapterKey]['options']['label'],
91
                'captcha' => [
92
                    'class' => $this->collectValueOptions()[$this->captchaAdapterKey],
93
                    'options' => $this->captchaConfig[$this->captchaAdapterKey]['options'],
94
                ],
95
            ],
96
        ]);
97
98
        $this->add([
99
            'type' => 'Zend\Form\Element\Submit',
100
            'name' => 'submitCaptcha',
101
            'attributes' => [
102
                'value' => 'Submit',
103
                'class' => 'submit',
104
            ],
105
        ]);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getInputFilterSpecification()
112
    {
113
        return [
114
115
        ];
116
    }
117
}
118