1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\DoctrinePHPCRAdminBundle\Guesser; |
15
|
|
|
|
16
|
|
|
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry; |
17
|
|
|
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata; |
18
|
|
|
use Doctrine\ODM\PHPCR\Mapping\MappingException; |
19
|
|
|
use Sonata\AdminBundle\Guesser\TypeGuesserInterface; |
20
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
21
|
|
|
use Sonata\Form\Type\BooleanType; |
22
|
|
|
use Sonata\Form\Type\DatePickerType; |
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
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Guesser for displaying fields. |
30
|
|
|
* |
31
|
|
|
* Form guesses happen in the FormContractor. |
32
|
|
|
*/ |
33
|
|
|
class TypeGuesser implements TypeGuesserInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var ManagerRegistry |
37
|
|
|
*/ |
38
|
|
|
protected $registry; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $cache; |
44
|
|
|
|
45
|
|
|
public function __construct(ManagerRegistry $registry) |
46
|
|
|
{ |
47
|
|
|
$this->registry = $registry; |
48
|
|
|
$this->cache = []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function guessType($class, $property, ModelManagerInterface $modelManager) |
55
|
|
|
{ |
56
|
|
|
if (!$metadata = $this->getMetadata($class)) { |
57
|
|
|
return new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($metadata->hasAssociation($property)) { |
61
|
|
|
$mapping = $metadata->mappings[$property]; |
62
|
|
|
|
63
|
|
|
switch ($mapping['type']) { |
64
|
|
|
case ClassMetadata::MANY_TO_MANY: |
65
|
|
|
case 'referrers': |
66
|
|
|
return new TypeGuess('doctrine_phpcr_many_to_many', [], Guess::HIGH_CONFIDENCE); |
67
|
|
|
|
68
|
|
|
case ClassMetadata::MANY_TO_ONE: |
69
|
|
|
case 'parent': |
70
|
|
|
return new TypeGuess('doctrine_phpcr_many_to_one', [], Guess::HIGH_CONFIDENCE); |
71
|
|
|
|
72
|
|
|
case 'children': |
73
|
|
|
return new TypeGuess('doctrine_phpcr_one_to_many', [], Guess::HIGH_CONFIDENCE); |
74
|
|
|
|
75
|
|
|
case 'child': |
76
|
|
|
return new TypeGuess('doctrine_phpcr_one_to_one', [], Guess::HIGH_CONFIDENCE); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// TODO: missing multivalue support |
81
|
|
|
switch ($metadata->getTypeOfField($property)) { |
82
|
|
|
case 'boolean': |
83
|
|
|
return new TypeGuess(BooleanType::class, [], Guess::HIGH_CONFIDENCE); |
84
|
|
|
case 'date': |
85
|
|
|
return new TypeGuess(DatePickerType::class, [], Guess::HIGH_CONFIDENCE); |
86
|
|
|
|
87
|
|
|
case 'decimal': |
88
|
|
|
case 'double': |
89
|
|
|
return new TypeGuess(TextType::class, [], Guess::MEDIUM_CONFIDENCE); |
90
|
|
|
case 'integer': |
91
|
|
|
case 'long': |
92
|
|
|
return new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE); |
93
|
|
|
case 'string': |
94
|
|
|
return new TypeGuess(TextType::class, [], Guess::HIGH_CONFIDENCE); |
95
|
|
|
case 'binary': |
96
|
|
|
case 'uri': |
97
|
|
|
return new TypeGuess(TextType::class, [], Guess::MEDIUM_CONFIDENCE); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $class |
105
|
|
|
* |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
|
|
protected function getMetadata($class) |
109
|
|
|
{ |
110
|
|
|
if (\array_key_exists($class, $this->cache)) { |
111
|
|
|
return $this->cache[$class]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->cache[$class] = null; |
115
|
|
|
foreach ($this->registry->getManagers() as $dm) { |
116
|
|
|
try { |
117
|
|
|
return $this->cache[$class] = $dm->getClassMetadata($class); |
118
|
|
|
} catch (MappingException $e) { |
119
|
|
|
// not an entity or mapped super class |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|