Skip to content

Commit

Permalink
Revert setRawAttributes override
Browse files Browse the repository at this point in the history
  • Loading branch information
MatanYadaev committed Jan 5, 2024
1 parent a132345 commit 1dc6ce6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 56 deletions.
25 changes: 0 additions & 25 deletions src/Traits/HasSpatial.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace MatanYadaev\EloquentSpatial\Traits;

use MatanYadaev\EloquentSpatial\Objects\Geometry;
use MatanYadaev\EloquentSpatial\SpatialBuilder;

trait HasSpatial
Expand All @@ -11,28 +10,4 @@ public function newEloquentBuilder($query): SpatialBuilder
{
return new SpatialBuilder($query);
}

/**
* @param array<string, mixed> $attributes
* @param bool $sync
* @return $this
*/
public function setRawAttributes(array $attributes, $sync = false)
{
$result = parent::setRawAttributes($attributes, $sync);

foreach ($attributes as $attribute => $value) {
$casts = $this->getCasts();
if (isset($casts[$attribute]) && is_subclass_of($casts[$attribute], Geometry::class)) {
$spatialAttribute = $this->getAttribute($attribute);

if ($spatialAttribute instanceof Geometry) {
$this->attributes[$attribute] = $spatialAttribute;
$this->original[$attribute] = $spatialAttribute;
}
}
}

return $result;
}
}
35 changes: 4 additions & 31 deletions tests/GeometryCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@
TestPlace::insert(array_merge(TestPlace::factory()->definition(), [
'point_with_line_string_cast' => DB::raw('POINT(0, 180)'),
]));
/** @var TestPlace $testPlace */
$testPlace = TestPlace::firstOrFail();

expect(function (): void {
TestPlace::firstOrFail();
expect(function () use ($testPlace): void {
$testPlace->getAttribute('point_with_line_string_cast');
})->toThrow(InvalidArgumentException::class);
});

Expand All @@ -124,32 +126,3 @@

expect($testPlace->point)->toEqual($point);
});

it('serializes and json encodes a model record with geometry', function (): void {
// Arrange
$point = new Point(0, 180);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->make(['point' => $point]);

// Act
// @phpstan-ignore-next-line
$recoveredTestPlace = unserialize(json_decode(json_encode(serialize($testPlace))));

// Assert
expect($recoveredTestPlace)->toEqual($testPlace);
});

it('serializes and json encodes a model record with geometry when retrieving from database', function (): void {
// Arrange
$point = new Point(0, 180);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);
$testPlaceFromDb = TestPlace::find($testPlace->id);

// Act
// @phpstan-ignore-next-line
$recoveredTestPlace = unserialize(json_decode(json_encode(serialize($testPlaceFromDb))));

// Assert
expect($recoveredTestPlace)->toEqual($testPlaceFromDb);
});

0 comments on commit 1dc6ce6

Please sign in to comment.