Passed
Push — master ( 98afe9...62e274 )
by Michael
02:28
created

ObjectMapper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 84
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4
ccs 19
cts 19
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addHandler() 0 4 1
A toResource() 0 14 2
A fromResource() 0 9 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper;
5
6
use Mikemirten\Component\JsonApi\Document\ResourceObject;
7
use Mikemirten\Component\JsonApi\Mapper\Handler\HandlerInterface;
8
use Mikemirten\Component\JsonApi\Mapper\Handler\IdentifierHandler\IdentifierHandlerInterface;
9
use Mikemirten\Component\JsonApi\Mapper\Handler\TypeHandler\TypeHandlerInterface;
10
11
/**
12
 * Object mapper.
13
 *
14
 * Maps data between object and resource of document.
15
 * Can be helpful for serialization of objects to JSON API document.
16
 *
17
 * Each handler responsible for certain part of resource (links, metadata, relationships...).
18
 * Identifier and type handlers are specific and required to resolve id and type of resource.
19
 * Other handlers are optional.
20
 *
21
 * @package Mikemirten\Component\JsonApi\Mapper
22
 */
23
class ObjectMapper
24
{
25
    /**
26
     * Required identifier handler
27
     * Must always be present regardless of other handlers
28
     *
29
     * @var IdentifierHandlerInterface
30
     */
31
    protected $identifierHandler;
32
33
    /**
34
     * Required type handler
35
     * Must always be present regardless of other handlers
36
     *
37
     * @var TypeHandlerInterface
38
     */
39
    protected $typeHandler;
40
41
    /**
42
     * Mapping handlers
43
     *
44
     * @var HandlerInterface[]
45
     */
46
    protected $handlers = [];
47
48
    /**
49
     * ObjectMapper constructor.
50
     *
51
     * @param IdentifierHandlerInterface $identifierHandler
52
     * @param TypeHandlerInterface       $typeHandler
53
     */
54 2
    public function __construct(IdentifierHandlerInterface $identifierHandler, TypeHandlerInterface $typeHandler)
55
    {
56 2
        $this->identifierHandler = $identifierHandler;
57 2
        $this->typeHandler       = $typeHandler;
58 2
    }
59
60
    /**
61
     * Add mapping handler
62
     *
63
     * @param HandlerInterface $handler
64
     */
65 2
    public function addHandler(HandlerInterface $handler)
66
    {
67 2
        $this->handlers[] = $handler;
68 2
    }
69
70
    /**
71
     * Map object's data to resource
72
     *
73
     * @param  mixed $object
74
     * @return ResourceObject
75
     */
76 1
    public function toResource($object): ResourceObject
77
    {
78 1
        $id   = $this->identifierHandler->getIdentifier($object);
79 1
        $type = $this->typeHandler->getType($object);
80
81 1
        $resource = new ResourceObject($id, $type);
82
83 1
        foreach ($this->handlers as $handler)
84
        {
85 1
            $handler->toResource($object, $resource);
86
        }
87
88 1
        return $resource;
89
    }
90
91
    /**
92
     * Map resource's data to provided object
93
     *
94
     * @param mixed $object
95
     * @param ResourceObject $resource
96
     */
97 1
    public function fromResource($object, ResourceObject $resource)
98
    {
99 1
        $this->identifierHandler->setIdentifier($object, $resource->getId());
100
101 1
        foreach ($this->handlers as $handler)
102
        {
103 1
            $handler->fromResource($object, $resource);
104
        }
105
    }
106
}