Completed
Push — 1.x ( fd103f...647e2f )
by Daniel
23:51 queued 13:50
created

UserHydrator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 5
Bugs 5 Features 0
Metric Value
wmc 6
c 5
b 5
f 0
lcom 0
cbo 2
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extract() 0 14 3
A hydrate() 0 8 2
A mapField() 0 6 1
1
<?php
2
3
namespace ZfcUser\Mapper;
4
5
use Zend\Stdlib\Hydrator\ClassMethods;
6
use ZfcUser\Entity\UserInterface as UserEntityInterface;
7
8
class UserHydrator extends ClassMethods
9
{
10
11
    /**
12
     * Extract values from an object
13
     *
14
     * @param  object $object
15
     * @return array
16
     * @throws Exception\InvalidArgumentException
17
     */
18
    public function extract($object)
19
    {
20
        if (!$object instanceof UserEntityInterface) {
21
            throw new Exception\InvalidArgumentException('$object must be an instance of ZfcUser\Entity\UserInterface');
22
        }
23
        /* @var $object UserInterface */
24
        $data = parent::extract($object);
25
        if ($data['id'] !== null) {
26
            $data = $this->mapField('id', 'user_id', $data);
27
        } else {
28
            unset($data['id']);
29
        }
30
        return $data;
31
    }
32
33
    /**
34
     * Hydrate $object with the provided $data.
35
     *
36
     * @param  array $data
37
     * @param  object $object
38
     * @return UserInterface
39
     * @throws Exception\InvalidArgumentException
40
     */
41
    public function hydrate(array $data, $object)
42
    {
43
        if (!$object instanceof UserEntityInterface) {
44
            throw new Exception\InvalidArgumentException('$object must be an instance of ZfcUser\Entity\UserInterface');
45
        }
46
        $data = $this->mapField('user_id', 'id', $data);
47
        return parent::hydrate($data, $object);
48
    }
49
50
    protected function mapField($keyFrom, $keyTo, array $array)
51
    {
52
        $array[$keyTo] = $array[$keyFrom];
53
        unset($array[$keyFrom]);
54
        return $array;
55
    }
56
}
57