Skip to content

Commit 547c3c5

Browse files
committed
refactor: move isNumeric/asFloat to BaseField
1 parent 3a4215e commit 547c3c5

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

src/opnsense/mvc/app/models/OPNsense/Base/Constraints/BaseConstraint.php

-18
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,6 @@ public function isEmpty($node)
4949
return false;
5050
}
5151

52-
/**
53-
* check if field value is numeric
54-
* @param $node
55-
* @return bool
56-
*/
57-
public function isNumeric($node): bool {
58-
return is_numeric((string)$node->getCurrentValue());
59-
}
60-
61-
/**
62-
* Try to convert to current value as float
63-
* @param $node
64-
* @return float
65-
*/
66-
public function asFloat($node): float {
67-
return floatval((string)$node->getCurrentValue());
68-
}
69-
7052
/**
7153
* @param $validator
7254
* @param $attribute

src/opnsense/mvc/app/models/OPNsense/Base/Constraints/ComparedToFieldConstraint.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@ public function validate($validator, $attribute): bool
6161
// if the other field is not set, or invalid type -> ignore this constraint
6262
if (
6363
$this->isEmpty($other_node_content) ||
64-
!$this->isNumeric($node) && !$this->isNumeric($other_node_content)
64+
!$node->isNumeric() && !$other_node_content->isNumeric()
6565
) {
6666
return true;
6767
}
6868

6969
if (
7070
!$this->is_constraint_fulfilled(
7171
$operator,
72-
$this->asFloat($node),
73-
$this->asFloat($other_node_content)
72+
$node->asFloat(),
73+
$other_node_content->asFloat(),
7474
)
7575
) {
7676
$this->appendMessage($validator, $attribute);

src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php

+16
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,22 @@ public function getCurrentValue(): ?string {
371371
return (string)$this->internalValue;
372372
}
373373

374+
/**
375+
* check if field value is numeric
376+
* @return bool
377+
*/
378+
public function isNumeric(): bool {
379+
return is_numeric($this->getCurrentValue());
380+
}
381+
382+
/**
383+
* Try to convert to current value as float
384+
* @return float
385+
*/
386+
public function asFloat(): float {
387+
return floatval($this->getCurrentValue());
388+
}
389+
374390
/**
375391
* default setter
376392
* @param string $value set field value

0 commit comments

Comments
 (0)