Skip to content

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

Merged
merged 3 commits into from
Dec 12, 2022
Merged

Conversation

trin4ik
Copy link
Contributor

@trin4ik trin4ik commented Dec 7, 2022

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 use Geometry::fromJson() to serialized array.

Copy link
Owner

@MatanYadaev MatanYadaev left a 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.

@@ -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));
Copy link
Owner

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.

@trin4ik
Copy link
Contributor Author

trin4ik commented Dec 12, 2022

hi, tests added, method fromArray created

Copy link
Owner

@MatanYadaev MatanYadaev left a 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.

Comment on lines 139 to 141
if (! isset($geometry['type'])) {
throw new OutOfBoundsException('Your geometry doesn\'t contain "type"');
}
Copy link
Owner

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
Copy link
Owner

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

Comment on lines 74 to 87
$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());
Copy link
Owner

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.

Suggested change
$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 {
Copy link
Owner

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.

@trin4ik
Copy link
Contributor Author

trin4ik commented Dec 12, 2022

  • codestyle fixed
  • throw OutOfBoundsException removed
  • API documented
  • test moved and fixed.

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

image

@MatanYadaev MatanYadaev changed the title Unserialize array-to-model with geometry Cast geometry from array Dec 12, 2022
@MatanYadaev MatanYadaev merged commit 904a0a8 into MatanYadaev:master Dec 12, 2022
@MatanYadaev
Copy link
Owner

@trin4ik Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants