julienmonnerie/kirby/config/fields/number.php

49 lines
942 B
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
use Kirby\Toolkit\Str;
return [
2022-08-31 15:02:43 +02:00
'props' => [
/**
* Default number that will be saved when a new page/user/file is created
*/
'default' => function ($default = null) {
2024-12-20 12:37:52 +01:00
return $this->toNumber($default) ?? '';
2022-08-31 15:02:43 +02:00
},
/**
* The lowest allowed number
*/
2025-04-21 18:57:21 +02:00
'min' => function (float|null $min = null) {
2022-08-31 15:02:43 +02:00
return $min;
},
/**
* The highest allowed number
*/
2025-04-21 18:57:21 +02:00
'max' => function (float|null $max = null) {
2022-08-31 15:02:43 +02:00
return $max;
},
/**
* Allowed incremental steps between numbers (i.e `0.5`)
*/
'step' => function ($step = null) {
2024-12-20 12:37:52 +01:00
return $this->toNumber($step) ?? '';
2022-08-31 15:02:43 +02:00
},
'value' => function ($value = null) {
2024-12-20 12:37:52 +01:00
return $this->toNumber($value) ?? '';
2022-08-31 15:02:43 +02:00
}
],
'methods' => [
'toNumber' => function ($value) {
if ($this->isEmpty($value) === true) {
return null;
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
return is_float($value) === true ? $value : (float)Str::float($value);
}
],
'validations' => [
'min',
'max'
]
2022-06-17 17:51:59 +02:00
];