Skip to content

Commit

Permalink
TASK: Introduce SerializedPropertyValues::withoutUnsets
Browse files Browse the repository at this point in the history
To make the code more obvious
  • Loading branch information
mhsdesign committed Feb 2, 2024
1 parent 2e92b68 commit 4892292
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public function updateToDatabase(Connection $databaseConnection, string $tableNa
'nodeaggregateid' => $this->nodeAggregateId->value,
'origindimensionspacepoint' => json_encode($this->originDimensionSpacePoint),
'origindimensionspacepointhash' => $this->originDimensionSpacePointHash,
'properties' => self::propertyValuesToJson($this->properties),
// we don't write null explicitly into the database
'properties' => json_encode($this->properties->withoutUnsets()),
'nodetypename' => $this->nodeTypeName->value,
'classification' => $this->classification->value,
'lastmodified' => $this->timestamps->lastModified,
Expand Down Expand Up @@ -143,7 +144,8 @@ public static function createNewInDatabase(
'nodeaggregateid' => $nodeAggregateId->value,
'origindimensionspacepoint' => json_encode($originDimensionSpacePoint),
'origindimensionspacepointhash' => $originDimensionSpacePointHash,
'properties' => self::propertyValuesToJson($properties),
// we don't write null explicitly into the database
'properties' => json_encode($properties->withoutUnsets()),
'nodetypename' => $nodeTypeName->value,
'classification' => $classification->value,
'created' => $timestamps->created,
Expand Down Expand Up @@ -200,19 +202,6 @@ public static function createCopyFromNodeRecord(
);
}

private static function propertyValuesToJson(SerializedPropertyValues $serializedPropertyValues): string
{
$values = [];
foreach ($serializedPropertyValues->values as $name => $serializedValue) {
if ($serializedValue instanceof UnsetPropertyValue) {
// we don't write null explicitly into the database
continue;
}
$values[$name] = $serializedValue;
}
return json_encode($values, JSON_THROW_ON_ERROR);
}

private static function parseDateTimeString(string $string): \DateTimeImmutable
{
$result = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $string);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ public function merge(self $other): self
return new self(array_merge($this->values, $other->values));
}

public function withoutUnsets(): self
{
$values = [];
foreach ($this->values as $name => $serializedValue) {
if ($serializedValue instanceof UnsetPropertyValue) {
continue;
}
$values[$name] = $serializedValue;
}
return new self($values);
}

/**
* @return array<string,self>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,24 @@ public function jsonSerializeReturnsNullForUnsetValues(): void
*/
public function getPlainValuesReturnsNullForUnsetValues(): void
{
$propertyValues = SerializedPropertyValues::fromArray(['someProperty' => UnsetPropertyValue::get(), 'otherProperty' => SerializedPropertyValue::create('mehehe', 'string')]);
$propertyValues = SerializedPropertyValues::fromArray(['someProperty' => UnsetPropertyValue::get(), 'otherProperty' => SerializedPropertyValue::create('me-he-he', 'string')]);
self::assertSame(
['someProperty' => null, 'otherProperty' => 'mehehe'],
['someProperty' => null, 'otherProperty' => 'me-he-he'],
$propertyValues->getPlainValues()
);
}

/**
* @test
*/
public function withoutUnsets(): void
{
// the format which is implicitly used in the event log, due to json_serialize
$propertyValues = SerializedPropertyValues::fromArray(['someProperty' => UnsetPropertyValue::get(), 'otherProperty' => SerializedPropertyValue::create('text', 'string'), 'reputation' => UnsetPropertyValue::get()]);
self::assertEquals(
SerializedPropertyValues::fromArray(['otherProperty' => SerializedPropertyValue::create('text', 'string')]),
$propertyValues->withoutUnsets()
);
}

}

0 comments on commit 4892292

Please sign in to comment.