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

ResultConditionsField   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Type() 0 3 1
A getMatchText() 0 4 2
A getMatchCondition() 0 4 2
A __construct() 0 5 1
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
    /**
44
     * Return the match condition
45
     *
46
     * @returns string
47
     */
48
    public function getMatchCondition()
49
    {
50
        $value = json_decode($this->Value(), true);
51
        return isset($value['match-select']) ? $value['match-select'] : self::MATCH_TYPE_DEFAULT;
52
    }
53
54
    /**
55
     * Return the condition's text to match against
56
     *
57
     * @return string
58
     */
59
    public function getMatchText()
60
    {
61
        $value = json_decode($this->Value(), true);
62
        return isset($value['match-text']) ? $value['match-text'] : '';
63
    }
64
}
65