/src/Guesser/FilterTypeGuesser.php

https://github.com/sonata-project/SonataDoctrinePhpcrAdminBundle · PHP · 141 lines · 94 code · 22 blank · 25 comment · 6 complexity · cd5d41b7f961d4ebdcaa9c2d13722574 MD5 · raw file

  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the Sonata Project package.
  5. *
  6. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Sonata\DoctrinePHPCRAdminBundle\Guesser;
  12. use Doctrine\Bundle\PHPCRBundle\Form\Type\DocumentType;
  13. use Doctrine\Bundle\PHPCRBundle\ManagerRegistry;
  14. use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
  15. use Doctrine\ODM\PHPCR\Mapping\MappingException;
  16. use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
  17. use Sonata\AdminBundle\Model\ModelManagerInterface;
  18. use Sonata\DoctrinePHPCRAdminBundle\Filter\BooleanFilter;
  19. use Sonata\DoctrinePHPCRAdminBundle\Filter\DateFilter;
  20. use Sonata\DoctrinePHPCRAdminBundle\Filter\NumberFilter;
  21. use Sonata\DoctrinePHPCRAdminBundle\Filter\StringFilter;
  22. use Sonata\Form\Type\BooleanType;
  23. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  24. use Symfony\Component\Form\Extension\Core\Type\TextType;
  25. use Symfony\Component\Form\Guess\Guess;
  26. use Symfony\Component\Form\Guess\TypeGuess;
  27. class FilterTypeGuesser implements TypeGuesserInterface
  28. {
  29. /**
  30. * @var ManagerRegistry
  31. */
  32. protected $registry;
  33. /**
  34. * @var array
  35. */
  36. private $cache;
  37. public function __construct(ManagerRegistry $registry)
  38. {
  39. $this->registry = $registry;
  40. $this->cache = [];
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function guessType($class, $property, ModelManagerInterface $modelManager)
  46. {
  47. if (!$metadata = $this->getMetadata($class)) {
  48. return false;
  49. }
  50. $options = [
  51. 'field_type' => TextType::class,
  52. 'field_options' => [],
  53. 'options' => [],
  54. ];
  55. if ($metadata->hasAssociation($property)) {
  56. // TODO add support for children, child, referrers and parentDocument associations
  57. $mapping = $metadata->mappings[$property];
  58. $options['operator_type'] = BooleanType::class;
  59. $options['operator_options'] = [];
  60. $options['field_type'] = DocumentType::class;
  61. if (!empty($mapping['targetDocument'])) {
  62. $options['field_options'] = [
  63. 'class' => $mapping['targetDocument'],
  64. ];
  65. }
  66. $options['field_name'] = $mapping['fieldName'];
  67. $options['mapping_type'] = $mapping['type'];
  68. switch ($mapping['type']) {
  69. case ClassMetadata::MANY_TO_MANY:
  70. return new TypeGuess('doctrine_phpcr_many_to_many', $options, Guess::HIGH_CONFIDENCE);
  71. case ClassMetadata::MANY_TO_ONE:
  72. return new TypeGuess('doctrine_phpcr_many_to_one', $options, Guess::HIGH_CONFIDENCE);
  73. }
  74. }
  75. // TODO add support for node, nodename, version created, version name
  76. $options['field_name'] = $property;
  77. switch ($metadata->getTypeOfField($property)) {
  78. case 'boolean':
  79. $options['field_type'] = BooleanType::class;
  80. $options['field_options'] = [];
  81. return new TypeGuess(BooleanFilter::class, $options, Guess::HIGH_CONFIDENCE);
  82. case 'date':
  83. return new TypeGuess(DateFilter::class, $options, Guess::HIGH_CONFIDENCE);
  84. case 'decimal':
  85. case 'float':
  86. return new TypeGuess(NumberFilter::class, $options, Guess::HIGH_CONFIDENCE);
  87. case 'integer':
  88. $options['field_type'] = NumberType::class;
  89. $options['field_options'] = [
  90. 'csrf_protection' => false,
  91. ];
  92. return new TypeGuess(NumberFilter::class, $options, Guess::HIGH_CONFIDENCE);
  93. case 'text':
  94. case 'string':
  95. $options['field_type'] = TextType::class;
  96. return new TypeGuess(StringFilter::class, $options, Guess::HIGH_CONFIDENCE);
  97. }
  98. return new TypeGuess(StringFilter::class, $options, Guess::LOW_CONFIDENCE);
  99. }
  100. /**
  101. * @param string $class
  102. *
  103. * @return mixed
  104. */
  105. protected function getMetadata($class)
  106. {
  107. if (\array_key_exists($class, $this->cache)) {
  108. return $this->cache[$class];
  109. }
  110. $this->cache[$class] = null;
  111. foreach ($this->registry->getManagers() as $dm) {
  112. try {
  113. return $this->cache[$class] = $dm->getClassMetadata($class);
  114. } catch (MappingException $e) {
  115. // not an entity or mapped super class
  116. }
  117. }
  118. }
  119. }