Skip to content

Commit 8ee4118

Browse files
committed
Formatting and improve setRawAttributes implementation
1 parent c1958ae commit 8ee4118

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

src/Traits/HasSpatial.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ public function newEloquentBuilder($query): SpatialBuilder
1313
}
1414

1515
/**
16-
* @param array<string, mixed> $attributes
16+
* @param array<string, mixed> $attributes
17+
* @param bool $sync
18+
* @return $this
1719
*/
1820
public function setRawAttributes(array $attributes, $sync = false)
1921
{
2022
$result = parent::setRawAttributes($attributes, $sync);
2123

2224
foreach ($attributes as $attribute => $value) {
23-
if ($value && is_string($value) && ! preg_match('//u', $value)) { // the string is binary
24-
// access the attribute to force conversion via attribute cast
25-
$spatialAttribute = $this->$attribute;
25+
$casts = $this->getCasts();
26+
if (isset($casts[$attribute]) && is_subclass_of($casts[$attribute], Geometry::class)) {
27+
$spatialAttribute = $this->getAttribute($attribute);
2628

27-
// override attribute and original attribute to get rid of binary strings
28-
// (Those would lead to errors while JSON encoding a serialized version of the model.)
2929
if ($spatialAttribute instanceof Geometry) {
3030
$this->attributes[$attribute] = $spatialAttribute;
3131
$this->original[$attribute] = $spatialAttribute;

tests/GeometryCastTest.php

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@
111111
]));
112112

113113
expect(function (): void {
114-
// cast deserialization happens in setRawAttributes
115-
// called immediately after retrieving db record
116114
TestPlace::firstOrFail();
117115
})->toThrow(InvalidArgumentException::class);
118116
});
@@ -127,40 +125,31 @@
127125
expect($testPlace->point)->toEqual($point);
128126
});
129127

130-
it('is serializeable and JSON encodeable by standard serialize / JSON encode functions', function (): void {
128+
it('serializes and json encodes a model record with geometry', function (): void {
129+
// Arrange
131130
$point = new Point(0, 180);
132-
133131
/** @var TestPlace $testPlace */
134132
$testPlace = TestPlace::factory()->make(['point' => $point]);
135133

136-
$serialized = serialize($testPlace);
137-
$json = json_encode($serialized);
138-
139-
expect($json)->toBeTruthy('JSON encoding failed.');
140-
134+
// Act
141135
// @phpstan-ignore-next-line
142-
$recoveredTestPlace = unserialize(json_decode($json));
136+
$recoveredTestPlace = unserialize(json_decode(json_encode(serialize($testPlace))));
143137

138+
// Assert
144139
expect($recoveredTestPlace)->toEqual($testPlace);
145140
});
146141

147-
it('is serializeable and JSON encodeable by standard serialize / JSON encode functions when retrieved from db', function (): void {
142+
it('serializes and json encodes a model record with geometry when retrieving from database', function (): void {
143+
// Arrange
148144
$point = new Point(0, 180);
149-
150145
/** @var TestPlace $testPlace */
151-
$testPlace = TestPlace::factory()->make(['point' => $point]);
152-
153-
$testPlace->save();
154-
146+
$testPlace = TestPlace::factory()->create(['point' => $point]);
155147
$testPlaceFromDb = TestPlace::find($testPlace->id);
156148

157-
$serialized = serialize($testPlaceFromDb);
158-
$json = json_encode($serialized);
159-
160-
expect($json)->toBeTruthy('JSON encoding failed.');
161-
149+
// Act
162150
// @phpstan-ignore-next-line
163-
$recoveredTestPlace = unserialize(json_decode($json));
151+
$recoveredTestPlace = unserialize(json_decode(json_encode(serialize($testPlaceFromDb))));
164152

153+
// Assert
165154
expect($recoveredTestPlace)->toEqual($testPlaceFromDb);
166155
});

0 commit comments

Comments
 (0)