Skip to content

Commit f1dd75d

Browse files
committed
Make sure required numerical value is valid when zero
1 parent 076c624 commit f1dd75d

4 files changed

Lines changed: 60 additions & 6 deletions

File tree

Filter/PositiveNumber.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ class PositiveNumber implements FilterInterface
66
{
77
public function filter(mixed $value): mixed
88
{
9-
return abs((int)$value);
9+
if ($value > 0) {
10+
return $value;
11+
}
12+
13+
return 0;
1014
}
1115
}

Util/IsEmpty.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Loki\Components\Util;
5+
6+
use Loki\Components\Component\ComponentInterface;
7+
8+
class IsEmpty
9+
{
10+
public function execute(ComponentInterface $component, mixed $value): bool
11+
{
12+
if ($this->shouldBeNumber($component) && is_numeric($value)) {
13+
return false;
14+
}
15+
16+
if ($this->shouldBePositiveNumber($component) && is_numeric($value) && $value >= 0) {
17+
return false;
18+
}
19+
20+
if (is_string($value) && strlen($value) > 0) {
21+
return false;
22+
}
23+
24+
return empty($value);
25+
}
26+
27+
private function shouldBePositiveNumber(ComponentInterface $component): bool
28+
{
29+
$filters = $component->getFilters();
30+
31+
return in_array('positive_number', $filters, true);
32+
}
33+
34+
private function shouldBeNumber(ComponentInterface $component): bool
35+
{
36+
$filters = $component->getFilters();
37+
38+
return in_array('number', $filters, true);
39+
}
40+
}

Validator/RequiredValidator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
namespace Loki\Components\Validator;
44

55
use Loki\Components\Component\ComponentInterface;
6+
use Loki\Components\Util\IsEmpty;
67

78
class RequiredValidator implements ValidatorInterface
89
{
10+
public function __construct(
11+
private IsEmpty $isEmpty,
12+
) {
13+
}
14+
915
public function validate(mixed $value, ?ComponentInterface $component = null): bool|array
1016
{
11-
if (empty($value)) {
17+
if ($this->isEmpty->execute($component, $value)) {
1218
return [(string)__('Value is required')];
1319
}
1420

Validator/Validator.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Loki\Components\Validator;
44

5+
use Loki\Components\Util\IsEmpty;
56
use Magento\Framework\Phrase;
67
use Loki\Components\Component\ComponentInterface;
78
use Loki\Components\Messages\LocalMessage;
@@ -12,18 +13,21 @@ class Validator
1213
public function __construct(
1314
private readonly ValidatorRegistry $validatorRegistry,
1415
private readonly Ajax $ajax,
16+
private readonly IsEmpty $isEmpty,
1517
) {
1618
}
1719

1820
public function validate(
1921
ComponentInterface $component,
2022
mixed $data = null
2123
): bool {
22-
if ($this->isRequiredButEmpty($component, $data) && false === $this->ajax->isAjax()) {
24+
if ($this->isRequired($component)
25+
&& $this->isEmpty->execute($component, $data)
26+
&& false === $this->ajax->isAjax()) {
2327
return true;
2428
}
2529

26-
if (empty($data) && true === in_array('required', $component->getValidators())) {
30+
if ($this->isRequired($component) && $this->isEmpty->execute($component, $data)) {
2731
return false;
2832
}
2933

@@ -69,8 +73,8 @@ public function validate(
6973
return true;
7074
}
7175

72-
private function isRequiredButEmpty(ComponentInterface $component, mixed $data): bool
76+
private function isRequired(ComponentInterface $component): bool
7377
{
74-
return empty($data) && in_array('required', $component->getValidators());
78+
return in_array('required', $component->getValidators());
7579
}
7680
}

0 commit comments

Comments
 (0)