Skip to content

Commit

Permalink
override originalIsEquivalent to compare geometries (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatanYadaev authored Mar 5, 2024
1 parent d078b79 commit df3ed55
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/GeometryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function set($model, string $key, $value, array $attributes): ?Expression
if (! ($value instanceof $this->className)) {
$geometryType = is_object($value) ? $value::class : gettype($value);
throw new InvalidArgumentException(
sprintf('Expected %s, %s given.', static::class, $geometryType)
sprintf('Expected %s, %s given.', $this->className, $geometryType)
);
}

Expand Down
20 changes: 20 additions & 0 deletions src/Traits/HasSpatial.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@

trait HasSpatial
{
public function originalIsEquivalent($key)
{
if (! array_key_exists($key, $this->original)) {
return false;
}

$casts = $this->getCasts();

if (array_key_exists($key, $casts)) {
$original = $this->getOriginal($key);
$attribute = $this->getAttributeValue($key);

if ($original instanceof Geometry && $attribute instanceof Geometry) {
return $original->getWktData() === $attribute->getWktData();
}
}

return parent::originalIsEquivalent($key);
}

public function scopeWithDistance(
Builder $query,
ExpressionContract|Geometry|string $column,
Expand Down
63 changes: 63 additions & 0 deletions tests/GeometryCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,66 @@

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

it('checks a model record is not dirty after creation', function (): void {
$point = new Point(0, 180);

/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);

expect($testPlace->isDirty())->toBeFalse();
});

it('checks a model record is not dirty after fetch', function (): void {
$point = new Point(0, 180);
TestPlace::factory()->create(['point' => $point]);

/** @var TestPlace $testPlace */
$testPlace = TestPlace::firstOrFail();

expect($testPlace->isDirty())->toBeFalse();
});

it('checks a model record is dirty after update from null before save', function (): void {
$point = new Point(0, 180);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create([]);

$testPlace->point = $point;

expect($testPlace->isDirty())->toBeTrue();
});

it('checks a model record is dirty after update before save', function (): void {
$point = new Point(0, 180);
$point2 = new Point(0, 0);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);

$testPlace->point = $point2;

expect($testPlace->isDirty())->toBeTrue();
});

it('checks a model record is not dirty after update and save', function (): void {
$point = new Point(0, 180);
$point2 = new Point(0, 0);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);

$testPlace->point = $point2;
$testPlace->save();

expect($testPlace->isDirty())->toBeFalse();
});

it('checks a model record is not dirty after update to same value before save', function (): void {
$point = new Point(0, 180);
$point2 = new Point(0, 180);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);

$testPlace->point = $point2;

expect($testPlace->isDirty())->toBeFalse();
});

0 comments on commit df3ed55

Please sign in to comment.