| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <[email protected]> |
| 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 string|null $key They in which to look for the object to populate. |
| 22 | * Keeps backwards compatibility with `AbstractNormalizer`. |
| 23 | */ |
| 24 | protected function extractObjectToPopulate(string $class, array $context, ?string $key = null): ?object |
| 25 | { |
| 26 | $key ??= AbstractNormalizer::OBJECT_TO_POPULATE; |
| 27 | |
| 28 | if (isset($context[$key]) && \is_object($context[$key]) && $context[$key] instanceof $class) { |
| 29 | return $context[$key]; |
| 30 | } |
| 31 | |
| 32 | return null; |
| 33 | } |
| 34 | } |