Completed
Pull Request — master (#647)
by Robbie
02:01
created

EditableOption::set_allow_empty_values()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\UserForms\Model\EditableFormField;
4
5
use SilverStripe\CMS\Controllers\CMSMain;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Core\Convert;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\UserForms\Model\EditableFormField\EditableMultipleOptionField;
10
use SilverStripe\Versioned\Versioned;
11
12
/**
13
 * Base Class for EditableOption Fields such as the ones used in
14
 * dropdown fields and in radio check box groups
15
 *
16
 * @method EditableMultipleOptionField Parent()
17
 * @package userforms
18
 */
19
class EditableOption extends DataObject
20
{
21
    private static $default_sort = 'Sort';
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...
Unused Code introduced by
The property $default_sort is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
23
    private static $db = [
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...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
        'Name' => 'Varchar(255)',
25
        'Title' => 'Varchar(255)',
26
        'Default' => 'Boolean',
27
        'Sort' => 'Int',
28
        'Value' => 'Varchar(255)',
29
    ];
30
31
    private static $has_one = [
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...
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
32
        'Parent' => EditableMultipleOptionField::class,
33
    ];
34
35
    private static $extensions = [
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...
Unused Code introduced by
The property $extensions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
36
        Versioned::class . "('Stage', 'Live')"
37
    ];
38
39
    private static $summary_fields = [
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...
Unused Code introduced by
The property $summary_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
40
        'Title',
41
        'Default'
42
    ];
43
44
    protected static $allow_empty_values = false;
45
46
    private static $table_name = 'EditableOption';
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...
Unused Code introduced by
The property $table_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
47
48
    /**
49
     * Returns whether to allow empty values or not.
50
     *
51
     * @return boolean
52
     */
53
    public static function allow_empty_values()
54
    {
55
        return (bool) self::$allow_empty_values;
56
    }
57
58
    /**
59
     * Set whether to allow empty values.
60
     *
61
     * @param boolean $allow
62
     */
63
    public static function set_allow_empty_values($allow)
64
    {
65
        self::$allow_empty_values = (bool) $allow;
66
    }
67
68
    /**
69
     * @deprecated 5.0 Use "$Title.XML" in templates instead
70
     * @return string
71
     */
72
    public function getEscapedTitle()
73
    {
74
        return Convert::raw2att($this->Title);
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<SilverStripe\User...rmField\EditableOption>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
75
    }
76
77
    /**
78
     * Fetches a value for $this->Value. If empty values are not allowed,
79
     * then this will return the title in the case of an empty value.
80
     *
81
     * @return string
82
     */
83
    public function getValue()
84
    {
85
        $value = $this->getField('Value');
86
        if (empty($value) && !self::allow_empty_values()) {
87
            return $this->Title;
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<SilverStripe\User...rmField\EditableOption>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
88
        }
89
        return $value;
90
    }
91
92
    protected function onBeforeWrite()
93
    {
94
        if (!$this->Sort) {
0 ignored issues
show
Documentation introduced by
The property Sort does not exist on object<SilverStripe\User...rmField\EditableOption>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
95
            $this->Sort = EditableOption::get()->max('Sort') + 1;
0 ignored issues
show
Documentation introduced by
The property Sort does not exist on object<SilverStripe\User...rmField\EditableOption>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
96
        }
97
98
        parent::onBeforeWrite();
99
    }
100
}
101