TypeGuesser.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 Sonata\AdminBundle\Model\ModelManagerInterface;
  17. class TypeGuesser extends AbstractTypeGuesser
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function guessType($class, $property, ModelManagerInterface $modelManager)
  23. {
  24. if (!$ret = $this->getParentMetadataForProperty($class, $property, $modelManager)) {
  25. return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
  26. }
  27. list($metadata, $propertyName, $parentAssociationMappings) = $ret;
  28. if ($metadata->hasAssociation($propertyName)) {
  29. $mapping = $metadata->getAssociationMapping($propertyName);
  30. switch ($mapping['type']) {
  31. case ClassMetadataInfo::ONE_TO_MANY:
  32. return new TypeGuess('orm_one_to_many', array(), Guess::HIGH_CONFIDENCE);
  33. case ClassMetadataInfo::MANY_TO_MANY:
  34. return new TypeGuess('orm_many_to_many', array(), Guess::HIGH_CONFIDENCE);
  35. case ClassMetadataInfo::MANY_TO_ONE:
  36. return new TypeGuess('orm_many_to_one', array(), Guess::HIGH_CONFIDENCE);
  37. case ClassMetadataInfo::ONE_TO_ONE:
  38. return new TypeGuess('orm_one_to_one', array(), Guess::HIGH_CONFIDENCE);
  39. }
  40. }
  41. switch ($metadata->getTypeOfField($propertyName))
  42. {
  43. case 'array':
  44. return new TypeGuess('array', array(), Guess::HIGH_CONFIDENCE);
  45. case 'boolean':
  46. return new TypeGuess('boolean', array(), Guess::HIGH_CONFIDENCE);
  47. case 'datetime':
  48. case 'vardatetime':
  49. case 'datetimetz':
  50. return new TypeGuess('datetime', array(), Guess::HIGH_CONFIDENCE);
  51. case 'date':
  52. return new TypeGuess('date', array(), Guess::HIGH_CONFIDENCE);
  53. case 'decimal':
  54. case 'float':
  55. return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE);
  56. case 'integer':
  57. case 'bigint':
  58. case 'smallint':
  59. return new TypeGuess('integer', array(), Guess::MEDIUM_CONFIDENCE);
  60. case 'string':
  61. return new TypeGuess('text', array(), Guess::MEDIUM_CONFIDENCE);
  62. case 'text':
  63. return new TypeGuess('textarea', array(), Guess::MEDIUM_CONFIDENCE);
  64. case 'time':
  65. return new TypeGuess('time', array(), Guess::HIGH_CONFIDENCE);
  66. default:
  67. return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
  68. }
  69. }
  70. }