1010namespace OCA \ProfileFields \Service ;
1111
1212use DateTime ;
13+ use DateTimeInterface ;
1314use InvalidArgumentException ;
1415use JsonException ;
1516use OCA \ProfileFields \Db \FieldDefinition ;
@@ -27,7 +28,7 @@ public function __construct(
2728 /**
2829 * @param array<string, mixed>|scalar|null $rawValue
2930 */
30- public function upsert (FieldDefinition $ definition , string $ userUid , array |string |int |float |bool |null $ rawValue , string $ updatedByUid , ?string $ currentVisibility = null , ?DateTime $ updatedAt = null ): FieldValue {
31+ public function upsert (FieldDefinition $ definition , string $ userUid , array |string |int |float |bool |null $ rawValue , string $ updatedByUid , ?string $ currentVisibility = null , ?DateTimeInterface $ updatedAt = null ): FieldValue {
3132 $ normalizedValue = $ this ->normalizeValue ($ definition , $ rawValue );
3233 $ visibility = $ currentVisibility ?? $ definition ->getInitialVisibility ();
3334 if (!FieldVisibility::isValid ($ visibility )) {
@@ -40,7 +41,7 @@ public function upsert(FieldDefinition $definition, string $userUid, array|strin
4041 $ entity ->setValueJson ($ this ->encodeValue ($ normalizedValue ));
4142 $ entity ->setCurrentVisibility ($ visibility );
4243 $ entity ->setUpdatedByUid ($ updatedByUid );
43- $ entity ->setUpdatedAt ($ updatedAt ?? new DateTime ( ));
44+ $ entity ->setUpdatedAt ($ this -> asMutableDateTime ( $ updatedAt ));
4445
4546 if ($ entity ->getId () === null ) {
4647 return $ this ->fieldValueMapper ->insert ($ entity );
@@ -101,7 +102,7 @@ public function updateVisibility(FieldDefinition $definition, string $userUid, s
101102
102103 $ entity ->setCurrentVisibility ($ currentVisibility );
103104 $ entity ->setUpdatedByUid ($ updatedByUid );
104- $ entity ->setUpdatedAt (new DateTime ());
105+ $ entity ->setUpdatedAt ($ this -> asMutableDateTime ());
105106
106107 return $ this ->fieldValueMapper ->update ($ entity );
107108 }
@@ -180,4 +181,16 @@ private function decodeValue(string $valueJson): array {
180181
181182 return $ decoded ;
182183 }
184+
185+ private function asMutableDateTime (?DateTimeInterface $ value = null ): DateTime {
186+ if ($ value instanceof DateTime) {
187+ return clone $ value ;
188+ }
189+
190+ if ($ value !== null ) {
191+ return DateTime::createFromInterface ($ value );
192+ }
193+
194+ return new DateTime ();
195+ }
183196}
0 commit comments