-
Notifications
You must be signed in to change notification settings - Fork 52
Cast geometry from array #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. Please add tests. This package should be 100% covered with tests.
src/GeometryCast.php
Outdated
@@ -62,6 +62,10 @@ public function set($model, string $key, $value, array $attributes): Expression| | |||
return null; | |||
} | |||
|
|||
if (is_array($value) && isset($value['type']) && $value['type'] === class_basename($this->className)) { | |||
$value = Geometry::fromJson(json_encode($value)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a Geometry@fromArray
method that does the same logic and use it here instead.
hi, tests added, method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use 2 spaces instead of 4.
src/Objects/Geometry.php
Outdated
if (! isset($geometry['type'])) { | ||
throw new OutOfBoundsException('Your geometry doesn\'t contain "type"'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this validation isn't necessary, we already validate for correctness of GeoJson here: https://github.com/MatanYadaev/laravel-eloquent-spatial/blob/master/src/Factory.php#L34
* | ||
* @throws OutOfBoundsException | ||
*/ | ||
public static function fromArray(array $geometry): static |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add this method to the API.md
$geometryCollection = new GeometryCollection([ | ||
new Polygon([ | ||
new LineString([ | ||
new Point(0, 180), | ||
new Point(1, 179), | ||
new Point(2, 178), | ||
new Point(3, 177), | ||
new Point(0, 180), | ||
]), | ||
]), | ||
new Point(0, 180), | ||
]); | ||
|
||
$geometryCollectionFromArray = GeometryCollection::fromArray($geometryCollection->toArray()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to keep the tests following the AAA pattern - so anything that is related to arranging the data should be above the "act" part.
$geometryCollection = new GeometryCollection([ | |
new Polygon([ | |
new LineString([ | |
new Point(0, 180), | |
new Point(1, 179), | |
new Point(2, 178), | |
new Point(3, 177), | |
new Point(0, 180), | |
]), | |
]), | |
new Point(0, 180), | |
]); | |
$geometryCollectionFromArray = GeometryCollection::fromArray($geometryCollection->toArray()); | |
$geometryCollection = new GeometryCollection([ | |
new Polygon([ | |
new LineString([ | |
new Point(0, 180), | |
new Point(1, 179), | |
new Point(2, 178), | |
new Point(3, 177), | |
new Point(0, 180), | |
]), | |
]), | |
new Point(0, 180), | |
]); | |
$geometryCollectionArray = $geometryCollection->toArray(); | |
$geometryCollectionFromArray = GeometryCollection::fromArray($geometryCollectionArray); |
expect($geometryCollectionFromJson)->toEqual($geometryCollection); | ||
}); | ||
|
||
it('creates geometry collection from array (serialized model)', function (): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the test to the GeometryTest
because it's not related to a specific geometry type.
also added some test. How u run a tests? i use https://github.com/nektos/act and when i run pest/pest-coverage, i see a lot of coverage errors |
@trin4ik Thanks! |
At now we can do
$serialize = Model::find(35)->toArray();
but cant use$model = new Model($serialize)
. This PR look to values and try to useGeometry::fromJson()
to serialized array.