AbstractArrayDumper.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the Geocoder package.
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @license MIT License
  9. */
  10. namespace Geocoder\Dumper;
  11. use Geocoder\Location;
  12. /**
  13. * @author Tomas Norkūnas <norkunas.tom@gmail.com>
  14. */
  15. abstract class AbstractArrayDumper
  16. {
  17. /**
  18. * @param Location $location
  19. *
  20. * @return array
  21. */
  22. protected function getArray(Location $location): array
  23. {
  24. $properties = array_filter($location->toArray(), function ($value) {
  25. return !empty($value);
  26. });
  27. unset(
  28. $properties['latitude'],
  29. $properties['longitude'],
  30. $properties['bounds']
  31. );
  32. if ([] === $properties) {
  33. $properties = null;
  34. }
  35. $lat = 0;
  36. $lon = 0;
  37. if (null !== $coordinates = $location->getCoordinates()) {
  38. $lat = $coordinates->getLatitude();
  39. $lon = $coordinates->getLongitude();
  40. }
  41. $array = [
  42. 'type' => 'Feature',
  43. 'geometry' => [
  44. 'type' => 'Point',
  45. 'coordinates' => [$lon, $lat],
  46. ],
  47. 'properties' => $properties,
  48. ];
  49. if (null !== $bounds = $location->getBounds()) {
  50. $array['bounds'] = $bounds->toArray();
  51. }
  52. return $array;
  53. }
  54. }