Skip to content

Commit 208151f

Browse files
committed
improved resource point/location casting
1 parent 207811f commit 208151f

8 files changed

Lines changed: 30 additions & 13 deletions

File tree

server/src/Http/Resources/v1/Driver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function toArray($request)
5050
'jobs' => $this->whenLoaded('jobs', fn () => $this->getJobs()),
5151
'vendor' => $this->whenLoaded('vendor', fn () => new Vendor($this->vendor)),
5252
'fleets' => $this->whenLoaded('fleets', fn () => Fleet::collection($this->fleets()->without('drivers')->get())),
53-
'location' => $this->wasRecentlyCreated ? new Point(0, 0) : data_get($this, 'location', new Point(0, 0)),
53+
'location' => $this->wasRecentlyCreated ? new Point(0, 0) : Utils::castPoint($this->location),
5454
'heading' => (int) data_get($this, 'heading', 0),
5555
'altitude' => (int) data_get($this, 'altitude', 0),
5656
'speed' => (int) data_get($this, 'speed', 0),
@@ -96,7 +96,7 @@ public function toWebhookPayload()
9696
'vehicle' => data_get($this, 'vehicle.public_id'),
9797
'current_job' => data_get($this, 'currentJob.public_id'),
9898
'vendor' => data_get($this, 'vendor.public_id'),
99-
'location' => data_get($this, 'location', new Point(0, 0)),
99+
'location' => Utils::castPoint($this->location),
100100
'heading' => (int) data_get($this, 'heading', 0),
101101
'altitude' => (int) data_get($this, 'altitude', 0),
102102
'speed' => (int) data_get($this, 'speed', 0),

server/src/Http/Resources/v1/Index/Driver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Fleetbase\FleetOps\Http\Resources\v1\Index;
44

5+
use Fleetbase\FleetOps\Support\Utils;
56
use Fleetbase\Http\Resources\FleetbaseResource;
67
use Fleetbase\LaravelMysqlSpatial\Types\Point;
78
use Fleetbase\Support\Http;
@@ -35,7 +36,7 @@ public function toArray($request): array
3536
'phone' => $this->phone,
3637
'photo_url' => $this->photo_url,
3738
'status' => $this->status,
38-
'location' => $this->wasRecentlyCreated ? new Point(0, 0) : data_get($this, 'location', new Point(0, 0)),
39+
'location' => $this->wasRecentlyCreated ? new Point(0, 0) : Utils::castPoint($this->location),
3940
'heading' => (int) data_get($this, 'heading', 0),
4041
'altitude' => (int) data_get($this, 'altitude', 0),
4142
'speed' => (int) data_get($this, 'speed', 0),

server/src/Http/Resources/v1/Index/Place.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function toArray($request): array
3434
'city' => $this->city,
3535
'country' => $this->country,
3636
'avatar_url' => $this->avatar_url,
37-
'location' => Utils::getPointFromMixed($this->location),
37+
'location' => Utils::castPoint($this->location),
3838

3939
// Meta flag to indicate this is an index resource
4040
'meta' => [

server/src/Http/Resources/v1/Index/Vehicle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Fleetbase\FleetOps\Http\Resources\v1\Index;
44

5+
use Fleetbase\FleetOps\Support\Utils;
56
use Fleetbase\Http\Resources\FleetbaseResource;
6-
use Fleetbase\LaravelMysqlSpatial\Types\Point;
77
use Fleetbase\Support\Http;
88

99
/**
@@ -35,7 +35,7 @@ public function toArray($request): array
3535
'year' => $this->year,
3636
'photo_url' => $this->photo_url,
3737
'status' => $this->status,
38-
'location' => data_get($this, 'location', new Point(0, 0)),
38+
'location' => Utils::castPoint($this->location),
3939
'heading' => (int) data_get($this, 'heading', 0),
4040
'altitude' => (int) data_get($this, 'altitude', 0),
4141
'speed' => (int) data_get($this, 'speed', 0),

server/src/Http/Resources/v1/Place.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function toArray($request)
2828
'owner_uuid' => $this->when(Http::isInternalRequest(), $this->owner_uuid),
2929
'owner_type' => $this->when(Http::isInternalRequest(), $this->owner_type ? Utils::toEmberResourceType($this->owner_type) : null),
3030
'name' => $this->name,
31-
'location' => Utils::getPointFromMixed($this->location),
31+
'location' => Utils::castPoint($this->location),
3232
'address' => $this->address,
3333
'address_html' => $this->when(Http::isInternalRequest(), $this->address_html),
3434
'avatar_url' => $this->avatar_url,

server/src/Http/Resources/v1/Vehicle.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Fleetbase\FleetOps\Support\Utils;
66
use Fleetbase\Http\Resources\FleetbaseResource;
7-
use Fleetbase\LaravelMysqlSpatial\Types\Point;
87
use Fleetbase\Support\Http;
98

109
class Vehicle extends FleetbaseResource
@@ -132,7 +131,7 @@ public function toArray($request)
132131
'updated_at' => $this->updated_at,
133132
'created_at' => $this->created_at,
134133
// Location & telematics
135-
'location' => data_get($this, 'location', new Point(0, 0)),
134+
'location' => Utils::castPoint($this->location),
136135
'heading' => (int) data_get($this, 'heading', 0),
137136
'altitude' => (int) data_get($this, 'altitude', 0),
138137
'speed' => (int) data_get($this, 'speed', 0),
@@ -248,7 +247,7 @@ public function toWebhookPayload()
248247
'updated_at' => $this->updated_at,
249248
'created_at' => $this->created_at,
250249
// Location & telematics
251-
'location' => data_get($this, 'location', new Point(0, 0)),
250+
'location' => Utils::castPoint($this->location),
252251
'heading' => (int) data_get($this, 'heading', 0),
253252
'altitude' => (int) data_get($this, 'altitude', 0),
254253
'speed' => (int) data_get($this, 'speed', 0),

server/src/Http/Resources/v1/VehicleWithoutDriver.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Fleetbase\FleetOps\Support\Utils;
66
use Fleetbase\Http\Resources\FleetbaseResource;
7-
use Fleetbase\LaravelMysqlSpatial\Types\Point;
87
use Fleetbase\Support\Http;
98

109
class VehicleWithoutDriver extends FleetbaseResource
@@ -131,7 +130,7 @@ public function toArray($request)
131130
'updated_at' => $this->updated_at,
132131
'created_at' => $this->created_at,
133132
// Location & telematics
134-
'location' => data_get($this, 'location', new Point(0, 0)),
133+
'location' => Utils::castPoint($this->location),
135134
'heading' => (int) data_get($this, 'heading', 0),
136135
'altitude' => (int) data_get($this, 'altitude', 0),
137136
'speed' => (int) data_get($this, 'speed', 0),
@@ -245,7 +244,7 @@ public function toWebhookPayload()
245244
'updated_at' => $this->updated_at,
246245
'created_at' => $this->created_at,
247246
// Location & telematics
248-
'location' => data_get($this, 'location', new Point(0, 0)),
247+
'location' => Utils::castPoint($this->location),
249248
'heading' => (int) data_get($this, 'heading', 0),
250249
'altitude' => (int) data_get($this, 'altitude', 0),
251250
'speed' => (int) data_get($this, 'speed', 0),

server/src/Support/Utils.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,24 @@ public static function getPointFromMixed($coordinates): ?Point
334334
return new Point((float) $latitude, (float) $longitude);
335335
}
336336

337+
/**
338+
* Always return spatial point.
339+
*
340+
* @param [type] $mixed
341+
*
342+
* @return void
343+
*/
344+
public static function castPoint($mixed)
345+
{
346+
try {
347+
$point = static::getPointFromMixed($mixed);
348+
349+
return $point;
350+
} catch (\Throwable $e) {
351+
return new Point(0, 0);
352+
}
353+
}
354+
337355
/**
338356
* Determines if the given coordinates strictly represent a Point object.
339357
* These will explude resolvable coordinates from records.

0 commit comments

Comments
 (0)