Skip to content

Commit

Permalink
add place
Browse files Browse the repository at this point in the history
  • Loading branch information
daniwebdev committed Oct 31, 2023
1 parent 2e07266 commit 3a1d679
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"scripts": {
"pest": "./vendor/bin/pest",
"test-region": "./vendor/bin/pest tests/IndonesianRegionTest.php",
"test-idx": "./vendor/bin/pest tests/StockIDXTest.php"
"test-idx": "./vendor/bin/pest tests/StockIDXTest.php",
"test-places": "./vendor/bin/pest tests/PlacesTest.php"
}
}
5 changes: 5 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public function createIndonesianRegion()
{
return new \GOAPI\IO\IndonesianRegion($this->getHttpClient());
}

public function createPlaces()
{
return new \GOAPI\IO\Places($this->getHttpClient());
}
}
4 changes: 3 additions & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function take($count)
return new static(array_slice($this->items, 0, $count));
}

public function map(callable $callback)
public function map(callable $callback): Collection
{
return new static(array_map($callback, $this->items));
}
Expand All @@ -68,4 +68,6 @@ public function getIterator(): \Traversable
return new \ArrayIterator($this->items);
}



}
42 changes: 42 additions & 0 deletions src/Places.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
namespace GOAPI\IO;

class Places
{
private $client;
private $apiBaseUrl = '/places';

public function __construct(\GuzzleHttp\Client $client) {
$this->client = $client;
}

private function makeRequest($endpoint, $params = []) {
try {
$response = $this->client->request('GET', $this->apiBaseUrl . $endpoint, [
'query' => $params,
]);

// Assuming the API returns JSON response
return json_decode($response->getBody(), true);
} catch (\Exception $e) {
// Handle exceptions and errors here
// You might want to log the error or throw a custom exception
return ["error" => $e->getMessage()];
}
}

public function search($keyword) {
$response = $this->makeRequest('', ['search' => $keyword]);

return (new Collection($response['data']['results']))->map(function($item) {
return new \GOAPI\IO\Resources\Places\Place(
id: $item['id'],
displayName: $item['displayName'],
lng: $item['lng'],
lat: $item['lat'],
coordinate: $item['coordinate'],
);
});
}

}
13 changes: 13 additions & 0 deletions src/Resources/Places/Place.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace GOAPI\IO\Resources\Places;

class Place {

function __construct(
public $id,
public $displayName,
public $lng,
public $lat,
public $coordinate,
) {}
}
8 changes: 8 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
require './vendor/autoload.php';

$collection = new \GOAPI\IO\Collection([
'dam','da','df',
]);

echo $collection[0];
21 changes: 21 additions & 0 deletions tests/PlacesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

$client = new \GOAPI\IO\Client([
'api_key' => getenv('GOAPI_IO_API_KEY'), // your api key
]);

test('get client instance', function () use($client) {
$instance = $client->createPlaces();

expect($instance instanceof \GOAPI\IO\Places)->toBeTrue();
});

test('get search', function () use($client) {
$instance = $client->createPlaces();

$response = $instance->search("Jakarta");


expect($response->values())->toBeArray();
expect($response[0] instanceof \GOAPI\IO\Resources\Places\Place)->toBeTrue();
});

0 comments on commit 3a1d679

Please sign in to comment.