-
-
Notifications
You must be signed in to change notification settings - Fork 223
/
AbstractFixture.php
130 lines (116 loc) · 3.42 KB
/
AbstractFixture.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
namespace Doctrine\Common\DataFixtures;
use BadMethodCallException;
use Doctrine\Deprecations\Deprecation;
use function assert;
/**
* Abstract Fixture class helps to manage references
* between fixture classes in order to set relations
* among other fixtures
*/
abstract class AbstractFixture implements SharedFixtureInterface
{
/**
* Fixture reference repository
*
* @var ReferenceRepository|null
*/
protected $referenceRepository;
/**
* {@inheritDoc}
*/
public function setReferenceRepository(ReferenceRepository $referenceRepository)
{
$this->referenceRepository = $referenceRepository;
}
private function getReferenceRepository(): ReferenceRepository
{
assert($this->referenceRepository !== null);
return $this->referenceRepository;
}
/**
* Set the reference entry identified by $name
* and referenced to managed $object. If $name
* already is set, it overrides it
*
* @see Doctrine\Common\DataFixtures\ReferenceRepository::setReference
*
* @param string $name
* @param object $object - managed object
*
* @return void
*/
public function setReference($name, $object)
{
$this->getReferenceRepository()->setReference($name, $object);
}
/**
* Set the reference entry identified by $name
* and referenced to managed $object. If $name
* already is set, it throws a
* BadMethodCallException exception
*
* @see Doctrine\Common\DataFixtures\ReferenceRepository::addReference
*
* @param string $name
* @param object $object - managed object
*
* @return void
*
* @throws BadMethodCallException - if repository already has a reference by $name.
*/
public function addReference($name, $object)
{
$this->getReferenceRepository()->addReference($name, $object);
}
/**
* Loads an object using stored reference
* named by $name
*
* @see Doctrine\Common\DataFixtures\ReferenceRepository::getReference
*
* @param string $name
* @psalm-param class-string<T>|null $class
*
* @return object
* @psalm-return ($class is null ? object : T)
*
* @template T of object
*/
public function getReference($name, ?string $class = null)
{
if ($class === null) {
Deprecation::trigger(
'doctrine/data-fixtures',
'https://github.com/doctrine/data-fixtures/pull/409',
'Argument $class of %s() will be mandatory in 2.0.',
__METHOD__,
);
}
return $this->getReferenceRepository()->getReference($name, $class);
}
/**
* Check if an object is stored using reference
* named by $name
*
* @see Doctrine\Common\DataFixtures\ReferenceRepository::hasReference
*
* @param string $name
* @psalm-param class-string $class
*
* @return bool
*/
public function hasReference($name, ?string $class = null)
{
if ($class === null) {
Deprecation::trigger(
'doctrine/data-fixtures',
'https://github.com/doctrine/data-fixtures/pull/409',
'Argument $class of %s() will be mandatory in 2.0.',
__METHOD__,
);
}
return $this->getReferenceRepository()->hasReference($name, $class);
}
}