This repository was archived by the owner on Feb 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataTypeFactory.php
More file actions
171 lines (151 loc) · 4.33 KB
/
DataTypeFactory.php
File metadata and controls
171 lines (151 loc) · 4.33 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/*
* This file is part of the 4devs Serialiser package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FDevs\Serializer;
use FDevs\Serializer\DataType\BooleanType;
use FDevs\Serializer\DataType\CollectionType;
use FDevs\Serializer\DataType\DateTimeType;
use FDevs\Serializer\DataType\DoctrineType;
use FDevs\Serializer\DataType\FloatType;
use FDevs\Serializer\DataType\IntegerType;
use FDevs\Serializer\DataType\ObjectType;
use FDevs\Serializer\DataType\StringType;
use FDevs\Serializer\DataType\TypeInterface;
use FDevs\Serializer\Exception\DataTypeNotFoundException;
use FDevs\Serializer\Mapping\MetadataType;
use FDevs\Serializer\Normalizer\DenormalizerAwareInterface;
use FDevs\Serializer\Normalizer\DenormalizerAwareTrait;
use FDevs\Serializer\Normalizer\NormalizerAwareInterface;
use FDevs\Serializer\Normalizer\NormalizerAwareTrait;
class DataTypeFactory
{
use DenormalizerAwareTrait;
use NormalizerAwareTrait;
/**
* @var ResolvedDataType[]
*/
private $resolvedTypeList = [];
/**
* @var TypeInterface[]
*/
private $typeList = [];
/**
* @var array
*/
private $mappingTypeList = [
'int' => IntegerType::class,
'integer' => IntegerType::class,
'float' => FloatType::class,
'bool' => BooleanType::class,
'boolean' => BooleanType::class,
'collection' => CollectionType::class,
'datetime' => DateTimeType::class,
'doctrine' => DoctrineType::class,
'object' => ObjectType::class,
'string' => StringType::class,
];
/**
* DataTypeRegistry constructor.
*
* @param TypeInterface[] $types
*/
public function __construct(array $types = [])
{
foreach ($types as $type) {
$this->addType($type);
}
}
/**
* @param MetadataType $type
*
* @return TypeInterface
*/
public function getType(MetadataType $type)
{
return $this->getResolvedType($type)->getType();
}
/**
* @param MetadataType $type
*
* @return array
*/
public function resolveOptions(MetadataType $type)
{
return $this->getResolvedType($type)->getOptionsResolver()->resolve($type->getOptions());
}
/**
* @param TypeInterface $type
*
* @return $this
*/
public function addType(TypeInterface $type)
{
$this->typeList[get_class($type)] = $type;
return $this;
}
/**
* @param string $type
* @param array $options
*
* @throws DataTypeNotFoundException
*
* @return MetadataType
*/
public function createType($type, array $options = [])
{
$class = isset($this->mappingTypeList[$type]) ? $this->mappingTypeList[$type] : $type;
if (!class_exists($class)) {
throw new DataTypeNotFoundException($type, $class);
}
return new MetadataType($class, $options);
}
/**
* @param string $type
* @param string $class
*
* @return $this
*/
public function addMappingType($type, $class)
{
$this->mappingTypeList[$type] = $class;
return $this;
}
/**
* @param MetadataType $type
*
* @return ResolvedDataType
*/
private function getResolvedType(MetadataType $type)
{
$class = $type->getName();
if (!isset($this->resolvedTypeList[$class])) {
if (isset($this->typeList[$class])) {
$type = $this->typeList[$class];
unset($this->typeList[$class]);
} else {
$type = new $class();
}
if ($type instanceof DenormalizerAwareInterface && $denormalizer = $this->getDenormalizer()) {
$type->setDenormalizer($denormalizer);
}
if ($type instanceof NormalizerAwareInterface && $normalizer = $this->getNormalizer()) {
$type->setNormalizer($normalizer);
}
$this->resolvedTypeList[$class] = $this->resolveType($type);
}
return $this->resolvedTypeList[$class];
}
/**
* @param TypeInterface $type
*
* @return ResolvedDataType
*/
private function resolveType(TypeInterface $type)
{
return new ResolvedDataType($type);
}
}