Completed
Push — master ( 5776a0...605463 )
by Daniel
23s
created

NumericField_Readonly   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A performReadonlyTransformation() 0 3 1
A Value() 0 3 2
A getValueCast() 0 3 1
1
<?php
2
3
use SilverStripe\ORM\DataObject;
4
5
/**
6
 * Text input field with validation for numeric values. Supports validating
7
 * the numeric value as to the {@link i18n::get_locale()} value, or an
8
 * overridden locale specific to this field.
9
 *
10
 * @package forms
11
 * @subpackage fields-formattedinput
12
 */
13
class NumericField extends TextField {
14
15
	protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_DECIMAL;
16
17
	/**
18
	 * Override locale for this field.
19
	 *
20
	 * @var string
21
	 */
22
	protected $locale = null;
23
24
	/**
25
	 * @param mixed $value
26
	 * @param array $data
27
	 *
28
	 * @return $this
29
	 *
30
	 * @throws Zend_Locale_Exception
31
	 */
32
	public function setValue($value, $data = array()) {
33
		require_once "Zend/Locale/Format.php";
34
35
		// If passing in a non-string number, or a value
36
		// directly from a DataObject then localise this number
37
38
		if(is_int($value) || is_float($value) || $data instanceof DataObject) {
39
			$locale = new Zend_Locale($this->getLocale());
40
41
			$this->value = Zend_Locale_Format::toNumber(
42
				$value,
43
				array('locale' => $locale)
44
			);
45
		} else {
46
			$this->value = $this->clean($value);
47
		}
48
49
		return $this;
50
	}
51
52
	/**
53
	 * In some cases and locales, validation expects non-breaking spaces.
54
	 *
55
	 * Returns the value, with all spaces replaced with non-breaking spaces.
56
	 *
57
	 * @param string $input
58
	 *
59
	 * @return string
60
	 */
61
	protected function clean($input) {
62
		$replacement = html_entity_decode('&nbsp;', null, 'UTF-8');
63
64
		return str_replace(' ', $replacement, trim($input));
65
	}
66
67
	/**
68
	 * Determine if the current value is a valid number in the current locale.
69
	 *
70
	 * @return bool
71
	 */
72
	protected function isNumeric() {
73
		require_once "Zend/Locale/Format.php";
74
75
		$locale = new Zend_Locale($this->getLocale());
76
77
		return Zend_Locale_Format::isNumber(
78
			$this->clean($this->value),
79
			array('locale' => $locale)
80
		);
81
	}
82
83
	/**
84
	 * {@inheritdoc}
85
	 */
86
	public function Type() {
87
		return 'numeric text';
88
	}
89
90
	/**
91
	 * Validate this field
92
	 *
93
	 * @param Validator $validator
94
	 * @return bool
95
	 */
96
	public function validate($validator) {
97
		if(!$this->value) {
98
			return true;
99
		}
100
101
		if($this->isNumeric()) {
102
			return true;
103
		}
104
105
		$validator->validationError(
106
			$this->name,
107
			_t(
108
				'NumericField.VALIDATION',
109
				"'{value}' is not a number, only numbers can be accepted for this field",
110
				array('value' => $this->value)
111
			),
112
			"validation"
113
		);
114
115
		return false;
116
	}
117
118
	/**
119
	 * Extracts the number value from the localised string value.
120
	 *
121
	 * @return string
122
	 */
123
	public function dataValue() {
124
		require_once "Zend/Locale/Format.php";
125
126
		if(!$this->isNumeric()) {
127
			return 0;
128
		}
129
130
		$locale = new Zend_Locale($this->getLocale());
131
132
		$number = Zend_Locale_Format::getNumber(
133
			$this->clean($this->value),
134
			array('locale' => $locale)
135
		);
136
137
		return $number;
138
	}
139
140
	/**
141
	 * Creates a read-only version of the field.
142
	 *
143
	 * @return NumericField_Readonly
144
	 */
145
	public function performReadonlyTransformation() {
146
		$field = new NumericField_Readonly(
147
			$this->name,
148
			$this->title,
149
			$this->value
150
		);
151
152
		$field->setForm($this->form);
153
154
		return $field;
155
	}
156
157
	/**
158
	 * Gets the current locale this field is set to.
159
	 *
160
	 * @return string
161
	 */
162
	public function getLocale() {
163
		if($this->locale) {
164
			return $this->locale;
165
		}
166
167
		return i18n::get_locale();
168
	}
169
170
	/**
171
	 * Override the locale for this field.
172
	 *
173
	 * @param string $locale
174
	 *
175
	 * @return $this
176
	 */
177
	public function setLocale($locale) {
178
		$this->locale = $locale;
179
180
		return $this;
181
	}
182
}
183
184
/**
185
 * Readonly version of a numeric field.
186
 *
187
 * @package forms
188
 * @subpackage fields-basic
189
 */
190
class NumericField_Readonly extends ReadonlyField {
191
	/**
192
	 * @return static
193
	 */
194
	public function performReadonlyTransformation() {
195
		return clone $this;
196
	}
197
198
	/**
199
	 * @return string
200
	 */
201
	public function Value() {
202
		return $this->value ?: '0';
203
	}
204
205
	public function getValueCast() {
206
		return 'Decimal';
207
	}
208
}
209