4
4
5
5
use ApiPlatform \Validator \Exception \ValidationException ;
6
6
use App \Agent \Domain \Model \Agent ;
7
- use App \FieldHolder \Community \Domain \Model \Community ;
8
- use App \FieldHolder \Community \Domain \Repository \CommunityRepositoryInterface ;
9
7
use App \Field \Domain \Enum \FieldCommunity ;
10
8
use App \Field \Domain \Enum \FieldPlace ;
11
9
use App \Field \Domain \Exception \FieldEntityNotFoundException ;
12
10
use App \Field \Domain \Exception \FieldInvalidNameException ;
13
11
use App \Field \Domain \Exception \FieldUnicityViolationException ;
14
12
use App \Field \Domain \Model \Field ;
15
13
use App \Field \Domain \Repository \FieldRepositoryInterface ;
14
+ use App \FieldHolder \Community \Domain \Model \Community ;
15
+ use App \FieldHolder \Community \Domain \Repository \CommunityRepositoryInterface ;
16
16
use App \FieldHolder \Place \Domain \Model \Place ;
17
17
use App \FieldHolder \Place \Domain \Repository \PlaceRepositoryInterface ;
18
18
use Symfony \Bundle \SecurityBundle \Security ;
@@ -39,7 +39,6 @@ public function __construct(
39
39
*/
40
40
public function upsertFields (Place |Community $ entity , array $ fieldPayloads ): void
41
41
{
42
-
43
42
/** @var Agent $agent */
44
43
$ agent = $ this ->security ->getUser ();
45
44
@@ -49,6 +48,11 @@ public function upsertFields(Place|Community $entity, array $fieldPayloads): voi
49
48
Community::class => FieldCommunity::tryFrom ($ fieldPayload ->name ),
50
49
default => null ,
51
50
};
51
+ $ aliases = match ($ entity ::class) {
52
+ Place::class => FieldPlace::ALIASES ,
53
+ Community::class => FieldCommunity::ALIASES ,
54
+ default => null ,
55
+ };
52
56
if (null === $ enumValue ) {
53
57
throw new FieldInvalidNameException ($ fieldPayload ->name );
54
58
}
@@ -61,21 +65,21 @@ public function upsertFields(Place|Community $entity, array $fieldPayloads): voi
61
65
$ value = $ this ->maybeTransformEntities ($ enumValue , $ fieldPayload ->value );
62
66
if (Community::class === $ entity ::class) {
63
67
$ field ->community = $ entity ;
64
- if (FieldCommunity::PARENT_WIKIDATA_ID === $ enumValue ) {
65
- $ fieldPayload ->name = FieldCommunity::PARENT_COMMUNITY_ID ->value ;
66
- }
67
68
} else {
68
69
$ field ->place = $ entity ;
69
70
}
70
71
72
+ if (in_array ($ enumValue ->name , array_keys ($ aliases ))) {
73
+ $ fieldPayload ->name = $ aliases [$ enumValue ->name ]->value ;
74
+ }
71
75
$ field ->name = $ fieldPayload ->name ;
72
76
$ field ->value = $ value ;
73
77
$ field ->engine = $ fieldPayload ->engine ;
74
78
$ field ->reliability = $ fieldPayload ->reliability ;
75
79
$ field ->source = $ fieldPayload ->source ;
76
80
$ field ->explanation = $ fieldPayload ->explanation ;
77
81
$ field ->touch ();
78
-
82
+
79
83
// Unique constraints validation (TODO use custom Assert instead)
80
84
if (null !== $ field ->value
81
85
&& in_array ($ field ->name , Field::UNIQUE_CONSTRAINTS , true )
@@ -148,8 +152,7 @@ private function maybeTransformEntities(FieldCommunity|FieldPlace $nameEnum, mix
148
152
149
153
if ($ nameEnum ->name === FieldPlace::PARENT_WIKIDATA_IDS ->name ) {
150
154
$ instances = $ repo ->withWikidataIds ($ value )->asCollection ();
151
- }
152
- else {
155
+ } else {
153
156
$ instances = $ repo ->ofIds (array_map (fn (string $ id ) => UuidV7::fromString ($ id ), $ value ))->asCollection ();
154
157
}
155
158
@@ -163,8 +166,7 @@ private function maybeTransformEntities(FieldCommunity|FieldPlace $nameEnum, mix
163
166
if ($ nameEnum ->name === FieldCommunity::PARENT_WIKIDATA_ID ->name ) {
164
167
assert (is_int ($ value ));
165
168
$ instance = $ repo ->withWikidataId ($ value )->asCollection ()[0 ];
166
- }
167
- else {
169
+ } else {
168
170
// That's an object
169
171
assert (is_string ($ value ));
170
172
$ instance = $ repo ->ofId (Uuid::fromString ($ value ));
0 commit comments