4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Normalizer;
14 trait ObjectToPopulateTrait
17 * Extract the `object_to_populate` field from the context if it exists
18 * and is an instance of the provided $class.
20 * @param string $class The class the object should be
21 * @param $context The denormalization context
22 * @param string $key They in which to look for the object to populate.
23 * Keeps backwards compatibility with `AbstractNormalizer`.
25 * @return object|null an object if things check out, null otherwise
27 protected function extractObjectToPopulate($class, array $context, $key = null)
29 $key = $key ?: 'object_to_populate';
31 if (isset($context[$key]) && \is_object($context[$key]) && $context[$key] instanceof $class) {
32 return $context[$key];