FakeMetadataFactory.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\Validator\Tests\Fixtures;
  11. use Symfony\Component\Validator\Exception\NoSuchMetadataException;
  12. use Symfony\Component\Validator\MetadataFactoryInterface;
  13. use Symfony\Component\Validator\MetadataInterface;
  14. class FakeMetadataFactory implements MetadataFactoryInterface
  15. {
  16. protected $metadatas = array();
  17. public function getMetadataFor($class)
  18. {
  19. $hash = null;
  20. if (\is_object($class)) {
  21. $hash = spl_object_hash($class);
  22. $class = \get_class($class);
  23. }
  24. if (!\is_string($class)) {
  25. throw new NoSuchMetadataException(sprintf('No metadata for type %s', \gettype($class)));
  26. }
  27. if (!isset($this->metadatas[$class])) {
  28. if (isset($this->metadatas[$hash])) {
  29. return $this->metadatas[$hash];
  30. }
  31. throw new NoSuchMetadataException(sprintf('No metadata for "%s"', $class));
  32. }
  33. return $this->metadatas[$class];
  34. }
  35. public function hasMetadataFor($class)
  36. {
  37. $hash = null;
  38. if (\is_object($class)) {
  39. $hash = spl_object_hash($class);
  40. $class = \get_class($class);
  41. }
  42. if (!\is_string($class)) {
  43. return false;
  44. }
  45. return isset($this->metadatas[$class]) || isset($this->metadatas[$hash]);
  46. }
  47. public function addMetadata($metadata)
  48. {
  49. $this->metadatas[$metadata->getClassName()] = $metadata;
  50. }
  51. public function addMetadataForValue($value, MetadataInterface $metadata)
  52. {
  53. $key = \is_object($value) ? spl_object_hash($value) : $value;
  54. $this->metadatas[$key] = $metadata;
  55. }
  56. }