-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathInterfaceTypeExtractor.php
More file actions
114 lines (103 loc) · 3.28 KB
/
InterfaceTypeExtractor.php
File metadata and controls
114 lines (103 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\PageBundle\Serializer;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\PageBundle\Model\PageInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\PropertyInfo\Type as LegacyType;
use Symfony\Component\TypeInfo\Type;
final class InterfaceTypeExtractor implements PropertyTypeExtractorInterface
{
/**
* @param class-string<PageInterface> $pageClass
* @param class-string<BlockInterface> $blockClass
*/
public function __construct(
private string $pageClass,
private string $blockClass,
) {
}
/**
* @param array<array-key, mixed> $context
*
* @return LegacyType[]|null
*/
public function getTypes(string $class, string $property, array $context = []): ?array
{
if ($this->pageClass === $class) {
if ('children' === $property) {
return [new LegacyType(
LegacyType::BUILTIN_TYPE_OBJECT,
false,
null,
true,
null,
$this->getPageType()
)];
}
if ('blocks' === $property) {
return [new LegacyType(
LegacyType::BUILTIN_TYPE_OBJECT,
false,
null,
true,
null,
$this->getBlockType()
)];
}
if ('parent' === $property) {
return [$this->getPageType()];
}
} elseif ($this->blockClass === $class) {
if ('children' === $property) {
return [new LegacyType(
LegacyType::BUILTIN_TYPE_OBJECT,
false,
null,
true,
null,
$this->getBlockType()
)];
}
}
return null;
}
/**
* @param array<array-key, mixed> $context
*/
public function getType(string $class, string $property, array $context = []): ?Type
{
if ($this->pageClass === $class) {
if ('children' === $property) {
return Type::list(Type::object($this->pageClass));
}
if ('blocks' === $property) {
return Type::list(Type::object($this->blockClass));
}
if ('parent' === $property) {
return Type::object($this->pageClass);
}
} elseif ($this->blockClass === $class) {
if ('children' === $property) {
return Type::list(Type::object($this->blockClass));
}
}
return null;
}
private function getPageType(): LegacyType
{
return new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, $this->pageClass);
}
private function getBlockType(): LegacyType
{
return new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, $this->blockClass);
}
}