1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\ORM\Mapping\Factory; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Collection; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
8
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
9
|
|
|
use Doctrine\ORM\PersistentCollection; |
10
|
|
|
|
11
|
|
|
class CollectionFactory |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* This method creates a custom collection for the entity defined by ClassMetadata $class. |
15
|
|
|
* If no custom collection is specified, it will fallback to PersistentCollection. |
16
|
|
|
* The custom collection must extend from PersistentCollection. |
17
|
|
|
* |
18
|
|
|
* @param EntityManagerInterface $em |
19
|
|
|
* @param ClassMetadata $class |
20
|
|
|
* @param Collection $collection the contents of the specific collection to create |
21
|
|
|
* |
22
|
|
|
* @return PersistentCollection |
23
|
|
|
*/ |
24
|
494 |
|
public function create(EntityManagerInterface $em, ClassMetadata $class, Collection $collection) |
25
|
|
|
{ |
26
|
494 |
|
$customCollectionClassName = $class->customCollectionClassName; |
27
|
|
|
|
28
|
494 |
|
if (empty($customCollectionClassName)) { |
29
|
485 |
|
$customCollectionClassName = PersistentCollection::class; |
30
|
9 |
|
} elseif (! is_a($customCollectionClassName, PersistentCollection::class, true)) { |
31
|
3 |
|
throw new ORMInvalidArgumentException( |
32
|
3 |
|
'The custom collection specified for entity ' . $class->getName() |
33
|
3 |
|
. ' (' . $customCollectionClassName . ') must extend ' . PersistentCollection::class . '.' |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
491 |
|
return new $customCollectionClassName($em, $class, $collection); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* This method creates the default collection to be used in the UnitOfWork. |
42
|
|
|
* |
43
|
|
|
* @param EntityManagerInterface $em |
44
|
|
|
* @param ClassMetadata $class |
45
|
|
|
* @param Collection $collection the contents of the specific collection to create |
46
|
|
|
* |
47
|
|
|
* @return PersistentCollection |
48
|
|
|
*/ |
49
|
772 |
|
public function createDefault(EntityManagerInterface $em, ClassMetadata $class, Collection $collection) |
50
|
|
|
{ |
51
|
772 |
|
return new PersistentCollection($em, $class, $collection); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|