Skip to content

Commit

Permalink
feat: add GeoPoint caster
Browse files Browse the repository at this point in the history
  • Loading branch information
efureev committed Jul 30, 2021
1 parent 58e335d commit c8f1e9d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": "^8.0",
"efureev/support": "^4.1",
"efureev/support": "^4.1.2",
"illuminate/database": "^8.38"
},
"require-dev": {
Expand Down
39 changes: 39 additions & 0 deletions src/Caster/GeoPoint.php
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Php\Support\Laravel\Caster;

use Php\Support\Types\GeoPoint as BaseGeoPoint;

/**
* Class GeoPoint
* @package Php\Support\Laravel\Caster
*
* GeoPoint for PG
*/
class GeoPoint extends BaseGeoPoint implements Caster
{
public static function castToDatabase($value): ?string
{
if (!$value) {
return null;
}

if ($value instanceof static) {
return $value->toPgDB();
}

throw new \RuntimeException('Invalid type of $value: ' . gettype($value));
}

public static function isEquivalent($value, $original): bool
{
return $value->toJson() === $original?->toJson();
}

public function value(): ?string
{
return static::castToDatabase($this);
}
}
25 changes: 24 additions & 1 deletion tests/Functional/CasterTraitTest.php
Expand Up @@ -5,6 +5,7 @@
namespace Php\Support\Laravel\Tests\Functional;

use Illuminate\Database\Eloquent\Collection;
use Php\Support\Laravel\Caster\GeoPoint;
use Php\Support\Laravel\Tests\Database\Factories\TestModelFactory;
use Php\Support\Laravel\Tests\TestClasses\Entity\EmptyParams;
use Php\Support\Laravel\Tests\TestClasses\Entity\Params;
Expand All @@ -18,7 +19,11 @@ class CasterTraitTest extends AbstractFunctionalTestCase
public function testCreateAndGetWithNullParams(): void
{
/** @var Collection $list */
$list = TestModelFactory::times(5)->create();
$list = TestModelFactory::times(5)->create(
[
'geo_point' => null,
]
);

static::assertCount(5, $list);

Expand All @@ -32,6 +37,7 @@ public function testCreateAndGetWithNullParams(): void
static::assertNotEmpty($item->str);
static::assertNull($item->str_empty);
static::assertNull($item->int);
static::assertNull($item->geo_point);

static::assertIsBool($item->enabled);
static::assertNull($item->config);
Expand Down Expand Up @@ -210,6 +216,23 @@ public function testCreateAndGetWithFillArrayParams(): void
}
}

public function testCreateGeoPoint(): void
{
$model = TestModel::create(
[
'geo_point' => new GeoPoint(111.02, 21.20),
]
);

static::assertInstanceOf(GeoPoint::class, $model->geo_point);
static::assertEquals(111.02, $model->geo_point->x);
static::assertEquals(21.20, $model->geo_point->y);
static::assertEquals('(111.02,21.2)', $model->geo_point->value());
static::assertEquals([111.02, 21.2], $model->geo_point->toArray());
static::assertEquals('{"longitude":111.02,"latitude":21.2}', $model->geo_point->toJson());
}


public function testCreateAndGetWithClassParams(): void
{
/** @var Collection $list */
Expand Down
4 changes: 4 additions & 0 deletions tests/TestClasses/Models/TestModel.php
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Foundation\Auth\User;
use Php\Support\Laravel\Caster\GeoPoint;
use Php\Support\Laravel\Caster\HasCasts;
use Php\Support\Laravel\Tests\TestClasses\Entity\Params;
use Php\Support\Laravel\Tests\TestClasses\Entity\Status;
Expand All @@ -17,6 +18,7 @@
* @property string $title
* @property Params $params
* @property Status $status
* @property GeoPoint $geo_point
* @property array $config
* @property string $str
* @property string $str_empty
Expand All @@ -40,6 +42,7 @@ class TestModel extends Model
'str_empty',
'int',
'enabled',
'geo_point',
];

protected $casts = [
Expand All @@ -50,6 +53,7 @@ class TestModel extends Model
'str' => 'string',
'str_empty' => 'string',
'int' => 'int',
'geo_point' => GeoPoint::class,
];

public function user(): BelongsTo
Expand Down
11 changes: 6 additions & 5 deletions tests/database/factories/TestModelFactory.php
@@ -1,9 +1,9 @@
<?php


namespace Php\Support\Laravel\Tests\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Php\Support\Laravel\Caster\GeoPoint;
use Php\Support\Laravel\Tests\TestClasses\Entity\Status;
use Php\Support\Laravel\Tests\TestClasses\Models\TestModel;

Expand All @@ -19,10 +19,11 @@ class TestModelFactory extends Factory
public function definition(): array
{
return [
'title' => $this->faker->sentence,
'str' => $this->faker->title,
'enabled' => $this->faker->randomElement([true, false]),
'status' => $this->faker->randomElement(Status::STATUSES),
'title' => $this->faker->sentence,
'str' => $this->faker->title,
'enabled' => $this->faker->randomElement([true, false]),
'status' => $this->faker->randomElement(Status::STATUSES),
'geo_point' => new GeoPoint($this->faker->randomFloat(), $this->faker->randomFloat()),
];
}
}
Expand Up @@ -46,6 +46,8 @@ static function (Blueprint $table) {
}
);

DB::statement('ALTER TABLE test_table ADD COLUMN geo_point point');

Schema::create(
'pg_table',
static function (Blueprint $table) {
Expand Down

0 comments on commit c8f1e9d

Please sign in to comment.