@@ -21,26 +21,18 @@ final class EntityRelation
2121 public const MANY_TO_MANY = 'ManyToMany ' ;
2222 public const ONE_TO_ONE = 'OneToOne ' ;
2323
24- private $ type ;
25-
26- private $ owningClass ;
27-
28- private $ inverseClass ;
29-
3024 private $ owningProperty ;
31-
3225 private $ inverseProperty ;
33-
34- private $ isNullable = false ;
35-
36- private $ isSelfReferencing = false ;
37-
38- private $ orphanRemoval = false ;
39-
40- private $ mapInverseRelation = true ;
41-
42- public function __construct (string $ type , string $ owningClass , string $ inverseClass )
43- {
26+ private bool $ isNullable = false ;
27+ private bool $ isSelfReferencing = false ;
28+ private bool $ orphanRemoval = false ;
29+ private bool $ mapInverseRelation = true ;
30+
31+ public function __construct (
32+ private string $ type ,
33+ private string $ owningClass ,
34+ private string $ inverseClass
35+ ) {
4436 if (!\in_array ($ type , self ::getValidRelationTypes ())) {
4537 throw new \Exception (sprintf ('Invalid relation type "%s" ' , $ type ));
4638 }
@@ -49,9 +41,6 @@ public function __construct(string $type, string $owningClass, string $inverseCl
4941 throw new \Exception ('Use ManyToOne instead of OneToMany ' );
5042 }
5143
52- $ this ->type = $ type ;
53- $ this ->owningClass = $ owningClass ;
54- $ this ->inverseClass = $ inverseClass ;
5544 $ this ->isSelfReferencing = $ owningClass === $ inverseClass ;
5645 }
5746
@@ -89,78 +78,60 @@ public static function getValidRelationTypes(): array
8978 ];
9079 }
9180
92- public function getOwningRelation ()
93- {
94- switch ($ this ->getType ()) {
95- case self ::MANY_TO_ONE :
96- return (new RelationManyToOne ())
97- ->setPropertyName ($ this ->owningProperty )
98- ->setTargetClassName ($ this ->inverseClass )
99- ->setTargetPropertyName ($ this ->inverseProperty )
100- ->setIsNullable ($ this ->isNullable )
101- ->setIsSelfReferencing ($ this ->isSelfReferencing )
102- ->setMapInverseRelation ($ this ->mapInverseRelation )
103- ;
104- break ;
105- case self ::MANY_TO_MANY :
106- return (new RelationManyToMany ())
107- ->setPropertyName ($ this ->owningProperty )
108- ->setTargetClassName ($ this ->inverseClass )
109- ->setTargetPropertyName ($ this ->inverseProperty )
110- ->setIsOwning (true )->setMapInverseRelation ($ this ->mapInverseRelation )
111- ->setIsSelfReferencing ($ this ->isSelfReferencing )
112- ;
113- break ;
114- case self ::ONE_TO_ONE :
115- return (new RelationOneToOne ())
116- ->setPropertyName ($ this ->owningProperty )
117- ->setTargetClassName ($ this ->inverseClass )
118- ->setTargetPropertyName ($ this ->inverseProperty )
119- ->setIsNullable ($ this ->isNullable )
120- ->setIsOwning (true )
121- ->setIsSelfReferencing ($ this ->isSelfReferencing )
122- ->setMapInverseRelation ($ this ->mapInverseRelation )
123- ;
124- break ;
125- default :
126- throw new \InvalidArgumentException ('Invalid type ' );
127- }
128- }
129-
130- public function getInverseRelation ()
131- {
132- switch ($ this ->getType ()) {
133- case self ::MANY_TO_ONE :
134- return (new RelationOneToMany ())
135- ->setPropertyName ($ this ->inverseProperty )
136- ->setTargetClassName ($ this ->owningClass )
137- ->setTargetPropertyName ($ this ->owningProperty )
138- ->setOrphanRemoval ($ this ->orphanRemoval )
139- ->setIsSelfReferencing ($ this ->isSelfReferencing )
140- ;
141- break ;
142- case self ::MANY_TO_MANY :
143- return (new RelationManyToMany ())
144- ->setPropertyName ($ this ->inverseProperty )
145- ->setTargetClassName ($ this ->owningClass )
146- ->setTargetPropertyName ($ this ->owningProperty )
147- ->setIsOwning (false )
148- ->setIsSelfReferencing ($ this ->isSelfReferencing )
149- ;
150- break ;
151- case self ::ONE_TO_ONE :
152- return (new RelationOneToOne ())
153- ->setPropertyName ($ this ->inverseProperty )
154- ->setTargetClassName ($ this ->owningClass )
155- ->setTargetPropertyName ($ this ->owningProperty )
156- ->setIsNullable ($ this ->isNullable )
157- ->setIsOwning (false )
158- ->setIsSelfReferencing ($ this ->isSelfReferencing )
159- ;
160- break ;
161- default :
162- throw new \InvalidArgumentException ('Invalid type ' );
163- }
81+ public function getOwningRelation (): RelationManyToMany |RelationOneToOne |RelationManyToOne
82+ {
83+ return match ($ this ->getType ()) {
84+ self ::MANY_TO_ONE => (new RelationManyToOne ())
85+ ->setPropertyName ($ this ->owningProperty )
86+ ->setTargetClassName ($ this ->inverseClass )
87+ ->setTargetPropertyName ($ this ->inverseProperty )
88+ ->setIsNullable ($ this ->isNullable )
89+ ->setIsSelfReferencing ($ this ->isSelfReferencing )
90+ ->setMapInverseRelation ($ this ->mapInverseRelation ),
91+ self ::MANY_TO_MANY => (new RelationManyToMany ())
92+ ->setPropertyName ($ this ->owningProperty )
93+ ->setTargetClassName ($ this ->inverseClass )
94+ ->setTargetPropertyName ($ this ->inverseProperty )
95+ ->setIsOwning (true )->setMapInverseRelation (
96+ $ this ->mapInverseRelation
97+ )
98+ ->setIsSelfReferencing ($ this ->isSelfReferencing ),
99+ self ::ONE_TO_ONE => (new RelationOneToOne ())
100+ ->setPropertyName ($ this ->owningProperty )
101+ ->setTargetClassName ($ this ->inverseClass )
102+ ->setTargetPropertyName ($ this ->inverseProperty )
103+ ->setIsNullable ($ this ->isNullable )
104+ ->setIsOwning (true )
105+ ->setIsSelfReferencing ($ this ->isSelfReferencing )
106+ ->setMapInverseRelation ($ this ->mapInverseRelation ),
107+ default => throw new \InvalidArgumentException ('Invalid type ' ),
108+ };
109+ }
110+
111+ public function getInverseRelation (): RelationManyToMany |RelationOneToOne |RelationOneToMany
112+ {
113+ return match ($ this ->getType ()) {
114+ self ::MANY_TO_ONE => (new RelationOneToMany ())
115+ ->setPropertyName ($ this ->inverseProperty )
116+ ->setTargetClassName ($ this ->owningClass )
117+ ->setTargetPropertyName ($ this ->owningProperty )
118+ ->setOrphanRemoval ($ this ->orphanRemoval )
119+ ->setIsSelfReferencing ($ this ->isSelfReferencing ),
120+ self ::MANY_TO_MANY => (new RelationManyToMany ())
121+ ->setPropertyName ($ this ->inverseProperty )
122+ ->setTargetClassName ($ this ->owningClass )
123+ ->setTargetPropertyName ($ this ->owningProperty )
124+ ->setIsOwning (false )
125+ ->setIsSelfReferencing ($ this ->isSelfReferencing ),
126+ self ::ONE_TO_ONE => (new RelationOneToOne ())
127+ ->setPropertyName ($ this ->inverseProperty )
128+ ->setTargetClassName ($ this ->owningClass )
129+ ->setTargetPropertyName ($ this ->owningProperty )
130+ ->setIsNullable ($ this ->isNullable )
131+ ->setIsOwning (false )
132+ ->setIsSelfReferencing ($ this ->isSelfReferencing ),
133+ default => throw new \InvalidArgumentException ('Invalid type ' ),
134+ };
164135 }
165136
166137 public function getType (): string
@@ -178,7 +149,7 @@ public function getInverseClass(): string
178149 return $ this ->inverseClass ;
179150 }
180151
181- public function getOwningProperty ()
152+ public function getOwningProperty (): string
182153 {
183154 return $ this ->owningProperty ;
184155 }
@@ -203,7 +174,7 @@ public function getMapInverseRelation(): bool
203174 return $ this ->mapInverseRelation ;
204175 }
205176
206- public function setMapInverseRelation (bool $ mapInverseRelation )
177+ public function setMapInverseRelation (bool $ mapInverseRelation ): void
207178 {
208179 if ($ mapInverseRelation && $ this ->inverseProperty ) {
209180 throw new \Exception ('Cannot set setMapInverseRelation() to true when the inverse relation property is set. ' );
0 commit comments