AbstractMoneyType::getBindingType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\DBAL\Types;
6
7
use Doctrine\DBAL\ParameterType;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use Doctrine\DBAL\Types\Type;
10
use InvalidArgumentException;
11
use Money\Money;
12
13
abstract class AbstractMoneyType extends Type
14
{
15
    abstract protected function createMoney(string $value): Money;
16
17 1
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
18
    {
19 1
        return $platform->getIntegerTypeDeclarationSQL($column);
20
    }
21
22
    public function getBindingType(): ParameterType
23
    {
24
        return ParameterType::INTEGER;
25
    }
26
27
    /**
28
     * @param null|float|int|string $value
29
     */
30 2
    public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Money
31
    {
32 2
        if ($value === null) {
33 1
            return null;
34
        }
35
36 2
        $val = $this->createMoney((string) $value);
37
38 1
        return $val;
39
    }
40
41 2
    public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
42
    {
43 2
        if ($value instanceof Money) {
44
            return $value->getAmount();
45
        }
46
47 2
        if ($value === null) {
48 1
            return null;
49
        }
50
51 1
        throw new InvalidArgumentException('Cannot convert to database value: ' . var_export($value, true));
52
    }
53
}
54