symfony/symfony — pull request #2539 by ericclemmons (2011-11-02 03:34:52+00:00)
Title: [Serializer] `normalize` should use supported normalizer before Traversable

Bug fix: yes
Feature addition: no
Backwards compatibility break: no (discussion needed)
Symfony2 tests pass: yes
Fixes the following tickets: #2295

This PR fixes a bug where `Serializer` gave precedence to `\Traversable` even if the object implemented `NormalizableInterface`.

I created a private `supportsNormalization($object, $format)` function in `Serializer` using much of the logic in `normalizeObject(...)` so that `normalize` could easily test if an object should use custom normalization instead of being iterated over.

*Note that I do not consider this a BC break as any user who created a `\Traversable` AND `NormalizableInterface` would have never seen the normalization performed, so is likely very rare and undocumented behavior.*

Also, `denormalizeObject` could use the same abstraction that `normalizeObject` underwent, just for similarity in logic.

**There is an alternative way of doing this:**

* Wrap the original `normalizeObject` in a `try...catch(\Exception)` (because there are multiple types of exceptions that could be thrown)
* In the `catch`, if not `\Traversable` then throw the exception. Otherwise, return the traversed object

To me, relying on catching exceptions that can potentially be thrown from the normalization process is incorrect.