Completed
Push — master ( daaff8...44ccfa )
by Ingo
22:24 queued 11:43
created

SingleSelectField   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 145
rs 10
wmc 19
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setHasEmptyDefault() 0 4 1
A getHasEmptyDefault() 0 3 1
A setEmptyString() 0 5 1
A getEmptyString() 0 3 1
A getSourceEmpty() 0 8 2
B validate() 0 30 5
A castedCopy() 0 7 3
A getSchemaStateDefaults() 0 11 2
A getDefaultValue() 0 13 3
1
<?php
2
3
/**
4
 * Represents the base class for a single-select field
5
 */
6
abstract class SingleSelectField extends SelectField {
7
8
	/**
9
	 * Show the first <option> element as empty (not having a value),
10
	 * with an optional label defined through {@link $emptyString}.
11
	 * By default, the <select> element will be rendered with the
12
	 * first option from {@link $source} selected.
13
	 *
14
	 * @var bool
15
	 */
16
	protected $hasEmptyDefault = false;
17
18
	/**
19
	 * The title shown for an empty default selection,
20
	 * e.g. "Select...".
21
	 *
22
	 * @var string
23
	 */
24
	protected $emptyString = '';
25
26
	protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_SINGLESELECT;
27
28
	public function getSchemaStateDefaults() {
29
		$data = parent::getSchemaStateDefaults();
30
31
		// Add options to 'data'
32
		$data['data']['hasEmptyDefault'] = $this->getHasEmptyDefault();
33
		$data['data']['emptyString'] = $this->getHasEmptyDefault() ? $this->getEmptyString() : null;
34
35
		$data['value'] = $this->getDefaultValue();
36
37
		return $data;
38
	}
39
40
	public function getDefaultValue() {
41
		$value = $this->Value();
42
		// assign value to field, such as first option available
43
		if ($value === null) {
44
			if ($this->getHasEmptyDefault()) {
45
				$value = '';
46
			} else {
47
				$values = $this->getValidValues();
48
				$value = array_shift($values);
49
			}
50
		}
51
		return $value;
52
	}
53
54
	/**
55
	 * @param boolean $bool
56
	 * @return self Self reference
57
	 */
58
	public function setHasEmptyDefault($bool) {
59
		$this->hasEmptyDefault = $bool;
60
		return $this;
61
	}
62
63
	/**
64
	 * @return bool
65
	 */
66
	public function getHasEmptyDefault() {
67
		return $this->hasEmptyDefault;
68
	}
69
70
	/**
71
	 * Set the default selection label, e.g. "select...".
72
	 * Defaults to an empty string. Automatically sets
73
	 * {@link $hasEmptyDefault} to true.
74
	 *
75
	 * @param string $string
76
	 * @return $this
77
	 */
78
	public function setEmptyString($string) {
79
		$this->setHasEmptyDefault(true);
80
		$this->emptyString = $string;
81
		return $this;
82
	}
83
84
	/**
85
	 * @return string
86
	 */
87
	public function getEmptyString() {
88
		return $this->emptyString;
89
	}
90
91
	/**
92
	 * Gets the source array, including the empty string, if present
93
	 *
94
	 * @return array|ArrayAccess
95
	 */
96
	public function getSourceEmpty() {
97
		// Inject default option
98
		if($this->getHasEmptyDefault()) {
99
			return array('' => $this->getEmptyString()) + $this->getSource();
100
		} else {
101
			return $this->getSource();
102
		}
103
	}
104
105
	/**
106
	 * Validate this field
107
	 *
108
	 * @param Validator $validator
109
	 * @return bool
110
	 */
111
	public function validate($validator) {
112
		// Check if valid value is given
113
		$selected = $this->Value();
114
		if(strlen($selected)) {
115
			// Use selection rules to check which are valid
116
			foreach($this->getValidValues() as $formValue) {
117
				if($this->isSelectedValue($formValue, $selected)) {
118
					return true;
119
				}
120
			}
121
		} else {
122
			if ($this->getHasEmptyDefault()) {
123
				// Check empty value
124
				return true;
125
			}
126
			$selected = '(none)';
127
		}
128
129
		// Fail
130
		$validator->validationError(
131
			$this->name,
132
			_t(
133
				'DropdownField.SOURCE_VALIDATION',
134
				"Please select a value within the list provided. {value} is not a valid option",
135
				array('value' => $selected)
136
			),
137
			"validation"
138
		);
139
		return false;
140
	}
141
142
	public function castedCopy($classOrCopy) {
143
		$field = parent::castedCopy($classOrCopy);
144
		if($field instanceof SingleSelectField && $this->getHasEmptyDefault()) {
145
			$field->setEmptyString($this->getEmptyString());
146
		}
147
		return $field;
148
	}
149
150
}
151