|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the daikon-cqrs/money-interop project. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Daikon\Money\Validator; |
|
10
|
|
|
|
|
11
|
|
|
use Daikon\Boot\Middleware\ActionHandler; |
|
12
|
|
|
use Daikon\Boot\Middleware\Action\ValidatorInterface; |
|
13
|
|
|
use Daikon\Boot\Middleware\Action\ValidatorTrait; |
|
14
|
|
|
use Daikon\Interop\Assertion; |
|
15
|
|
|
use Daikon\Interop\InvalidArgumentException; |
|
16
|
|
|
use Daikon\Money\Service\MoneyService; |
|
17
|
|
|
use Daikon\Money\ValueObject\MoneyInterface; |
|
18
|
|
|
use Money\Exception\ParserException; |
|
19
|
|
|
|
|
20
|
|
|
final class MoneyValidator implements ValidatorInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use ValidatorTrait; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
private MoneyService $moneyService; |
|
25
|
|
|
|
|
26
|
|
|
private string $input; |
|
27
|
|
|
|
|
28
|
|
|
/** @var null|bool|string */ |
|
29
|
|
|
private $export; |
|
30
|
|
|
|
|
31
|
|
|
/** @var mixed */ |
|
32
|
|
|
private $default; |
|
33
|
|
|
|
|
34
|
|
|
private bool $required; |
|
35
|
|
|
|
|
36
|
|
|
private ?string $convert; |
|
37
|
|
|
|
|
38
|
|
|
private ?string $min; |
|
39
|
|
|
|
|
40
|
|
|
private ?string $max; |
|
41
|
|
|
|
|
42
|
|
|
private int $severity; |
|
43
|
|
|
|
|
44
|
|
|
private string $payload; |
|
45
|
|
|
|
|
46
|
|
|
private string $exportErrors; |
|
47
|
|
|
|
|
48
|
|
|
private string $exportErrorCode; |
|
49
|
|
|
|
|
50
|
|
|
private string $exportErrorSeverity; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param mixed $export |
|
54
|
|
|
* @param mixed $default |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct( |
|
57
|
|
|
MoneyService $moneyService, |
|
58
|
|
|
string $input, |
|
59
|
|
|
$export = null, |
|
60
|
|
|
$default = null, |
|
61
|
|
|
bool $required = true, |
|
62
|
|
|
string $convert = null, |
|
63
|
|
|
string $min = null, |
|
64
|
|
|
string $max = null, |
|
65
|
|
|
int $severity = self::SEVERITY_ERROR, |
|
66
|
|
|
string $payload = ActionHandler::ATTR_PAYLOAD, |
|
67
|
|
|
string $exportErrors = ActionHandler::ATTR_ERRORS, |
|
68
|
|
|
string $exportErrorCode = ActionHandler::ATTR_ERROR_CODE, |
|
69
|
|
|
string $exportErrorSeverity = ActionHandler::ATTR_ERROR_SEVERITY |
|
70
|
|
|
) { |
|
71
|
|
|
$this->moneyService = $moneyService; |
|
72
|
|
|
$this->input = $input; |
|
73
|
|
|
$this->export = $export; |
|
74
|
|
|
$this->default = $default; |
|
75
|
|
|
$this->required = $required; |
|
76
|
|
|
$this->convert = $convert; |
|
77
|
|
|
$this->min = $min; |
|
78
|
|
|
$this->max = $max; |
|
79
|
|
|
$this->severity = $severity; |
|
80
|
|
|
$this->payload = $payload; |
|
81
|
|
|
$this->exportErrors = $exportErrors; |
|
82
|
|
|
$this->exportErrorCode = $exportErrorCode; |
|
83
|
|
|
$this->exportErrorSeverity = $exportErrorSeverity; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** @param mixed $input */ |
|
87
|
|
|
private function validate(string $name, $input): MoneyInterface |
|
88
|
|
|
{ |
|
89
|
|
|
Assertion::string($input, 'Must be a string.'); |
|
90
|
|
|
|
|
91
|
|
|
try { |
|
92
|
|
|
$money = $this->moneyService->parse($input); |
|
93
|
|
|
if ($this->convert && $money->getCurrency() !== $this->convert) { |
|
94
|
|
|
$money = $this->moneyService->convert($money, $this->convert); |
|
95
|
|
|
} |
|
96
|
|
|
} catch (ParserException $error) { |
|
97
|
|
|
throw new InvalidArgumentException('Invalid amount.'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($this->min && !$money->isGreaterThanOrEqual($this->moneyService->parse($this->min))) { |
|
101
|
|
|
throw new InvalidArgumentException("Amount must be at least $this->min."); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ($this->max && !$money->isLessThanOrEqual($this->moneyService->parse($this->max))) { |
|
105
|
|
|
throw new InvalidArgumentException("Amount must be at most $this->max."); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $money; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|