Passed
Push — master ( 16e851...25995c )
by Daniel
09:34
created

SingleLookupField::Value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\ORM\DataObjectInterface;
7
use SilverStripe\ORM\Map;
8
use SilverStripe\ORM\FieldType\DBField;
9
10
/**
11
 * Read-only complement of {@link DropdownField}.
12
 *
13
 * Shows the "human value" of the dropdown field for the currently selected
14
 * value.
15
 */
16
class SingleLookupField extends SingleSelectField
17
{
18
    /**
19
     * @var bool
20
     */
21
    protected $readonly = true;
22
23
    /**
24
     * @return mixed|null
25
     */
26
    protected function valueToLabel()
27
    {
28
        $value = $this->value;
29
        $source = $this->getSource();
30
        $source = ($source instanceof Map) ? $source->toArray() : $source;
31
32
        if (array_key_exists($value, $source)) {
0 ignored issues
show
Bug introduced by
It seems like $source can also be of type ArrayAccess; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        if (array_key_exists($value, /** @scrutinizer ignore-type */ $source)) {
Loading history...
33
            return $source[$value];
34
        }
35
36
        return null;
37
    }
38
39
    /**
40
     * Ignore validation as the field is readonly
41
     *
42
     * @param Validator $validator
43
     * @return bool
44
     */
45
    public function validate($validator)
46
    {
47
        return true;
48
    }
49
50
    /**
51
     * Stubbed so invalid data doesn't save into the DB
52
     *
53
     * @param DataObjectInterface $record DataObject to save data into
54
     */
55
    public function saveInto(DataObjectInterface $record)
56
    {
57
    }
58
59
    /**
60
     * @return SingleLookupField
61
     */
62
    public function performReadonlyTransformation()
63
    {
64
        $clone = clone $this;
65
66
        return $clone;
67
    }
68
69
    /**
70
     * @return bool
71
     */
72
    public function getHasEmptyDefault()
73
    {
74
        return false;
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function Type()
81
    {
82
        return 'single-lookup readonly';
83
    }
84
85
    /**
86
     * Note: we need to transform value in here becaue React fields do not use Field() to display value
87
     *
88
     * @return mixed
89
     */
90
    public function Value()
91
    {
92
        $label = $this->valueToLabel();
93
        if (!is_null($label)) {
94
            return $label;
95
        }
96
97
        return $value;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $value seems to be never defined.
Loading history...
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getTemplate()
104
    {
105
        // this field uses the same default template as LookupField
106
        return parent::getTemplate() ?: LookupField::class;
107
    }
108
109
    /**
110
     * Returns a readonly span containing the correct value.
111
     *
112
     * @param array $properties
113
     *
114
     * @return string
115
     */
116
    public function Field($properties = [])
117
    {
118
        $label = $this->valueToLabel();
119
        if (!is_null($label)) {
120
            $attrValue = Convert::raw2xml($label);
121
            $inputValue = $this->value;
122
        } else {
123
            $attrValue = '<i>(' . _t('SilverStripe\\Forms\\FormField.NONE', 'none') . ')</i>';
124
            $inputValue = '';
125
        }
126
127
        $properties = array_merge($properties, array(
128
            'AttrValue' => DBField::create_field('HTMLFragment', $attrValue),
129
            'InputValue' => $inputValue
130
        ));
131
132
        return parent::Field($properties);
133
    }
134
}
135