1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Mapper\Handler; |
5
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Document\ResourceObject; |
7
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Attribute; |
8
|
|
|
use Mikemirten\Component\JsonApi\Mapper\MappingContext; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Attribute handler |
12
|
|
|
* |
13
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Handler |
14
|
|
|
*/ |
15
|
|
|
class AttributeHandler implements HandlerInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var DataTypeManager |
19
|
|
|
*/ |
20
|
|
|
protected $typeManager; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* AttributeHandler constructor. |
24
|
|
|
* |
25
|
|
|
* @param DataTypeManager $typeManager |
26
|
|
|
*/ |
27
|
6 |
|
public function __construct(DataTypeManager $typeManager) |
28
|
|
|
{ |
29
|
6 |
|
$this->typeManager = $typeManager; |
30
|
6 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
3 |
|
public function toResource($object, ResourceObject $resource, MappingContext $context) |
36
|
|
|
{ |
37
|
3 |
|
$definitions = $context->getDefinition()->getAttributes(); |
38
|
|
|
|
39
|
3 |
|
foreach ($definitions as $definition) |
40
|
|
|
{ |
41
|
3 |
|
$this->processAttributeToResource($object, $resource, $definition); |
42
|
|
|
} |
43
|
3 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Process attribute to resource mapping |
47
|
|
|
* |
48
|
|
|
* @param mixed $object |
49
|
|
|
* @param ResourceObject $resource |
50
|
|
|
* @param Attribute $definition |
51
|
|
|
*/ |
52
|
3 |
View Code Duplication |
protected function processAttributeToResource($object, ResourceObject $resource, Attribute $definition) |
|
|
|
|
53
|
|
|
{ |
54
|
3 |
|
$name = $definition->getName(); |
55
|
3 |
|
$getter = $definition->getGetter(); |
56
|
|
|
|
57
|
3 |
|
$value = $object->$getter(); |
58
|
|
|
|
59
|
3 |
|
if ($value === null && ! $definition->getProcessNull()) { |
60
|
1 |
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
$value = $this->typeManager->toResource($definition, $value); |
64
|
|
|
|
65
|
2 |
|
$resource->setAttribute($name, $value); |
66
|
2 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
3 |
|
public function fromResource($object, ResourceObject $resource, MappingContext $context) |
72
|
|
|
{ |
73
|
3 |
|
$definitions = $context->getDefinition()->getAttributes(); |
74
|
|
|
|
75
|
3 |
|
foreach ($definitions as $definition) |
76
|
|
|
{ |
77
|
3 |
|
if (! $definition->hasSetter()) { |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
$this->processResourceToAttribute($object, $resource, $definition); |
82
|
|
|
} |
83
|
3 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Process resource to attribute mapping |
87
|
|
|
* |
88
|
|
|
* @param mixed $object |
89
|
|
|
* @param ResourceObject $resource |
90
|
|
|
* @param Attribute $definition |
91
|
|
|
*/ |
92
|
3 |
View Code Duplication |
protected function processResourceToAttribute($object, ResourceObject $resource, Attribute $definition) |
|
|
|
|
93
|
|
|
{ |
94
|
3 |
|
$name = $definition->getName(); |
95
|
|
|
|
96
|
3 |
|
if (! $resource->hasAttribute($name)) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
3 |
|
$value = $resource->getAttribute($name); |
101
|
|
|
|
102
|
3 |
|
if ($value === null && ! $definition->getProcessNull()) { |
103
|
1 |
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
$value = $this->typeManager->fromResource($definition, $value); |
107
|
2 |
|
$setter = $definition->getSetter(); |
108
|
|
|
|
109
|
2 |
|
$object->$setter($value); |
110
|
|
|
} |
111
|
|
|
} |
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.