RadioValidator::allowAny()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace fieldwork\validators;
4
5
class RadioValidator extends FieldValidator
6
{
7
8
    private $value;
9
10
    const ANY = null;
11
12
    /**
13
     * @param string $errorMsg The message to show if the field does not pass validation
14
     * @param string $value    The value to allow
15
     */
16
    public function __construct ($errorMsg, $value = self::ANY)
17
    {
18
        parent::__construct($errorMsg);
19
        $this->value = $value;
20
    }
21
22
    public function getJsonData ()
23
    {
24
        return array_merge(parent::getJsonData(), array(
25
            'any'   => $this->allowAny(),
26
            'value' => $this->value
27
        ));
28
    }
29
30
    public function isValid ($value)
31
    {
32
        return $this->allowAny() ? (!empty($value)) : $value === $this->value;
33
    }
34
35
    public function describeObject ()
36
    {
37
        return 'radio';
38
    }
39
40
    /**
41
     * Whether the validator returns true as long as any value is checked
42
     * @return bool
43
     */
44
    private function allowAny ()
45
    {
46
        return $this->value === self::ANY;
47
    }
48
49
}
50