ClassResolverTrait.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 mixed $value
  25. *
  26. * @return string
  27. *
  28. * @throws InvalidArgumentException If the class does not exists
  29. */
  30. private function getClass($value)
  31. {
  32. if (is_string($value)) {
  33. if (!class_exists($value) && !interface_exists($value)) {
  34. throw new InvalidArgumentException(sprintf('The class or interface "%s" does not exist.', $value));
  35. }
  36. return ltrim($value, '\\');
  37. }
  38. if (!is_object($value)) {
  39. throw new InvalidArgumentException(sprintf('Cannot create metadata for non-objects. Got: "%s"', gettype($value)));
  40. }
  41. return get_class($value);
  42. }
  43. }