@@ -46,7 +46,8 @@ class FilesMetadata implements IFilesMetadata {
4646 private string $ syncToken = '' ;
4747
4848 public function __construct (
49- private int $ fileId = 0
49+ private int $ fileId = 0 ,
50+ private string $ fileEtag = '' ,
5051 ) {
5152 }
5253
@@ -59,6 +60,15 @@ public function getFileId(): int {
5960 return $ this ->fileId ;
6061 }
6162
63+ /**
64+ * @inheritDoc
65+ * @return string related file etag
66+ * @since 29.0.0
67+ */
68+ public function getFileEtag (): string {
69+ return $ this ->fileEtag ;
70+ }
71+
6272 /**
6373 * @inheritDoc
6474 * @return int timestamp
@@ -156,6 +166,35 @@ public function setEditPermission(string $key, int $permission): void {
156166 $ this ->metadata [$ key ]->setEditPermission ($ permission );
157167 }
158168
169+
170+ public function getEtag (string $ key ): string {
171+ if (!array_key_exists ($ key , $ this ->metadata )) {
172+ throw new FilesMetadataNotFoundException ();
173+ }
174+
175+ return $ this ->metadata [$ key ]->getEtag ();
176+ }
177+
178+ public function setEtag (string $ key , string $ etag ): void {
179+ if (!array_key_exists ($ key , $ this ->metadata )) {
180+ throw new FilesMetadataNotFoundException ();
181+ }
182+
183+ $ this ->metadata [$ key ]->setEtag ($ etag );
184+ }
185+
186+ /**
187+ * @param string $key metadata key
188+ *
189+ * @inheritDoc
190+ * @return bool
191+ * @since 29.0.0
192+ */
193+ public function isUpToDate (string $ key ): bool {
194+ return ($ this ->fileEtag !== '' && $ this ->hasKey ($ key ) && $ this ->metadata [$ key ]->getEtag () === $ this ->getFileEtag ());
195+ }
196+
197+
159198 /**
160199 * @param string $key metadata key
161200 *
@@ -311,16 +350,16 @@ public function getType(string $key): string {
311350 public function setString (string $ key , string $ value , bool $ index = false ): IFilesMetadata {
312351 $ this ->confirmKeyFormat ($ key );
313352 try {
314- if ($ this ->getString ($ key ) === $ value && $ index === $ this ->isIndex ($ key )) {
315- return $ this ; // we ignore if value and index have not changed
353+ if ($ this ->getString ($ key ) === $ value && $ index === $ this ->isIndex ($ key ) && $ this -> isUpToDate ( $ key ) ) {
354+ return $ this ; // we ignore if value and index have not changed and etag is still the same
316355 }
317356 } catch (FilesMetadataNotFoundException |FilesMetadataTypeException $ e ) {
318357 // if value does not exist, or type has changed, we keep on the writing
319358 }
320359
321360 $ meta = new MetadataValueWrapper (IMetadataValueWrapper::TYPE_STRING );
322361 $ this ->updated = true ;
323- $ this ->metadata [$ key ] = $ meta ->setValueString ($ value )->setIndexed ($ index );
362+ $ this ->metadata [$ key ] = $ meta ->setValueString ($ value )->setIndexed ($ index )-> setEtag ( $ this -> getFileEtag ()) ;
324363
325364 return $ this ;
326365 }
@@ -338,15 +377,15 @@ public function setString(string $key, string $value, bool $index = false): IFil
338377 public function setInt (string $ key , int $ value , bool $ index = false ): IFilesMetadata {
339378 $ this ->confirmKeyFormat ($ key );
340379 try {
341- if ($ this ->getInt ($ key ) === $ value && $ index === $ this ->isIndex ($ key )) {
342- return $ this ; // we ignore if value have not changed
380+ if ($ this ->getInt ($ key ) === $ value && $ index === $ this ->isIndex ($ key ) && $ this -> isUpToDate ( $ key ) ) {
381+ return $ this ; // we ignore if value and index have not changed and etag is still the same
343382 }
344383 } catch (FilesMetadataNotFoundException |FilesMetadataTypeException $ e ) {
345384 // if value does not exist, or type has changed, we keep on the writing
346385 }
347386
348387 $ meta = new MetadataValueWrapper (IMetadataValueWrapper::TYPE_INT );
349- $ this ->metadata [$ key ] = $ meta ->setValueInt ($ value )->setIndexed ($ index );
388+ $ this ->metadata [$ key ] = $ meta ->setValueInt ($ value )->setIndexed ($ index )-> setEtag ( $ this -> getFileEtag ()) ;
350389 $ this ->updated = true ;
351390
352391 return $ this ;
@@ -364,15 +403,15 @@ public function setInt(string $key, int $value, bool $index = false): IFilesMeta
364403 public function setFloat (string $ key , float $ value , bool $ index = false ): IFilesMetadata {
365404 $ this ->confirmKeyFormat ($ key );
366405 try {
367- if ($ this ->getFloat ($ key ) === $ value && $ index === $ this ->isIndex ($ key )) {
368- return $ this ; // we ignore if value have not changed
406+ if ($ this ->getFloat ($ key ) === $ value && $ index === $ this ->isIndex ($ key ) && $ this -> isUpToDate ( $ key ) ) {
407+ return $ this ; // we ignore if value and index have not changed and etag is still the same
369408 }
370409 } catch (FilesMetadataNotFoundException |FilesMetadataTypeException $ e ) {
371410 // if value does not exist, or type has changed, we keep on the writing
372411 }
373412
374413 $ meta = new MetadataValueWrapper (IMetadataValueWrapper::TYPE_FLOAT );
375- $ this ->metadata [$ key ] = $ meta ->setValueFloat ($ value )->setIndexed ($ index );
414+ $ this ->metadata [$ key ] = $ meta ->setValueFloat ($ value )->setIndexed ($ index )-> setEtag ( $ this -> getFileEtag ()) ;
376415 $ this ->updated = true ;
377416
378417 return $ this ;
@@ -392,15 +431,15 @@ public function setFloat(string $key, float $value, bool $index = false): IFiles
392431 public function setBool (string $ key , bool $ value , bool $ index = false ): IFilesMetadata {
393432 $ this ->confirmKeyFormat ($ key );
394433 try {
395- if ($ this ->getBool ($ key ) === $ value && $ index === $ this ->isIndex ($ key )) {
396- return $ this ; // we ignore if value have not changed
434+ if ($ this ->getBool ($ key ) === $ value && $ index === $ this ->isIndex ($ key ) && $ this -> isUpToDate ( $ key ) ) {
435+ return $ this ; // we ignore if value and index have not changed and etag is still the same
397436 }
398437 } catch (FilesMetadataNotFoundException |FilesMetadataTypeException $ e ) {
399438 // if value does not exist, or type has changed, we keep on the writing
400439 }
401440
402441 $ meta = new MetadataValueWrapper (IMetadataValueWrapper::TYPE_BOOL );
403- $ this ->metadata [$ key ] = $ meta ->setValueBool ($ value )->setIndexed ($ index );
442+ $ this ->metadata [$ key ] = $ meta ->setValueBool ($ value )->setIndexed ($ index )-> setEtag ( $ this -> getFileEtag ()) ;
404443 $ this ->updated = true ;
405444
406445 return $ this ;
@@ -419,15 +458,15 @@ public function setBool(string $key, bool $value, bool $index = false): IFilesMe
419458 public function setArray (string $ key , array $ value ): IFilesMetadata {
420459 $ this ->confirmKeyFormat ($ key );
421460 try {
422- if ($ this ->getArray ($ key ) === $ value ) {
423- return $ this ; // we ignore if value have not changed
461+ if ($ this ->getArray ($ key ) === $ value && $ this -> isUpToDate ( $ key ) ) {
462+ return $ this ; // we ignore if value and index have not changed and etag is still the same
424463 }
425464 } catch (FilesMetadataNotFoundException |FilesMetadataTypeException $ e ) {
426465 // if value does not exist, or type has changed, we keep on the writing
427466 }
428467
429468 $ meta = new MetadataValueWrapper (IMetadataValueWrapper::TYPE_ARRAY );
430- $ this ->metadata [$ key ] = $ meta ->setValueArray ($ value );
469+ $ this ->metadata [$ key ] = $ meta ->setValueArray ($ value )-> setEtag ( $ this -> getFileEtag ()) ;
431470 $ this ->updated = true ;
432471
433472 return $ this ;
@@ -446,15 +485,15 @@ public function setArray(string $key, array $value): IFilesMetadata {
446485 public function setStringList (string $ key , array $ value , bool $ index = false ): IFilesMetadata {
447486 $ this ->confirmKeyFormat ($ key );
448487 try {
449- if ($ this ->getStringList ($ key ) === $ value ) {
450- return $ this ; // we ignore if value have not changed
488+ if ($ this ->getStringList ($ key ) === $ value && $ this -> isUpToDate ( $ key ) ) {
489+ return $ this ; // we ignore if value and index have not changed and etag is still the same
451490 }
452491 } catch (FilesMetadataNotFoundException |FilesMetadataTypeException $ e ) {
453492 // if value does not exist, or type has changed, we keep on the writing
454493 }
455494
456495 $ meta = new MetadataValueWrapper (IMetadataValueWrapper::TYPE_STRING_LIST );
457- $ this ->metadata [$ key ] = $ meta ->setValueStringList ($ value )->setIndexed ($ index );
496+ $ this ->metadata [$ key ] = $ meta ->setValueStringList ($ value )->setIndexed ($ index )-> setEtag ( $ this -> getFileEtag ()) ;
458497 $ this ->updated = true ;
459498
460499 return $ this ;
@@ -473,15 +512,15 @@ public function setStringList(string $key, array $value, bool $index = false): I
473512 public function setIntList (string $ key , array $ value , bool $ index = false ): IFilesMetadata {
474513 $ this ->confirmKeyFormat ($ key );
475514 try {
476- if ($ this ->getIntList ($ key ) === $ value ) {
477- return $ this ; // we ignore if value have not changed
515+ if ($ this ->getIntList ($ key ) === $ value && $ this -> isUpToDate ( $ key ) ) {
516+ return $ this ; // we ignore if value and index have not changed and etag is still the same
478517 }
479518 } catch (FilesMetadataNotFoundException |FilesMetadataTypeException $ e ) {
480519 // if value does not exist, or type has changed, we keep on the writing
481520 }
482521
483522 $ valueWrapper = new MetadataValueWrapper (IMetadataValueWrapper::TYPE_STRING_LIST );
484- $ this ->metadata [$ key ] = $ valueWrapper ->setValueIntList ($ value )->setIndexed ($ index );
523+ $ this ->metadata [$ key ] = $ valueWrapper ->setValueIntList ($ value )->setIndexed ($ index )-> setEtag ( $ this -> getFileEtag ()) ;
485524 $ this ->updated = true ;
486525
487526 return $ this ;
0 commit comments