Passed
Pull Request — master (#49)
by Robbie
03:10
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
        $data['data']['matchTypeDefault'] = self::MATCH_TYPE_DEFAULT;
48
        return $data;
49
    }
50
51
    /**
52
     * Get a list of options for filtering with a human readable (translated) label
53
     *
54
     * @return array[]
55
     */
56
    public static function getMatchOptions()
57
    {
58
        return [
59
            ['value' => self::MATCH_TYPE_MUST, 'title' => _t(__CLASS__ . '.MUST_MATCH', 'Must match')],
60
            ['value' => self::MATCH_TYPE_MUST_NOT, 'title' => _t(__CLASS__ . '.MUST_NOT_MATCH', 'Must not match')],
61
        ];
62
    }
63
}
64