Passed
Pull Request — master (#49)
by Robbie
02:58
created

ResultConditionsField::getMatchOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\CKANRegistry\Forms;
4
5
use SilverStripe\Forms\Form;
6
use SilverStripe\Forms\TextField;
7
8
/**
9
 * A ResultConditionsField renders a list of "must have" and "must not have" text conditions which are then used
10
 * to determine whether a column is displayed to frontend users.
11
 */
12
class ResultConditionsField extends TextField
13
{
14
    /**
15
     * @var int
16
     */
17
    const MATCH_TYPE_MUST = 1;
18
19
    /**
20
     * @var int
21
     */
22
    const MATCH_TYPE_MUST_NOT = 0;
23
24
    /**
25
     * @var int
26
     */
27
    const MATCH_TYPE_DEFAULT = self::MATCH_TYPE_MUST;
28
29
    protected $schemaComponent = 'ResultConditions';
30
31
    public function __construct($name, $title = null, $value = '', $maxLength = null, Form $form = null)
32
    {
33
        parent::__construct($name, $title, $value, $maxLength, $form);
34
35
        $this->addExtraClass('ckan-result-conditions__container');
36
    }
37
38
    public function Type()
39
    {
40
        return 'ckan-result-conditions';
41
    }
42
43
    public function getSchemaDataDefaults()
44
    {
45
        $data = parent::getSchemaDataDefaults();
46
        $data['data']['source'] = static::getMatchOptions();
47
        return $data;
48
    }
49
50
    /**
51
     * Get a list of options for filtering with a human readable (translated) label
52
     *
53
     * @return array
54
     */
55
    public static function getMatchOptions()
56
    {
57
        return [
58
            ['value' => self::MATCH_TYPE_MUST, 'title' => _t(__CLASS__ . '.MUST_MATCH', 'Must match')],
59
            ['value' => self::MATCH_TYPE_MUST_NOT, 'title' => _t(__CLASS__ . '.MUST_NOT_MATCH', 'Must not match')],
60
        ];
61
    }
62
63
    /**
64
     * Return the match condition
65
     *
66
     * @returns string
67
     */
68
    public function getMatchCondition()
69
    {
70
        $value = json_decode($this->Value(), true);
71
        return isset($value['match-select']) ? $value['match-select'] : self::MATCH_TYPE_DEFAULT;
72
    }
73
74
    /**
75
     * Return the condition's text to match against
76
     *
77
     * @return string
78
     */
79
    public function getMatchText()
80
    {
81
        $value = json_decode($this->Value(), true);
82
        return isset($value['match-text']) ? $value['match-text'] : '';
83
    }
84
}
85