Skip to content

Commit 4ca4c91

Browse files
committed
wip
1 parent de565b1 commit 4ca4c91

File tree

4 files changed

+85
-60
lines changed

4 files changed

+85
-60
lines changed

phpinsights.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use NunoMaduro\PhpInsights\Domain\Insights\ForbiddenNormalClasses;
6+
use ObjectCalisthenics\Sniffs\Files\ClassTraitAndInterfaceLengthSniff;
67
use ObjectCalisthenics\Sniffs\Metrics\MaxNestingLevelSniff;
78
use ObjectCalisthenics\Sniffs\Metrics\MethodPerClassLimitSniff;
89
use PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\UselessOverridingMethodSniff;
@@ -81,14 +82,19 @@
8182
],
8283

8384
'config' => [
85+
LineLengthSniff::class => [
86+
'lineLimit' => 110,
87+
'absoluteLineLimit' => 150,
88+
'ignoreComments' => false,
89+
],
8490
MaxNestingLevelSniff::class => [
8591
'exclude' => [
8692
'src/Objects/Geometry.php',
8793
],
8894
],
8995
UnusedParameterSniff::class => [
9096
'exclude' => [
91-
'src/Objects/Geometry.php',
97+
'src/Objects/GeometryCast.php',
9298
],
9399
],
94100
MethodPerClassLimitSniff::class => [
@@ -97,10 +103,10 @@
97103
'src/Objects/Geometry.php',
98104
],
99105
],
100-
LineLengthSniff::class => [
101-
'lineLimit' => 110,
102-
'absoluteLineLimit' => 150,
103-
'ignoreComments' => false,
106+
ClassTraitAndInterfaceLengthSniff::class => [
107+
'exclude' => [
108+
'src/Builders/SpatialBuilder.php',
109+
],
104110
],
105111
],
106112

src/Builders/SpatialBuilder.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010

1111
class SpatialBuilder extends Builder
1212
{
13-
public function withDistance(string $column, Geometry | string $geometryOrColumn, string $alias = 'distance'): self
14-
{
13+
public function withDistance(
14+
string $column,
15+
Geometry | string $geometryOrColumn,
16+
string $alias = 'distance'
17+
): self {
1518
$geometryOrColumn = $this->toExpression($geometryOrColumn);
1619

1720
if (! $this->getQuery()->columns) {
@@ -28,8 +31,12 @@ public function withDistance(string $column, Geometry | string $geometryOrColumn
2831
);
2932
}
3033

31-
public function whereDistance(string $column, Geometry | string $geometryOrColumn, string $operator, int | float $distance): self
32-
{
34+
public function whereDistance(
35+
string $column,
36+
Geometry | string $geometryOrColumn,
37+
string $operator,
38+
int | float $distance
39+
): self {
3340
$geometryOrColumn = $this->toExpression($geometryOrColumn);
3441

3542
return $this->whereRaw(
@@ -43,8 +50,11 @@ public function whereDistance(string $column, Geometry | string $geometryOrColum
4350
);
4451
}
4552

46-
public function orderByDistance(string $column, Geometry | string $geometryOrColumn, string $direction = 'asc'): self
47-
{
53+
public function orderByDistance(
54+
string $column,
55+
Geometry | string $geometryOrColumn,
56+
string $direction = 'asc'
57+
): self {
4858
$geometryOrColumn = $this->toExpression($geometryOrColumn);
4959

5060
return $this->orderByRaw(

src/Objects/Geometry.php

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
99
use Illuminate\Contracts\Support\Arrayable;
1010
use Illuminate\Contracts\Support\Jsonable;
11-
use Illuminate\Database\Eloquent\Model;
1211
use Illuminate\Database\Query\Expression;
1312
use JsonSerializable;
1413
use MatanYadaev\EloquentSpatial\Exceptions\InvalidTypeException;
@@ -91,53 +90,6 @@ abstract public function getCoordinates(): array;
9190
*/
9291
public static function castUsing(array $arguments): CastsAttributes
9392
{
94-
$className = static::class;
95-
96-
return new class($className) implements CastsAttributes {
97-
private string $className;
98-
99-
public function __construct(string $className)
100-
{
101-
$this->className = $className;
102-
}
103-
104-
/**
105-
* @param Model $model
106-
* @param string $key
107-
* @param string|null $wkt
108-
* @param array<string, mixed> $attributes
109-
*
110-
* @return Geometry|null
111-
*/
112-
public function get($model, string $key, $wkt, array $attributes): ?Geometry
113-
{
114-
if (! $wkt) {
115-
return null;
116-
}
117-
118-
return $this->className::fromWkt($wkt);
119-
}
120-
121-
/**
122-
* @param Model $model
123-
* @param string $key
124-
* @param Geometry|null $geometry
125-
* @param array<string, mixed> $attributes
126-
*
127-
* @return Expression|string|null
128-
*/
129-
public function set($model, string $key, $geometry, array $attributes): Expression | string | null
130-
{
131-
if (! $geometry) {
132-
return null;
133-
}
134-
135-
if (! ($geometry instanceof $this->className)) {
136-
throw new InvalidTypeException($this->className, $geometry);
137-
}
138-
139-
return $geometry->toWkt();
140-
}
141-
};
93+
return new GeometryCast(static::class);
14294
}
14395
}

src/Objects/GeometryCast.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace MatanYadaev\EloquentSpatial\Objects;
4+
5+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Query\Expression;
8+
use MatanYadaev\EloquentSpatial\Exceptions\InvalidTypeException;
9+
10+
class GeometryCast implements CastsAttributes
11+
{
12+
private string $className;
13+
14+
public function __construct(string $className)
15+
{
16+
$this->className = $className;
17+
}
18+
19+
/**
20+
* @param Model $model
21+
* @param string $key
22+
* @param string|null $wkt
23+
* @param array<string, mixed> $attributes
24+
*
25+
* @return Geometry|null
26+
*/
27+
public function get($model, string $key, $wkt, array $attributes): ?Geometry
28+
{
29+
if (! $wkt) {
30+
return null;
31+
}
32+
33+
return $this->className::fromWkt($wkt, false);
34+
}
35+
36+
/**
37+
* @param Model $model
38+
* @param string $key
39+
* @param Geometry|null $geometry
40+
* @param array<string, mixed> $attributes
41+
*
42+
* @return Expression|string|null
43+
* @throws InvalidTypeException
44+
*/
45+
public function set($model, string $key, $geometry, array $attributes): Expression | string | null
46+
{
47+
if (! $geometry) {
48+
return null;
49+
}
50+
51+
if (! ($geometry instanceof $this->className)) {
52+
throw new InvalidTypeException($this->className, $geometry);
53+
}
54+
55+
return $geometry->toWkt();
56+
}
57+
}

0 commit comments

Comments
 (0)