Skip to content

Commit

Permalink
Merge pull request #45 from sandcore-dev/json-filter-null-values
Browse files Browse the repository at this point in the history
Filter out null values in toArray/getJSON
  • Loading branch information
Sibyx authored Nov 24, 2020
2 parents 6b016de + 58d5eb9 commit 1c1b407
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 365 deletions.
17 changes: 17 additions & 0 deletions src/phpGPX/Helpers/SerializationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,21 @@ public static function serialize($object)
return $object != null ? $object->toArray() : null;
}
}

public static function filterNotNull(array $array)
{
foreach ($array as &$item) {
if (!is_array($item)) {
continue;
}

$item = self::filterNotNull($item);
}

$array = array_filter($array, function ($item) {
return $item !== null && (!is_array($item) || count($item));
});

return $array;
}
}
4 changes: 2 additions & 2 deletions src/phpGPX/Models/GpxFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ public function __construct()
*/
public function toArray()
{
return [
return SerializationHelper::filterNotNull([
'creator' => SerializationHelper::stringOrNull($this->creator),
'metadata' => SerializationHelper::serialize($this->metadata),
'waypoints' => SerializationHelper::serialize($this->waypoints),
'routes' => SerializationHelper::serialize($this->routes),
'tracks' => SerializationHelper::serialize($this->tracks),
'extensions' => SerializationHelper::serialize($this->extensions)
];
]);
}

/**
Expand Down
172 changes: 2 additions & 170 deletions tests/LoadFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,14 @@ public function testLoadXmlFileGeneratedByTimezero()
private function createExpectedArray()
{
return [
'creator' => null,
'metadata' => null,
'waypoints' => [
[
'lat' => 49.3636333333086,
'lon' => 0.0800866666666667,
'ele' => null,
'time' => '2014-12-13T16:32:51+00:00',
'magvar' => null,
'geoidheight' => null,
'name' => 'Event 0000',
'cmt' => null,
'desc' => null,
'src' => null,
'link' => [],
'sym' => null,
'type' => null,
'fix' => null,
'sat' => null,
'hdop' => null,
'vdop' => null,
'pdop' => null,
'ageofdgpsdata' => null,
'dgpsid' => null,
'difference' => null,
'distance' => null,
'cmt' => '',
'extensions' => [
'trackpoint' => null,
'unsupported' => [
'MxTimeZeroSymbol' => 10,
'color' => -16744448,
Expand All @@ -60,47 +40,21 @@ private function createExpectedArray()
[
'lat' => 49.3636333333086,
'lon' => 0.0800866666666667,
'ele' => null,
'time' => '2014-12-13T16:32:52+00:00',
'magvar' => null,
'geoidheight' => null,
'name' => 'Event 0001',
'cmt' => null,
'desc' => null,
'src' => null,
'link' => [],
'sym' => null,
'type' => null,
'fix' => null,
'sat' => null,
'hdop' => null,
'vdop' => null,
'pdop' => null,
'ageofdgpsdata' => null,
'dgpsid' => null,
'difference' => null,
'distance' => null,
'cmt' => '',
'extensions' => [
'trackpoint' => null,
'unsupported' => [
'MxTimeZeroSymbol' => 10,
'color' => -16744448,
],
],
],
],
'routes' => [],
'tracks' => [
[
'name' => 'Ownship',
'cmt' => null,
'desc' => null,
'src' => null,
'link' => [],
'number' => null,
'type' => null,
'extensions' => [
'trackpoint' => null,
'unsupported' => [
'guid' => 201,
],
Expand All @@ -111,80 +65,23 @@ private function createExpectedArray()
[
'lat' => 49.3635449998312,
'lon' => 0.0801483333364938,
'ele' => null,
'time' => '2010-01-01T14:48:37+00:00',
'magvar' => null,
'geoidheight' => null,
'name' => null,
'cmt' => null,
'desc' => null,
'src' => null,
'link' => array(),
'sym' => null,
'type' => null,
'fix' => null,
'sat' => null,
'hdop' => null,
'vdop' => null,
'pdop' => null,
'ageofdgpsdata' => null,
'dgpsid' => null,
'difference' => null,
'distance' => 0.0,
'extensions' => null,
],
[
'lat' => 49.3635350651798,
'lon' => 0.0801416666698513,
'ele' => null,
'time' => '2010-01-01T14:48:40+00:00',
'magvar' => null,
'geoidheight' => null,
'name' => null,
'cmt' => null,
'desc' => null,
'src' => null,
'link' => array(),
'sym' => null,
'type' => null,
'fix' => null,
'sat' => null,
'hdop' => null,
'vdop' => null,
'pdop' => null,
'ageofdgpsdata' => null,
'dgpsid' => null,
'difference' => 1.2055693602077022,
'distance' => 1.2055693602077022,
'extensions' => null,
],
[
'lat' => 49.3635266991555,
'lon' => 0.0801333333365323,
'ele' => null,
'time' => '2010-01-01T14:48:46+00:00',
'magvar' => null,
'geoidheight' => null,
'name' => null,
'cmt' => null,
'desc' => null,
'src' => null,
'link' => array(),
'sym' => null,
'type' => null,
'fix' => null,
'sat' => null,
'hdop' => null,
'vdop' => null,
'pdop' => null,
'ageofdgpsdata' => null,
'dgpsid' => null,
'difference' => 1.1088552014759407,
'distance' => 2.314424561683643,
'extensions' => null,
],
],
'extensions' => null,
'stats' => [
'distance' => 2.314424561683643,
'avgSpeed' => 0.2571582846315159,
Expand Down Expand Up @@ -214,14 +111,7 @@ private function createExpectedArray()
],
[
'name' => 'Ownship',
'cmt' => null,
'desc' => null,
'src' => null,
'link' => [],
'number' => null,
'type' => null,
'extensions' => [
'trackpoint' => null,
'unsupported' => [
'guid' => 102,
],
Expand All @@ -232,80 +122,23 @@ private function createExpectedArray()
[
'lat' => 49.4574117319429,
'lon' => 0.0343682156842231,
'ele' => null,
'time' => '2016-04-03T14:13:09+00:00',
'magvar' => null,
'geoidheight' => null,
'name' => null,
'cmt' => null,
'desc' => null,
'src' => null,
'link' => array(),
'sym' => null,
'type' => null,
'fix' => null,
'sat' => null,
'hdop' => null,
'vdop' => null,
'pdop' => null,
'ageofdgpsdata' => null,
'dgpsid' => null,
'difference' => null,
'distance' => 0.0,
'extensions' => null,
],
[
'lat' => 49.4573966992346,
'lon' => 0.0343466078409025,
'ele' => null,
'time' => '2016-04-03T14:13:10+00:00',
'magvar' => null,
'geoidheight' => null,
'name' => null,
'cmt' => null,
'desc' => null,
'src' => null,
'link' => array(),
'sym' => null,
'type' => null,
'fix' => null,
'sat' => null,
'hdop' => null,
'vdop' => null,
'pdop' => null,
'ageofdgpsdata' => null,
'dgpsid' => null,
'difference' => 2.2876315307770505,
'distance' => 2.2876315307770505,
'extensions' => null,
],
[
'lat' => 49.4573700325059,
'lon' => 0.0342948235267376,
'ele' => null,
'time' => '2016-04-03T14:13:12+00:00',
'magvar' => null,
'geoidheight' => null,
'name' => null,
'cmt' => null,
'desc' => null,
'src' => null,
'link' => array(),
'sym' => null,
'type' => null,
'fix' => null,
'sat' => null,
'hdop' => null,
'vdop' => null,
'pdop' => null,
'ageofdgpsdata' => null,
'dgpsid' => null,
'difference' => 4.775098771720203,
'distance' => 7.062730302497254,
'extensions' => null,
],
],
'extensions' => null,
'stats' => [
'distance' => 7.062730302497254,
'avgSpeed' => 2.354243434165751,
Expand Down Expand Up @@ -334,7 +167,6 @@ private function createExpectedArray()
],
],
],
'extensions' => null,
];
}
}
Loading

0 comments on commit 1c1b407

Please sign in to comment.