Skip to content
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

Add location component #28

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public static function flowButton(string $token, array $data): Component\FlowBut
{
return new Component\FlowButton($token, $data);
}

public static function location(string $name, string $address, float $latitude, float $longitude): Component\Location
{
return new Component\Location($name, $address, $latitude, $longitude);
}
}
32 changes: 32 additions & 0 deletions src/Component/Location.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace NotificationChannels\WhatsApp\Component;

class Location extends Component
{
protected string $name;
protected string $address;
protected float $latitude;
protected float $longitude;

public function __construct(string $name, string $address, float $latitude, float $longitude)
{
$this->name = $name;
$this->address = $address;
$this->latitude = $latitude;
$this->longitude = $longitude;
}

public function toArray(): array
{
return [
'type' => 'location',
'location' => [
'latitude' => (string) $this->latitude,
'longitude' => (string) $this->longitude,
'name' => $this->name,
'address' => $this->address,
],
];
}
}
30 changes: 30 additions & 0 deletions tests/Component/LocationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace NotificationChannels\WhatsApp\Test\Component;

use NotificationChannels\WhatsApp\Component\Location;
use PHPUnit\Framework\TestCase;

final class LocationTest extends TestCase
{
/** @test */
public function location_is_valid()
{
$name = 'name';
$address = 'address';
$latitude = 1.234567;
$longitude = 7.891011;

$currency = new Location($name, $address, $latitude, $longitude);
$expectedValue = [
'type' => 'location',
'location' => [
'latitude' => (string) $latitude,
'longitude' => (string) $longitude,
'name' => $name,
'address' => $address,
],
];
$this->assertEquals($expectedValue, $currency->toArray());
}
}
8 changes: 8 additions & 0 deletions tests/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public function it_can_return_a_video_component()
$this->assertInstanceOf(Component\Video::class, $component);
}

/** @test */
public function it_can_return_a_location_component()
{
$component = Component::location('My Place', 'Liepaja, Latvia', 56.51078, 21.00212);

$this->assertInstanceOf(Component\Location::class, $component);
}

/** @test */
public function it_can_return_a_url_button_component()
{
Expand Down