forked from statamic/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRange.php
More file actions
110 lines (99 loc) · 3.45 KB
/
Range.php
File metadata and controls
110 lines (99 loc) · 3.45 KB
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
<?php
namespace Statamic\Fieldtypes;
use Statamic\Facades\GraphQL;
use Statamic\Fields\Fieldtype;
class Range extends Fieldtype
{
protected $categories = ['controls'];
protected function configFieldItems(): array
{
return [
[
'display' => __('Input Behavior'),
'fields' => [
'min' => [
'display' => __('Min'),
'instructions' => __('statamic::fieldtypes.range.config.min'),
'type' => 'text',
'input_type' => 'number',
'validate' => ['numeric'],
'default' => 0,
'width' => 50,
],
'max' => [
'display' => __('Max'),
'instructions' => __('statamic::fieldtypes.range.config.max'),
'type' => 'text',
'input_type' => 'number',
'validate' => ['numeric'],
'default' => 100,
'width' => 50,
],
'step' => [
'display' => __('Step'),
'instructions' => __('statamic::fieldtypes.range.config.step'),
'type' => 'text',
'input_type' => 'number',
'validate' => ['numeric'],
'default' => 1,
'width' => 50,
],
],
],
[
'display' => __('Appearance'),
'fields' => [
'prepend' => [
'display' => __('Prepend'),
'instructions' => __('statamic::fieldtypes.range.config.prepend'),
'type' => 'text',
'width' => 50,
],
'append' => [
'display' => __('Append'),
'instructions' => __('statamic::fieldtypes.range.config.append'),
'type' => 'text',
'width' => 50,
],
],
],
[
'display' => __('Data & Format'),
'fields' => [
'default' => [
'display' => __('Default Value'),
'instructions' => __('statamic::messages.fields_default_instructions'),
'type' => 'text',
'input_type' => 'number',
'default' => null,
],
],
],
];
}
public function process($data)
{
if ($this->usesDecimals()) {
return (float) $data;
}
return (int) $data;
}
protected function usesDecimals(): bool
{
$step = $this->config('step', 1);
$min = $this->config('min', 0);
$max = $this->config('max', 100);
return $this->isDecimal($step) || $this->isDecimal($min) || $this->isDecimal($max);
}
protected function isDecimal($value): bool
{
if (! is_numeric($value)) {
return false;
}
return floor((float) $value) != (float) $value;
}
public function toGqlType()
{
return $this->usesDecimals() ? GraphQL::float() : GraphQL::int();
}
}