Completed
Pull Request — master (#32)
by Sascha-Oliver
15:35 queued 13:23
created

MoneyHydrator::hydrate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 3
eloc 6
nc 2
nop 2
crap 3.0261
1
<?php
2
3
namespace ZFBrasil\DoctrineMoneyModule\Hydrator;
4
5
use Zend\Hydrator\HydratorInterface;
6
use Money\Money;
7
use Money\Currency;
8
9
/**
10
 * Hydrator for Money object
11
 *
12
 * @author Fábio Carneiro <[email protected]>
13
 * @license MIT
14
 */
15
class MoneyHydrator implements HydratorInterface
16
{
17
    /**
18
     * {@inheritDoc}
19
     */
20 5
    public function extract($object)
21
    {
22
        return [
23 5
            'amount' => $object->getAmount(),
24 5
            'currency' => $object->getCurrency()->getName()
25 5
        ];
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     *
31
     * @return Money
32
     */
33 3
    public function hydrate(array $data, $object)
34
    {
35 3
        if (empty($data['amount']) || empty($data['currency'])) {
36
            return null;
37
        }
38
39 3
        return new Money(
40 3
            $data['amount'],
41 3
            new Currency($data['currency'])
42 3
        );
43
    }
44
}
45