Skip to content

Commit

Permalink
Rename and refactor field cleaning methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Jun 21, 2024
1 parent 3bb3d23 commit 9156ccb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 33 deletions.
79 changes: 49 additions & 30 deletions src/Content/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ public function create(array $fields, Language|string $language = 'default'): vo
{
$language = Language::ensure($language);

$fields = $this->convertFieldNamesToLowerCase($fields);
$fields = $this->removeUnwantedFields($fields, $language);

$this->model->storage()->create($this->id, $language, $fields);
$this->model->storage()->create(
versionId: $this->id,
language: $language,
fields: $this->prepareFieldsBeforeWrite($fields, $language)
);
}

/**
Expand Down Expand Up @@ -172,28 +173,17 @@ public function move(
}

/**
* Returns the stored content fields
*
* @return array<string, string>
* Prepare fields to be written by removing unwanted fields
* depending on the language or model and by cleaning the field names
*/
public function read(Language|string $language = 'default'): array
protected function prepareFieldsBeforeWrite(array $fields, Language $language): array
{
$language = Language::ensure($language);
// convert all field names to lower case
$fields = $this->convertFieldNamesToLowerCase($fields);

try {
$fields = $this->model->storage()->read($this->id, $language);
$fields = $this->convertFieldNamesToLowerCase($fields);
return $fields;
} catch (Throwable) {
return [];
}
}
// make sure to store the right fields for the model
$fields = $this->model->contentFileData($fields, $language);

/**
* Remove fields that should not be stored for the given version and language
*/
protected function removeUnwantedFields(array $fields, Language $language): array
{
// the default language stores all fields
if ($language->isDefault() === true) {
return $fields;
Expand All @@ -212,6 +202,33 @@ protected function removeUnwantedFields(array $fields, Language $language): arra
return $fields;
}

/**
* Make sure that reading from storage will always
* return a usable set of fields with clean field names
*/
protected function prepareFieldsAfterRead(array $fields, Language $language): array
{
return $this->convertFieldNamesToLowerCase($fields);
}

/**
* Returns the stored content fields
*
* @return array<string, string>
*/
public function read(Language|string $language = 'default'): array
{
$language = Language::ensure($language);

try {
$fields = $this->model->storage()->read($this->id, $language);
$fields = $this->prepareFieldsAfterRead($fields, $language);
return $fields;
} catch (Throwable) {
return [];
}
}

/**
* Replaces the content of the current version with the given fields
*
Expand All @@ -225,10 +242,11 @@ public function replace(array $fields, Language|string $language = 'default'): v

$language = Language::ensure($language);

$fields = $this->convertFieldNamesToLowerCase($fields);
$fields = $this->removeUnwantedFields($fields, $language);

$this->model->storage()->update($this->id, $language, $fields);
$this->model->storage()->update(
versionId: $this->id,
language: $language,
fields: $this->prepareFieldsBeforeWrite($fields, $language)
);
}

/**
Expand Down Expand Up @@ -280,9 +298,10 @@ public function update(array $fields, Language|string $language = 'default'): vo
...$this->convertFieldNamesToLowerCase($fields)
];

// make sure to not store unnecessary fields for the version & language
$fields = $this->removeUnwantedFields($fields, $language);

$this->model->storage()->update($this->id, $language, $fields);
$this->model->storage()->update(
versionId: $this->id,
language: $language,
fields: $this->prepareFieldsBeforeWrite($fields, $language)
);
}
}
17 changes: 14 additions & 3 deletions tests/Content/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function testCreateSingleLanguage(): void
/**
* @covers ::create
* @covers ::convertFieldNamesToLowerCase
* @covers ::removeUnwantedFields
* @covers ::prepareFieldsBeforeWrite
*/
public function testCreateWithDirtyFields(): void
{
Expand Down Expand Up @@ -653,6 +653,8 @@ public function testMoveToVersion(): void

/**
* @covers ::read
* @covers ::convertFieldNamesToLowerCase
* @covers ::prepareFieldsAfterRead
*/
public function testReadMultiLanguage(): void
{
Expand All @@ -673,6 +675,8 @@ public function testReadMultiLanguage(): void

/**
* @covers ::read
* @covers ::convertFieldNamesToLowerCase
* @covers ::prepareFieldsAfterRead
*/
public function testReadSingleLanguage(): void
{
Expand All @@ -691,6 +695,7 @@ public function testReadSingleLanguage(): void
/**
* @covers ::read
* @covers ::convertFieldNamesToLowerCase
* @covers ::prepareFieldsAfterRead
*/
public function testReadWithDirtyFields(): void
{
Expand All @@ -713,6 +718,8 @@ public function testReadWithDirtyFields(): void

/**
* @covers ::read
* @covers ::convertFieldNamesToLowerCase
* @covers ::prepareFieldsAfterRead
*/
public function testReadWithInvalidLanguage(): void
{
Expand All @@ -731,6 +738,8 @@ public function testReadWithInvalidLanguage(): void

/**
* @covers ::replace
* @covers ::convertFieldNamesToLowerCase
* @covers ::prepareFieldsBeforeWrite
*/
public function testReplaceMultiLanguage(): void
{
Expand Down Expand Up @@ -759,6 +768,8 @@ public function testReplaceMultiLanguage(): void

/**
* @covers ::replace
* @covers ::convertFieldNamesToLowerCase
* @covers ::prepareFieldsBeforeWrite
*/
public function testReplaceSingleLanguage(): void
{
Expand All @@ -781,7 +792,7 @@ public function testReplaceSingleLanguage(): void
/**
* @covers ::replace
* @covers ::convertFieldNamesToLowerCase
* @covers ::removeUnwantedFields
* @covers ::prepareFieldsBeforeWrite
*/
public function testReplaceWithDirtyFields(): void
{
Expand Down Expand Up @@ -1099,7 +1110,7 @@ public function testUpdateSingleLanguage(): void
/**
* @covers ::update
* @covers ::convertFieldNamesToLowerCase
* @covers ::removeUnwantedFields
* @covers ::prepareFieldsBeforeWrite
*/
public function testUpdateWithDirtyFields(): void
{
Expand Down

0 comments on commit 9156ccb

Please sign in to comment.