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