Completed
Pull Request — master (#3)
by Pavel
06:46
created

JsonApiHydrator::hydrateRelationships()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 20
nc 5
nop 2
1
<?php
2
namespace pmill\Doctrine\Hydrator;
3
4
/**
5
 * Json API Request Doctrine Hydrator
6
 */
7
class JsonApiHydrator extends ArrayHydrator
8
{
9
    /**
10
     * @link http://jsonapi.org/format/#document-resource-objects
11
     * @param object|string $entity     Doctrine entity or class name
12
     * @param array         $data       Data to hydrate
13
     *
14
     * @return mixed|object
15
     */
16
    public function hydrate($entity, array $data)
17
    {
18 View Code Duplication
        if (is_string($entity) && class_exists($entity)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
19
            $entity = new $entity;
20
        } elseif (!is_object($entity)) {
21
            throw new \InvalidArgumentException(
22
                'Entity passed to JsonApiHydrator::hydrate() must be a class name or entity object'
23
            );
24
        }
25
26
        if (isset($data['attributes']) && is_array($data['attributes'])) {
27
            $entity = $this->hydrateProperties($entity, $data['attributes']);
28
        }
29
30
        if (isset($data['relationships']) && is_array($data['relationships'])) {
31
            $entity = $this->hydrateRelationships($entity, $data['relationships']);
32
        }
33
34
        return $entity;
35
    }
36
37
    /**
38
     * Map JSON API resource relations to doctrine entity.
39
     *
40
     * @param       $entity
41
     * @param array $relationships
42
     *
43
     * @return array
44
     * @throws \Exception
45
     */
46
    protected function hydrateRelationships($entity, array $relationships)
47
    {
48
        $metadata = $this->entityManager->getClassMetadata(get_class($entity));
49
        foreach ($relationships as $name => $data) {
50
            if (!isset($metadata->associationMappings[$name])) {
51
                throw new \Exception(sprintf('Relation `%s` association not found', $name));
52
            }
53
54
            if (is_array($data['data'])) {
55
                if (isset($data['data']['id']) && isset($data['data']['type'])) {
56
                    $this->hydrateToOneAssociation($entity, $name,
57
                        $metadata->associationMappings[$name],
58
                        $data['data']['id']
59
                    );
60
                } else {
61
                    $this->hydrateToManyAssociation($entity, $name,
62
                        $metadata->associationMappings[$name],
63
                        array_map(
64
                            function($relation) {
65
                                if (isset($relation['id']) && isset($relation['type'])) {
66
                                    return $relation['id'];
67
                                }
68
69
                                return ['attributes' => $relation];
70
                            },
71
                            $data['data']
72
                        )
73
                    );
74
                }
75
            }
76
        }
77
78
        return $entity;
79
    }
80
}
81