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\Handler\DataTypeHandler\DataTypeHandlerInterface; |
9
|
|
|
use Mikemirten\Component\JsonApi\Mapper\MappingContext; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Attribute handler |
13
|
|
|
* |
14
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Handler |
15
|
|
|
*/ |
16
|
|
|
class AttributeHandler implements HandlerInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* List of generic types |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $genericTypes = ['integer', 'float', 'string', 'boolean']; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Data-type handlers |
27
|
|
|
* |
28
|
|
|
* @var DataTypeHandlerInterface[] |
29
|
|
|
*/ |
30
|
|
|
protected $registeredTypes = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Register data-type handler |
34
|
|
|
* |
35
|
|
|
* @param DataTypeHandlerInterface $handler |
36
|
|
|
*/ |
37
|
1 |
|
public function registerDataTypeHandler(DataTypeHandlerInterface $handler) |
38
|
|
|
{ |
39
|
1 |
|
foreach ($handler->supports() as $name) |
40
|
|
|
{ |
41
|
1 |
|
$this->registeredTypes[$name] = $handler; |
42
|
|
|
} |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
4 |
|
public function toResource($object, ResourceObject $resource, MappingContext $context) |
49
|
|
|
{ |
50
|
4 |
|
$definitions = $context->getDefinition()->getAttributes(); |
51
|
|
|
|
52
|
4 |
|
foreach ($definitions as $definition) |
53
|
|
|
{ |
54
|
4 |
|
$this->processAttributeToResource($object, $resource, $definition); |
55
|
|
|
} |
56
|
3 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Process attribute to resource mapping |
60
|
|
|
* |
61
|
|
|
* @param mixed $object |
62
|
|
|
* @param ResourceObject $resource |
63
|
|
|
* @param Attribute $definition |
64
|
|
|
*/ |
65
|
4 |
|
protected function processAttributeToResource($object, ResourceObject $resource, Attribute $definition) |
66
|
|
|
{ |
67
|
4 |
|
$name = $definition->getName(); |
68
|
4 |
|
$getter = $definition->getGetter(); |
69
|
|
|
|
70
|
4 |
|
$value = $object->$getter(); |
71
|
|
|
|
72
|
4 |
|
if ($definition->hasType()) { |
73
|
3 |
|
$value = $this->processType($definition, $value); |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
$resource->setAttribute($name, $value); |
77
|
3 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Process data-type |
81
|
|
|
* |
82
|
|
|
* @param Attribute $definition |
83
|
|
|
* @param mixed $value |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
3 |
|
protected function processType(Attribute $definition, $value) |
87
|
|
|
{ |
88
|
3 |
|
$type = $definition->getType(); |
89
|
|
|
|
90
|
3 |
|
if (isset($this->registeredTypes[$type])) { |
91
|
1 |
|
$parameters = $definition->getTypeParameters(); |
92
|
|
|
|
93
|
1 |
|
return $this->registeredTypes[$type]->toResource($value, $parameters); |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
if (in_array($type, $this->genericTypes)) { |
97
|
1 |
|
return $this->processGenericType($type, $value); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
throw new \LogicException(sprintf('Unable to handle unknown type "%s" of attribute "%s"', $type, $definition->getName())); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Process generic data-types |
105
|
|
|
* |
106
|
|
|
* @param string $type |
107
|
|
|
* @param mixed $value |
108
|
|
|
* @return bool|float|int|string |
109
|
|
|
*/ |
110
|
1 |
|
protected function processGenericType(string $type, $value) |
111
|
|
|
{ |
112
|
1 |
|
if ($type === 'integer') { |
113
|
|
|
return (int) $value; |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
if ($type === 'float') { |
117
|
|
|
return (float) $value; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
if ($type === 'bool') { |
121
|
|
|
return (bool) $value; |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
return (string) $value; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function fromResource($object, ResourceObject $resource, MappingContext $context) |
131
|
|
|
{ |
132
|
|
|
// TODO: Implement fromResource() method. |
133
|
|
|
} |
134
|
|
|
} |