ac647889168bdb7e22a169fdd67f9c72416c8954
[yaffs-website] / vendor / symfony / serializer / Normalizer / ObjectToPopulateTrait.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Serializer\Normalizer;
13
14 trait ObjectToPopulateTrait
15 {
16     /**
17      * Extract the `object_to_populate` field from the context if it exists
18      * and is an instance of the provided $class.
19      *
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`.
24      *
25      * @return object|null an object if things check out, null otherwise
26      */
27     protected function extractObjectToPopulate($class, array $context, $key = null)
28     {
29         $key = $key ?: 'object_to_populate';
30
31         if (isset($context[$key]) && is_object($context[$key]) && $context[$key] instanceof $class) {
32             return $context[$key];
33         }
34
35         return null;
36     }
37 }