ClassResolverTrait.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Mapping\Factory;
  11. use Symfony\Component\Serializer\Exception\InvalidArgumentException;
  12. /**
  13. * Resolves a class name.
  14. *
  15. * @internal
  16. *
  17. * @author Kévin Dunglas <dunglas@gmail.com>
  18. */
  19. trait ClassResolverTrait
  20. {
  21. /**
  22. * Gets a class name for a given class or instance.
  23. *
  24. * @param object|string $value
  25. *
  26. * @throws InvalidArgumentException If the class does not exist
  27. */
  28. private function getClass($value): string
  29. {
  30. if (\is_string($value)) {
  31. if (!class_exists($value) && !interface_exists($value, false)) {
  32. throw new InvalidArgumentException(sprintf('The class or interface "%s" does not exist.', $value));
  33. }
  34. return ltrim($value, '\\');
  35. }
  36. if (!\is_object($value)) {
  37. throw new InvalidArgumentException(sprintf('Cannot create metadata for non-objects. Got: "%s".', get_debug_type($value)));
  38. }
  39. return \get_class($value);
  40. }
  41. }