-
Notifications
You must be signed in to change notification settings - Fork 641
/
Number.php
187 lines (159 loc) · 4.43 KB
/
Number.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\fields;
use Craft;
use craft\base\ElementInterface;
use craft\base\Field;
use craft\base\PreviewableFieldInterface;
use craft\helpers\Db;
use craft\helpers\Localization;
use craft\i18n\Locale;
/**
* Number represents a Number field.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0
*/
class Number extends Field implements PreviewableFieldInterface
{
// Static
// =========================================================================
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('app', 'Number');
}
// Properties
// =========================================================================
/**
* @var int|float|null The default value for new elements
*/
public $defaultValue;
/**
* @var int|float The minimum allowed number
*/
public $min = 0;
/**
* @var int|float|null The maximum allowed number
*/
public $max;
/**
* @var int The number of digits allowed after the decimal point
*/
public $decimals = 0;
/**
* @var int|null The size of the field
*/
public $size;
// Public Methods
// =========================================================================
/**
* @inheritdoc
*/
public function init()
{
parent::init();
// Normalize $defaultValue
if ($this->defaultValue === '') {
$this->defaultValue = null;
}
// Normalize $max
if ($this->max === '') {
$this->max = null;
}
// Normalize $min
if ($this->min === '') {
$this->min = null;
}
// Normalize $decimals
if (!$this->decimals) {
$this->decimals = 0;
}
// Normalize $size
if ($this->size !== null && !$this->size) {
$this->size = null;
}
}
/**
* @inheritdoc
*/
public function rules()
{
$rules = parent::rules();
$rules[] = [['min', 'max'], 'number'];
$rules[] = [['decimals', 'size'], 'integer'];
$rules[] = [
['max'],
'compare',
'compareAttribute' => 'min',
'operator' => '>='
];
if (!$this->decimals) {
$rules[] = [['min', 'max'], 'integer'];
}
return $rules;
}
/**
* @inheritdoc
*/
public function getSettingsHtml()
{
return Craft::$app->getView()->renderTemplate('_components/fieldtypes/Number/settings',
[
'field' => $this
]);
}
/**
* @inheritdoc
*/
public function getContentColumnType(): string
{
return Db::getNumericalColumnType($this->min, $this->max, $this->decimals);
}
/**
* @inheritdoc
*/
public function normalizeValue($value, ElementInterface $element = null)
{
// Was this submitted with a locale ID?
if (isset($value['locale'], $value['value'])) {
$value = Localization::normalizeNumber($value['value'], $value['locale']);
}
return $this->isValueEmpty($value, $element) ? null : $value;
}
/**
* @inheritdoc
*/
public function getInputHtml($value, ElementInterface $element = null): string
{
if ($this->isFresh($element) && $this->defaultValue !== null) {
$value = $this->defaultValue;
}
// If decimals is 0 (or null, empty for whatever reason), don't run this
if ($value !== null && $this->decimals) {
$decimalSeparator = Craft::$app->getLocale()->getNumberSymbol(Locale::SYMBOL_DECIMAL_SEPARATOR);
$value = number_format($value, $this->decimals, $decimalSeparator, '');
}
return '<input type="hidden" name="'.$this->handle.'[locale]" value="'.Craft::$app->language.'">'.
Craft::$app->getView()->renderTemplate('_includes/forms/text', [
'name' => $this->handle.'[value]',
'value' => $value,
'size' => $this->size
]);
}
/**
* @inheritdoc
*/
public function getElementValidationRules(): array
{
return [
['number', 'min' => $this->min ?: null, 'max' => $this->max ?: null],
];
}
}