TypeGuesser.php 3.1 KB

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