|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jellyfish\Transfer\Definition; |
|
4
|
|
|
|
|
5
|
|
|
use Jellyfish\Serializer\SerializerInterface; |
|
6
|
|
|
|
|
7
|
|
|
class ClassDefinitionMapMapper implements ClassDefinitionMapMapperInterface |
|
8
|
|
|
{ |
|
9
|
|
|
protected const TYPE = ClassDefinition::class . '[]'; |
|
10
|
|
|
protected const FORMAT = 'json'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var \Jellyfish\Serializer\SerializerInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $serializer; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param \Jellyfish\Serializer\SerializerInterface $serializer |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct( |
|
21
|
|
|
SerializerInterface $serializer |
|
22
|
|
|
) { |
|
23
|
|
|
$this->serializer = $serializer; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $data |
|
28
|
|
|
* |
|
29
|
|
|
* @return \Jellyfish\Transfer\Definition\ClassDefinitionInterface[] |
|
30
|
|
|
*/ |
|
31
|
|
|
public function from(string $data): array |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var \Jellyfish\Transfer\Definition\ClassDefinitionInterface[] $classDefinitions */ |
|
34
|
|
|
$classDefinitions = $this->serializer->deserialize($data, static::TYPE, static::FORMAT); |
|
35
|
|
|
$classDefinitionMap = []; |
|
36
|
|
|
|
|
37
|
|
|
foreach ($classDefinitions as $classDefinition) { |
|
38
|
|
|
$classDefinitionMapKey = $this->generateClassDefinitionMapKey($classDefinition); |
|
39
|
|
|
$classDefinitionMap[$classDefinitionMapKey] = $classDefinition; |
|
40
|
|
|
$classPropertyDefinitionMap = []; |
|
41
|
|
|
|
|
42
|
|
|
foreach ($classDefinition->getProperties() as $classPropertyDefinition) { |
|
43
|
|
|
$classPropertyDefinitionMap[$classPropertyDefinition->getName()] = $classPropertyDefinition; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$classDefinition->setProperties($classPropertyDefinitionMap); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $classDefinitionMap; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param \Jellyfish\Transfer\Definition\ClassDefinitionInterface $classDefinition |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function generateClassDefinitionMapKey(ClassDefinitionInterface $classDefinition): string |
|
58
|
|
|
{ |
|
59
|
|
|
if ($classDefinition->getNamespace() === null) { |
|
60
|
|
|
return $classDefinition->getName(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return \sprintf('%s\\%s', $classDefinition->getNamespace(), $classDefinition->getName()); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|