UserOptionSetField::getValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
use Broarm\EventTickets\UserOptionSetFieldGridFieldConfig;
3
4
/**
5
 * Class UserOptionSetField
6
 *
7
 * @author Bram de Leeuw
8
 * @package UserTextField
9
 *
10
 * @method \HasManyList Options()
11
 */
12
class UserOptionSetField extends Broarm\EventTickets\UserField
13
{
14
    /**
15
     * @var OptionsetField
16
     */
17
    protected $fieldType = 'OptionsetField';
18
19
    private static $has_many = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
20
        'Options' => 'Broarm\EventTickets\UserFieldOption'
21
    );
22
23
    public function getCMSFields()
24
    {
25
        $fields = parent::getCMSFields();
26
        if ($this->exists()) {
27
            $fields->addFieldToTab('Root.Main', GridField::create(
28
                'Options',
29
                _t('AttendeeExtraField.Options', 'Add field options'),
30
                $this->Options(),
31
                UserOptionSetFieldGridFieldConfig::create()
32
            ));
33
        }
34
35
        return $fields;
36
    }
37
38
    /**
39
     * @param string  $fieldName
40
     * @param null    $defaultValue
41
     * @param boolean $main check if the field is for the main attendee
42
     *
43
     * @return OptionsetField
44
     */
45
    public function createField($fieldName, $defaultValue = null, $main = false)
46
    {
47
        /** @var OptionsetField $field */
48
        $field = parent::createField($fieldName, $defaultValue, $main);
49
        $field->setSource($this->Options()->map()->toArray());
50
        $field->setValue($defaultValue);
51
        return $field;
52
    }
53
54
    /**
55
     * Get the value by set option
56
     *
57
     * @return mixed
58
     */
59
    public function getValue()
60
    {
61
        if ($option = $this->Options()->byID($this->getField('Value'))) {
62
            return $option->Title;
63
        }
64
65
        return null;
66
    }
67
}
68