1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Doctrine MongoDBBundle |
5
|
|
|
* |
6
|
|
|
* The code was originally distributed inside the Symfony framework. |
7
|
|
|
* |
8
|
|
|
* (c) Fabien Potencier <[email protected]> |
9
|
|
|
* (c) Doctrine Project |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Saxulum\DoctrineMongodbOdmManagerRegistry\Form\Type; |
16
|
|
|
|
17
|
|
|
use Saxulum\DoctrineMongodbOdmManagerRegistry\Form\ChoiceList\MongoDBQueryBuilderLoader; |
18
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
19
|
|
|
use Symfony\Bridge\Doctrine\Form\Type\DoctrineType; |
20
|
|
|
use Symfony\Component\OptionsResolver\Options; |
21
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Form type for a MongoDB document |
25
|
|
|
* |
26
|
|
|
* @author Thibault Duplessis <[email protected]> |
27
|
|
|
* @author Christophe Coevoet <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class DocumentType extends DoctrineType |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @see Symfony\Bridge\Doctrine\Form\Type\DoctrineType::getLoader() |
33
|
|
|
*/ |
34
|
|
|
public function getLoader(ObjectManager $manager, $queryBuilder, $class) |
35
|
|
|
{ |
36
|
|
|
return new MongoDBQueryBuilderLoader( |
37
|
|
|
$queryBuilder, |
38
|
|
|
$manager, |
39
|
|
|
$class |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param OptionsResolver $resolver |
45
|
|
|
*/ |
46
|
|
|
public function configureOptions(OptionsResolver $resolver) |
47
|
|
|
{ |
48
|
|
|
parent::configureOptions($resolver); |
49
|
|
|
|
50
|
|
|
$resolver->setDefaults(array( |
51
|
|
|
'document_manager' => null, |
52
|
|
|
)); |
53
|
|
|
|
54
|
|
|
$registry = $this->registry; |
55
|
|
|
$normalizer = function (Options $options, $manager) use ($registry) { |
56
|
|
|
if (isset($options['document_manager']) && $manager) { |
57
|
|
|
throw new \InvalidArgumentException('You cannot set both an "em" and "document_manager" option.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$manager = $options['document_manager'] ?: $manager; |
61
|
|
|
|
62
|
|
|
if (null === $manager) { |
63
|
|
|
return $registry->getManagerForClass($options['class']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($manager instanceof ObjectManager) { |
67
|
|
|
return $manager; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $registry->getManager($manager); |
71
|
|
|
}; |
72
|
|
|
|
73
|
|
|
$resolver->setNormalizer('em', $normalizer); |
74
|
|
|
|
75
|
|
|
$resolver->setAllowedTypes('document_manager', array('null', 'string', 'Doctrine\ODM\MongoDB\DocumentManager')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
* |
81
|
|
|
* @internal Symfony 2.8 compatibility |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getBlockPrefix() |
86
|
|
|
{ |
87
|
|
|
return 'document'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
* |
93
|
|
|
* @internal Symfony 2.7 compatibility |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getName() |
98
|
|
|
{ |
99
|
|
|
return $this->getBlockPrefix(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|