TypeGuesser.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\DoctrineORMAdminBundle\Guesser;
  11. use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
  12. use Symfony\Bridge\Doctrine\RegistryInterface;
  13. use Symfony\Component\Form\Guess\Guess;
  14. use Symfony\Component\Form\Guess\TypeGuess;
  15. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  16. use Doctrine\ORM\Mapping\MappingException;
  17. class TypeGuesser implements TypeGuesserInterface
  18. {
  19. protected $registry;
  20. private $cache;
  21. public function __construct(RegistryInterface $registry)
  22. {
  23. $this->registry = $registry;
  24. $this->cache = array();
  25. }
  26. /**
  27. * @param string $class
  28. * @param string $property
  29. * @return TypeGuess
  30. */
  31. public function guessType($class, $property)
  32. {
  33. if (!$ret = $this->getMetadata($class)) {
  34. return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
  35. }
  36. list($metadata, $name) = $ret;
  37. if ($metadata->hasAssociation($property)) {
  38. $mapping = $metadata->getAssociationMapping($property);
  39. switch ($mapping['type']) {
  40. case ClassMetadataInfo::ONE_TO_MANY:
  41. return new TypeGuess('orm_one_to_many', array(), Guess::HIGH_CONFIDENCE);
  42. case ClassMetadataInfo::MANY_TO_MANY:
  43. return new TypeGuess('orm_many_to_many', array(), Guess::HIGH_CONFIDENCE);
  44. case ClassMetadataInfo::MANY_TO_ONE:
  45. return new TypeGuess('orm_many_to_one', array(), Guess::HIGH_CONFIDENCE);
  46. case ClassMetadataInfo::ONE_TO_ONE:
  47. return new TypeGuess('orm_one_to_one', array(), Guess::HIGH_CONFIDENCE);
  48. }
  49. }
  50. switch ($metadata->getTypeOfField($property))
  51. {
  52. case 'array':
  53. return new TypeGuess('array', array(), Guess::HIGH_CONFIDENCE);
  54. case 'boolean':
  55. return new TypeGuess('boolean', array(), Guess::HIGH_CONFIDENCE);
  56. case 'datetime':
  57. case 'vardatetime':
  58. case 'datetimetz':
  59. return new TypeGuess('datetime', array(), Guess::HIGH_CONFIDENCE);
  60. case 'date':
  61. return new TypeGuess('date', array(), Guess::HIGH_CONFIDENCE);
  62. case 'decimal':
  63. case 'float':
  64. return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE);
  65. case 'integer':
  66. case 'bigint':
  67. case 'smallint':
  68. return new TypeGuess('integer', array(), Guess::MEDIUM_CONFIDENCE);
  69. case 'string':
  70. return new TypeGuess('text', array(), Guess::MEDIUM_CONFIDENCE);
  71. case 'text':
  72. return new TypeGuess('textarea', array(), Guess::MEDIUM_CONFIDENCE);
  73. case 'time':
  74. return new TypeGuess('time', array(), Guess::HIGH_CONFIDENCE);
  75. default:
  76. return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
  77. }
  78. }
  79. protected function getMetadata($class)
  80. {
  81. if (array_key_exists($class, $this->cache)) {
  82. return $this->cache[$class];
  83. }
  84. $this->cache[$class] = null;
  85. foreach ($this->registry->getEntityManagers() as $name => $em) {
  86. try {
  87. return $this->cache[$class] = array($em->getClassMetadata($class), $name);
  88. } catch (MappingException $e) {
  89. // not an entity or mapped super class
  90. }
  91. }
  92. }
  93. }