Skip to content

Commit df3ed55

Browse files
authored
override originalIsEquivalent to compare geometries (#115)
1 parent d078b79 commit df3ed55

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

src/GeometryCast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function set($model, string $key, $value, array $attributes): ?Expression
6969
if (! ($value instanceof $this->className)) {
7070
$geometryType = is_object($value) ? $value::class : gettype($value);
7171
throw new InvalidArgumentException(
72-
sprintf('Expected %s, %s given.', static::class, $geometryType)
72+
sprintf('Expected %s, %s given.', $this->className, $geometryType)
7373
);
7474
}
7575

src/Traits/HasSpatial.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@
1111

1212
trait HasSpatial
1313
{
14+
public function originalIsEquivalent($key)
15+
{
16+
if (! array_key_exists($key, $this->original)) {
17+
return false;
18+
}
19+
20+
$casts = $this->getCasts();
21+
22+
if (array_key_exists($key, $casts)) {
23+
$original = $this->getOriginal($key);
24+
$attribute = $this->getAttributeValue($key);
25+
26+
if ($original instanceof Geometry && $attribute instanceof Geometry) {
27+
return $original->getWktData() === $attribute->getWktData();
28+
}
29+
}
30+
31+
return parent::originalIsEquivalent($key);
32+
}
33+
1434
public function scopeWithDistance(
1535
Builder $query,
1636
ExpressionContract|Geometry|string $column,

tests/GeometryCastTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,66 @@
126126

127127
expect($testPlace->point)->toEqual($point);
128128
});
129+
130+
it('checks a model record is not dirty after creation', function (): void {
131+
$point = new Point(0, 180);
132+
133+
/** @var TestPlace $testPlace */
134+
$testPlace = TestPlace::factory()->create(['point' => $point]);
135+
136+
expect($testPlace->isDirty())->toBeFalse();
137+
});
138+
139+
it('checks a model record is not dirty after fetch', function (): void {
140+
$point = new Point(0, 180);
141+
TestPlace::factory()->create(['point' => $point]);
142+
143+
/** @var TestPlace $testPlace */
144+
$testPlace = TestPlace::firstOrFail();
145+
146+
expect($testPlace->isDirty())->toBeFalse();
147+
});
148+
149+
it('checks a model record is dirty after update from null before save', function (): void {
150+
$point = new Point(0, 180);
151+
/** @var TestPlace $testPlace */
152+
$testPlace = TestPlace::factory()->create([]);
153+
154+
$testPlace->point = $point;
155+
156+
expect($testPlace->isDirty())->toBeTrue();
157+
});
158+
159+
it('checks a model record is dirty after update before save', function (): void {
160+
$point = new Point(0, 180);
161+
$point2 = new Point(0, 0);
162+
/** @var TestPlace $testPlace */
163+
$testPlace = TestPlace::factory()->create(['point' => $point]);
164+
165+
$testPlace->point = $point2;
166+
167+
expect($testPlace->isDirty())->toBeTrue();
168+
});
169+
170+
it('checks a model record is not dirty after update and save', function (): void {
171+
$point = new Point(0, 180);
172+
$point2 = new Point(0, 0);
173+
/** @var TestPlace $testPlace */
174+
$testPlace = TestPlace::factory()->create(['point' => $point]);
175+
176+
$testPlace->point = $point2;
177+
$testPlace->save();
178+
179+
expect($testPlace->isDirty())->toBeFalse();
180+
});
181+
182+
it('checks a model record is not dirty after update to same value before save', function (): void {
183+
$point = new Point(0, 180);
184+
$point2 = new Point(0, 180);
185+
/** @var TestPlace $testPlace */
186+
$testPlace = TestPlace::factory()->create(['point' => $point]);
187+
188+
$testPlace->point = $point2;
189+
190+
expect($testPlace->isDirty())->toBeFalse();
191+
});

0 commit comments

Comments
 (0)