Skip to content

Commit

Permalink
Merge pull request #7 from mvdnbrk/service-point-to-array
Browse files Browse the repository at this point in the history
Convert service point to array.
  • Loading branch information
mvdnbrk authored Mar 20, 2019
2 parents 08b7fa2 + 6b87b18 commit 865e517
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Resources/ServicePoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,21 @@ public function setGeoLocationAttribute($value)
$this->longitude = $collection->get('longitude');
});
}

/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
return collect(parent::toArray())
->merge([
'distance' => $this->distanceForHumans()
])
->reject(function ($value) {
return empty($value);
})
->all();
}
}
42 changes: 42 additions & 0 deletions tests/Unit/Resources/ServicePointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,46 @@ public function it_can_get_distance_in_a_human_readable_format()
$servicepoint->distance = 11500;
$this->assertEquals('12 km', $servicepoint->distanceForHumans());
}

/** @test */
public function to_array()
{
$servicepoint = new ServicePoint([
'id' => 'testcode1234',
'name' => 'Test name',
'latitude' => 1.11,
'longitude' => 2.22,
'distance' => 100,
]);

$array = $servicepoint->toArray();

$this->assertIsArray($array);
$this->assertEquals('testcode1234', $array['id']);
$this->assertEquals('Test name', $array['name']);
$this->assertEquals(1.11, $array['latitude']);
$this->assertEquals(2.22, $array['longitude']);
$this->assertEquals('100 meter', $array['distance']);
}

/** @test */
public function to_array_removes_empty_attributes()
{
$servicepoint = new ServicePoint([
'id' => null,
'name' => 'Test name',
'latitude' => null,
'longitude' => null,
'distance' => null,
]);

$array = $servicepoint->toArray();

$this->assertIsArray($array);
$this->assertArrayNotHasKey('id', $array);
$this->assertEquals('Test name', $array['name']);
$this->assertArrayNotHasKey('latitude', $array);
$this->assertArrayNotHasKey('longitude', $array);
$this->assertArrayNotHasKey('distance', $array);
}
}

0 comments on commit 865e517

Please sign in to comment.