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 pathOptionManager.php
More file actions
184 lines (156 loc) · 6.66 KB
/
OptionManager.php
File metadata and controls
184 lines (156 loc) · 6.66 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
172
173
174
175
176
177
178
179
180
181
182
183
184
<?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\NormalizerInterface as DataNormalizer;
use FDevs\Serializer\Exception\UnsupportedDataTypeException;
use FDevs\Serializer\Mapping\MetadataInterface;
use FDevs\Serializer\Normalizer\DenormalizerAwareInterface;
use FDevs\Serializer\Normalizer\NormalizerAwareInterface;
use FDevs\Serializer\Option\AccessorInterface;
use FDevs\Serializer\Option\NameConverterInterface;
use FDevs\Serializer\Visibility\AdvancedVisibilityInterface;
use FDevs\Serializer\Visibility\VisibilityInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class OptionManager implements OptionManagerInterface
{
/**
* @var OptionRegistry
*/
private $optionRegistry;
/**
* @var array
*/
private $option;
/**
* @var NameConverterInterface[]
*/
private $convert;
/**
* @var AccessorInterface[]
*/
private $accessor;
/**
* @var DataNormalizer[]
*/
private $normalizer;
/**
* @var DenormalizerInterface[]
*/
private $denormalizer;
/**
* OptionManager constructor.
*
* @param OptionRegistry $optionRegistry
*/
public function __construct(OptionRegistry $optionRegistry)
{
$this->optionRegistry = $optionRegistry;
}
/**
* {@inheritdoc}
*/
public function isVisibleProperty(MetadataInterface $metadata, $name, array $context = [])
{
/** @var VisibilityInterface $visible */
$visible = $this->optionRegistry->getOption($metadata->getName());
return $visible->isVisibleProperty($name, $metadata->getOptions(), $context);
}
/**
* {@inheritdoc}
*/
public function isVisibleValue(MetadataInterface $metadata, $name, $value, array $context = [])
{
/** @var AdvancedVisibilityInterface $visible */
$visible = $this->optionRegistry->getOption($metadata->getName());
return $visible->isVisibleValue($name, $value, $metadata->getOptions(), $context);
}
/**
* {@inheritdoc}
*/
public function convertName(MetadataInterface $convert, $name, array $context = [])
{
$key = spl_object_hash($convert);
if (!isset($this->convert[$key])) {
$this->convert[$key] = $this->optionRegistry->getOption($convert->getName(), OptionRegistry::TYPE_NAME_CONVERTER);
$optionResolver = new OptionsResolver();
$this->convert[$key]->configureOptions($optionResolver);
$this->option[OptionRegistry::TYPE_NAME_CONVERTER][$key] = $optionResolver->resolve($convert->getOptions());
}
return $this->convert[$key]->convert($name, $this->option[OptionRegistry::TYPE_NAME_CONVERTER][$key], $context);
}
/**
* {@inheritdoc}
*/
public function getValue(MetadataInterface $accessor, $object, array $context = [])
{
$key = spl_object_hash($accessor);
if (!isset($this->accessor[$key])) {
$this->accessor[$key] = $this->optionRegistry->getOption($accessor->getName(), OptionRegistry::TYPE_ACCESSOR);
$optionResolver = new OptionsResolver();
$this->accessor[$key]->configureOptions($optionResolver);
$this->option[OptionRegistry::TYPE_ACCESSOR][$key] = $optionResolver->resolve($accessor->getOptions());
}
return $this->accessor[$key]->getValue($object, $this->option[OptionRegistry::TYPE_ACCESSOR][$key], $context);
}
/**
* {@inheritdoc}
*/
public function normalize(MetadataInterface $type, $value, array $context = [], NormalizerInterface $normalizer = null)
{
$key = spl_object_hash($type);
if (!isset($this->normalizer[$key])) {
$this->normalizer[$key] = $this->optionRegistry->getDataType($type->getName());
$optionResolver = new OptionsResolver();
$this->normalizer[$key]->configureOptions($optionResolver);
$this->option[OptionRegistry::TYPE_DATA_TYPE][$key] = $optionResolver->resolve($type->getOptions());
}
if (!$this->normalizer[$key]->supportsNormalization($value, $this->option[OptionRegistry::TYPE_DATA_TYPE][$key])) {
throw new UnsupportedDataTypeException(gettype($value), get_class($this->normalizer[$key]));
}
if ($this->normalizer[$key] instanceof NormalizerAwareInterface) {
$this->normalizer[$key]->setNormalizer($normalizer);
}
return $this->normalizer[$key]->normalize($value, $this->option[OptionRegistry::TYPE_DATA_TYPE][$key], $context);
}
/**
* {@inheritdoc}
*/
public function setValue(MetadataInterface $accessor, $object, $data, array $context = [])
{
$key = spl_object_hash($accessor);
if (!isset($this->accessor[$key])) {
$this->accessor[$key] = $this->optionRegistry->getOption($accessor->getName(), OptionRegistry::TYPE_ACCESSOR);
$optionResolver = new OptionsResolver();
$this->accessor[$key]->configureOptions($optionResolver);
$this->option[OptionRegistry::TYPE_ACCESSOR][$key] = $optionResolver->resolve($accessor->getOptions());
}
return $this->accessor[$key]->setValue($object, $data, $this->option[OptionRegistry::TYPE_ACCESSOR][$key], $context);
}
/**
* {@inheritdoc}
*/
public function denormalize(MetadataInterface $accessor, $value, array $context = [], DenormalizerInterface $denormalizer = null)
{
$key = spl_object_hash($accessor);
if (!isset($this->denormalizer[$key])) {
$this->denormalizer[$key] = $this->optionRegistry->getDataType($accessor->getName());
$optionResolver = new OptionsResolver();
$this->denormalizer[$key]->configureOptions($optionResolver);
$this->option[OptionRegistry::TYPE_DATA_TYPE][$key] = $optionResolver->resolve($accessor->getOptions());
}
if (!$this->denormalizer[$key]->supportsDenormalization($value, $this->option[OptionRegistry::TYPE_DATA_TYPE][$key])) {
throw new UnsupportedDataTypeException(gettype($value), get_class($this->denormalizer[$key]));
}
if ($this->denormalizer[$key] instanceof DenormalizerAwareInterface) {
$this->denormalizer[$key]->setDenormalizer($denormalizer);
}
return $this->denormalizer[$key]->denormalize($value, $this->option[OptionRegistry::TYPE_DATA_TYPE][$key], $context);
}
}