8000 re-added supports(de)normalization() · symfony/symfony@c3d6123 · GitHub
[go: up one dir, main page]

Skip to content

Commit c3d6123

Browse files
committed
re-added supports(de)normalization()
1 parent 078f7f3 commit c3d6123

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/Symfony/Component/Serializer/Normalizer/DenormalizerInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,14 @@ interface DenormalizerInterface
2727
* @return object
2828
*/
2929
function denormalize($data, $class, $format = null);
30+
31+
/**
32+
* Checks whether the given class is supported for denormalization by this normalizer
33+
*
34+
* @param mixed $data Data to denormalize from.
35+
* @param string $type The class to which the data should be denormalized.
36+
* @param string $format The format being deserialized from.
37+
* @return Boolean
38+
*/
39+
function supportsDenormalization($data, $type, $format = null);
3040
}

src/Symfony/Component/Serializer/Normalizer/NormalizerInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,13 @@ interface NormalizerInterface
2626
* @return array|scalar
2727
*/
2828
function normalize($object, $format = null);
29+
30+
/**
31+
* Checks whether the given class is supported for normalization by this normalizer
32+
*
33+
* @param mixed $data Data to normalize.
34+
* @param string $format The format being (de-)serialized from or into.
35+
* @return Boolean
36+
*/
37+
function supportsNormalization($data, $format = null);
2938
}

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,66 @@ public function denormalize($data, $type, $format = null)
132132
return $this->denormalizeObject($data, $type, $format);
133133
}
134134

135+
/**
136+
* {@inheritdoc}
137+
*/
138+
public function supportsNormalization($data, $format = null)
139+
{
140+
try {
141+
$this->getNormalizer($format);
142+
} catch (\RuntimeException $e) {
143+
return false;
144+
}
145+
146+
return true;
147+
}
148+
149+
/**
150+
* {@inheritdoc}
151+
*/
152+
public function supportsDenormalization($data, $type, $format = null)
153+
{
154+
try {
155+
$this->getDenormalizer($data, $format = null);
156+
} catch (\RuntimeException $e) {
157+
return false;
158+
}
159+
160+
return true;
161+
}
162+
163+
/**
164+
* {@inheritdoc}
165+
*/
166+
private function getNormalizer($data, $format = null)
167+
{
168+
foreach ($this->normalizers as $normalizer) {
169+
if ($normalizer instanceof NormalizerInterface
170+
&& $normalizer->supportsNormalization($data, $format)
171+
) {
172+
return $normalizer;
173+
}
174+
}
175+
176+
throw new RuntimeException(sprintf('No normalizer found for format "%s".', $format));
177+
}
178+
179+
/**
180+
* {@inheritdoc}
181+
*/
182+
private function getDenormalizer($data, $type, $format = null)
183+
{
184+
foreach ($this->normalizers as $normalizer) {
185+
if ($normalizer instanceof DenormalizerInterface
186+
&& $normalizer->supportsDenormalization($data, $type, $format)
187+
) {
188+
return $normalizer;
189+
}
190+
}
191+
192+
throw new RuntimeException(sprintf('No denormalizer found for format "%s".', $format));
193+
}
194+
135195
/**
136196
* {@inheritdoc}
137197
*/

0 commit comments

Comments
 (0)
0