|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ajir\RabbitMqSqlBundle\Factory; |
|
3
|
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
|
5
|
|
|
use Ajir\RabbitMqSqlBundle\DataMapper\DataMapper; |
|
6
|
|
|
use Ajir\RabbitMqSqlBundle\Model\ManyToManyRelation; |
|
7
|
|
|
use Ajir\RabbitMqSqlBundle\Model\ManyToOneRelation; |
|
8
|
|
|
use Ajir\RabbitMqSqlBundle\Model\OneToManyRelation; |
|
9
|
|
|
use Ajir\RabbitMqSqlBundle\Model\OneToOneRelation; |
|
10
|
|
|
use ReflectionClass; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class RelationFactory |
|
14
|
|
|
* |
|
15
|
|
|
* @author Florian Ajir <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class RelationFactory implements RelationFactoryInterface |
|
18
|
|
|
{ |
|
19
|
|
|
const RELATION_FACTORY = 'Ajir\RabbitMqSqlBundle\Model\RelationInterface'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var ReflectionClass |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $oneToOneClass; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ReflectionClass |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $oneToManyClass; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ReflectionClass |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $manyToOneClass; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var ReflectionClass |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $manyToManyClass; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $oneToOneClassName |
|
43
|
|
|
* @param string $oneToManyClassName |
|
44
|
|
|
* @param string $manyToOneClassName |
|
45
|
|
|
* @param string $manyToManyClassName |
|
46
|
|
|
*/ |
|
47
|
8 |
|
public function __construct( |
|
48
|
|
|
$oneToOneClassName, |
|
49
|
|
|
$oneToManyClassName, |
|
50
|
|
|
$manyToOneClassName, |
|
51
|
|
|
$manyToManyClassName |
|
52
|
|
|
) { |
|
53
|
8 |
|
$this->oneToOneClass = $this->createRelation($oneToOneClassName); |
|
54
|
7 |
|
$this->oneToManyClass = $this->createRelation($oneToManyClassName); |
|
55
|
7 |
|
$this->manyToOneClass = $this->createRelation($manyToOneClassName); |
|
56
|
7 |
|
$this->manyToManyClass = $this->createRelation($manyToManyClassName); |
|
57
|
7 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $relationClassName |
|
61
|
|
|
* |
|
62
|
|
|
* @return ReflectionClass |
|
63
|
|
|
*/ |
|
64
|
8 |
|
private function createRelation($relationClassName) |
|
65
|
|
|
{ |
|
66
|
8 |
|
$relation = new ReflectionClass($relationClassName); |
|
67
|
8 |
|
if (!$relation->implementsInterface(self::RELATION_FACTORY)) { |
|
68
|
1 |
|
throw new InvalidArgumentException(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
7 |
|
return $relation; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $relation |
|
76
|
|
|
* @param array $data |
|
77
|
|
|
* |
|
78
|
|
|
* @return ManyToManyRelation|ManyToOneRelation|OneToManyRelation|OneToOneRelation |
|
79
|
|
|
* |
|
80
|
|
|
* @throws InvalidArgumentException |
|
81
|
|
|
*/ |
|
82
|
6 |
|
public function create($relation, $data) |
|
83
|
|
|
{ |
|
84
|
|
|
switch ($relation) { |
|
85
|
6 |
|
case DataMapper::RELATION_ONE_TO_ONE: |
|
86
|
2 |
|
return $this->createOneToOne($data); |
|
87
|
|
|
|
|
88
|
|
|
case DataMapper::RELATION_ONE_TO_MANY: |
|
89
|
2 |
|
return $this->createOneToMany($data); |
|
90
|
|
|
|
|
91
|
|
|
case DataMapper::RELATION_MANY_TO_ONE: |
|
92
|
2 |
|
return $this->createManyToOne($data); |
|
93
|
|
|
|
|
94
|
1 |
|
case DataMapper::RELATION_MANY_TO_MANY: |
|
95
|
2 |
|
return $this->createManyToMany($data); |
|
96
|
|
|
|
|
97
|
|
|
default: |
|
98
|
1 |
|
throw new InvalidArgumentException("Invalid relation : $relation"); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param array $data |
|
104
|
|
|
* |
|
105
|
|
|
* @return OneToOneRelation |
|
106
|
|
|
*/ |
|
107
|
2 |
|
protected function createOneToOne($data) |
|
108
|
|
|
{ |
|
109
|
2 |
|
return $this->oneToOneClass->newInstance($data); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param array $data |
|
114
|
|
|
* |
|
115
|
|
|
* @return OneToManyRelation |
|
116
|
|
|
*/ |
|
117
|
2 |
|
protected function createOneToMany($data) |
|
118
|
|
|
{ |
|
119
|
2 |
|
return $this->oneToManyClass->newInstance($data); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param array $data |
|
124
|
|
|
* |
|
125
|
|
|
* @return ManyToOneRelation |
|
126
|
|
|
*/ |
|
127
|
2 |
|
protected function createManyToOne($data) |
|
128
|
|
|
{ |
|
129
|
2 |
|
return $this->manyToOneClass->newInstance($data); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param array $data |
|
134
|
|
|
* |
|
135
|
|
|
* @return ManyToManyRelation |
|
136
|
|
|
*/ |
|
137
|
2 |
|
protected function createManyToMany($data) |
|
138
|
|
|
{ |
|
139
|
2 |
|
return $this->manyToManyClass->newInstance($data); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|