1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PhpMob package. |
5
|
|
|
* |
6
|
|
|
* (c) Ishmael Doss <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace PhpMob\Omise\Validator\Constraints; |
15
|
|
|
|
16
|
|
|
use PhpMob\Omise\Currency; |
17
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
18
|
|
|
use Symfony\Component\Validator\Constraint; |
19
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
20
|
|
|
|
21
|
|
|
class ChargeLimitValidator extends ConstraintValidator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param mixed $value |
25
|
|
|
* @param Constraint|ChargeLimit $constraint |
26
|
|
|
*/ |
27
|
|
|
public function validate($value, Constraint $constraint) |
28
|
|
|
{ |
29
|
|
|
if (!\is_array($value) && !\is_object($value)) { |
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
34
|
|
|
$amount = $accessor->getValue($value, $constraint->amountField); |
35
|
|
|
$currency = $accessor->getValue($value, $constraint->currencyField); |
36
|
|
|
|
37
|
|
|
if (!\in_array(\strtolower($currency), [Currency::THB, Currency::JPY, Currency::SGD])) { |
38
|
|
|
// https://www.omise.co/currency-and-amount |
39
|
|
|
// ... todo: how we can do? ... now forward to api side error! |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$amount = $amount * ($constraint->divisor ? $constraint->divisor : 1); |
44
|
|
|
list($min, $max) = Currency::getMinMaxs($currency); |
45
|
|
|
$divisor = Currency::getDivisionOffset($currency); |
46
|
|
|
|
47
|
|
View Code Duplication |
if ($amount < $min) { |
|
|
|
|
48
|
|
|
$this->context->buildViolation($constraint->minMessage) |
49
|
|
|
->atPath($constraint->amountField) |
50
|
|
|
->setParameter('{{ amount }}', number_format($min / $divisor)) |
51
|
|
|
->setParameter('{{ currency }}', $currency) |
52
|
|
|
->addViolation(); |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
if ($amount > $max) { |
|
|
|
|
57
|
|
|
$this->context->buildViolation($constraint->maxMessage) |
58
|
|
|
->atPath($constraint->amountField) |
59
|
|
|
->setParameter('{{ amount }}', number_format($max / $divisor)) |
60
|
|
|
->setParameter('{{ currency }}', $currency) |
61
|
|
|
->addViolation(); |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.