Skip to content

Commit

Permalink
Make examples more idiomatic
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthetechie committed Sep 5, 2024
1 parent 89b252d commit 3e98cf5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 76 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,3 @@ try {
```

For more examples, check out the [integration tests](https://github.com/stadiamaps/stadiamaps-api-php/tree/main/test/Api).
Note that most of our examples use associative arrays for brevity,
but you can also construct empty request classes and uss methods for better IDE autocompletion.


6 changes: 5 additions & 1 deletion test/Api/GeospatialApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OpenAPI\Client\Api\GeospatialApi;
use OpenAPI\Client\ApiException;
use GuzzleHttp;
use OpenAPI\Client\Model\Coordinate;
use OpenAPI\Client\Model\HeightRequest;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -73,7 +74,10 @@ public static function tearDownAfterClass(): void
*/
public function testElevation()
{
$req = new HeightRequest(array('id' => 'Seoul', 'shape' => [$this->seoul]));
$req = (new HeightRequest())
->setId('Seoul')
->setShape([new Coordinate($this->seoul)]);

$result = $this->apiInstance->elevation($req);
self::assertEquals($req['id'], $result->getId());
self::assertNotCount(0, $result->getHeight());
Expand Down
133 changes: 62 additions & 71 deletions test/Api/RoutingApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
use GuzzleHttp;
use OpenAPI\Client\Model\AnnotationFilters;
use OpenAPI\Client\Model\AutoCostingOptions;
use OpenAPI\Client\Model\Contour;
use OpenAPI\Client\Model\Coordinate;
use OpenAPI\Client\Model\CostingModel;
use OpenAPI\Client\Model\CostingOptions;
use OpenAPI\Client\Model\DistanceUnit;
use OpenAPI\Client\Model\IsochroneRequest;
use OpenAPI\Client\Model\MapMatchRequest;
use OpenAPI\Client\Model\MatrixRequest;
use OpenAPI\Client\Model\MatrixWaypoint;
use OpenAPI\Client\Model\NearestRoadsRequest;
use OpenAPI\Client\Model\OptimizedRouteRequest;
use OpenAPI\Client\Model\RouteRequest;
Expand Down Expand Up @@ -84,24 +87,16 @@ public static function tearDownAfterClass(): void
*
* Calculate areas of equal travel time from a location.
*
* @throws ApiException
*/
public function testIsochrone()
{
$req = new IsochroneRequest(array('id' => 'kesklinn',
'locations' => [
[
'lat' => 59.436884,
'lon' => 24.742595
]
],
'costing' => 'pedestrian',
'contours' => [
array(
'time' => 5,
'color' => 'aabbcc'
)
],
'polygons' => true));
$req = (new IsochroneRequest())
->setId('kesklinn')
->setLocations([(new Coordinate())->setLat(59.436884)->setLon(24.742595)])
->setCosting(CostingModel::PEDESTRIAN)
->setContours([(new Contour())->setTime(5)->setColor('aabbcc')])
->setPolygons(true);
$result = $this->apiInstance->isochrone($req);
self::assertEquals($req->getId(), $result->getId());
self::assertEquals("FeatureCollection", $result->getType());
Expand All @@ -117,10 +112,11 @@ public function testIsochrone()
*/
public function testMapMatch()
{
$req = new MapMatchRequest(array('id' => 'mapMatch',
'encoded_polyline' => '_grbgAh~{nhF?lBAzBFvBHxBEtBKdB?fB@dBZdBb@hBh@jBb@x@\|@x@pB\x@v@hBl@nBPbCXtBn@|@z@ZbAEbAa@~@q@z@QhA]pAUpAVhAPlAWtASpAAdA[dASdAQhAIlARjANnAZhAf@n@`A?lB^nCRbA\xB`@vBf@tBTbCFbARzBZvBThBRnBNrBP`CHbCF`CNdCb@vBX`ARlAJfADhA@dAFdAP`AR`Ah@hBd@bBl@rBV|B?vB]tBCvBBhAF`CFnBXtAVxAVpAVtAb@|AZ`Bd@~BJfA@fAHdADhADhABjAGzAInAAjAB|BNbCR|BTjBZtB`@lBh@lB\|Bl@rBXtBN`Al@g@t@?nAA~AKvACvAAlAMdAU`Ac@hAShAI`AJ`AIdAi@bAu@|@k@p@]p@a@bAc@z@g@~@Ot@Bz@f@X`BFtBXdCLbAf@zBh@fBb@xAb@nATjAKjAW`BI|AEpAHjAPdAAfAGdAFjAv@p@XlAVnA?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lAj@~A^`B^|AXvAVpAXdBh@~Ap@fCh@hB\zBN`Aj@xBFdA@jALbAPbAJdAHdAJbAHbAHfAJhALbA\lBTvBAdC@bC@jCKjASbC?`CM`CDpB\xAj@tB\fA\bAVfAJdAJbAXz@L|BO`AOdCDdA@~B\z@l@v@l@v@l@r@j@t@b@x@b@r@z@jBVfCJdAJdANbCPfCF|BRhBS~BS`AYbAe@~BQdA')
->setCosting(CostingModel::PEDESTRIAN)
->setUnits(DistanceUnit::MI);
$result = $this->apiInstance->mapMatch($req);
self::assertEquals(0, $result->getTrip()->getStatus());
self::assertEquals(ValhallaLongUnits::MILES, $result->getTrip()->getUnits());
Expand All @@ -137,7 +133,9 @@ public function testMapMatch()
*/
public function testNearestRoads()
{
$req = new NearestRoadsRequest(array("locations" => [array("lat" => 59.436884, "lon" => 24.742595)], "verbose" => true));
$req = (new NearestRoadsRequest())
->setLocations([(new Coordinate())->setLat(59.436884)->setLon(24.742595)])
->setVerbose(true);
$result = $this->apiInstance->nearestRoads($req);
self::assertNotCount(0, $result[0]->getEdges());
}
Expand All @@ -151,13 +149,13 @@ public function testNearestRoads()
*/
public function testOptimizedRoute()
{
$req = new OptimizedRouteRequest(array(
'id' => 'optimizedRoute',
'locations' => [$this->locationA, $this->locationB, $this->locationC, $this->locationA],
'costing' => CostingModel::AUTO,
'costing_options' => array(CostingModel::AUTO => array('use_highways' => 0.3)),
'units' => DistanceUnit::MI
));
$req = (new OptimizedRouteRequest())
->setId('optimizedRoute')
->setLocations([new Coordinate($this->locationA), new Coordinate($this->locationB), new Coordinate($this->locationC), new Coordinate($this->locationA)])
->setCosting(CostingModel::AUTO)
->setUnits(DistanceUnit::MI)
->setCostingOptions((new CostingOptions())
->setAuto((new AutoCostingOptions())->setUseHighways(0.3)));
$result = $this->apiInstance->optimizedRoute($req);
self::assertEquals($req->getId(), $result->getId());
self::assertEquals(0, $result->getTrip()->getStatus());
Expand All @@ -175,13 +173,14 @@ public function testOptimizedRoute()
*/
public function testRoute()
{
$req = new RouteRequest(array(
'id' => 'route',
'locations' => [$this->locationA, $this->locationB],
'costing' => CostingModel::AUTO,
'costing_options' => array(CostingModel::AUTO => array('use_highways' => 0.3)),
'units' => DistanceUnit::MI
));
$req = (new RouteRequest())
->setId('route')
->setLocations([$this->locationA, $this->locationB])
->setCosting(CostingModel::AUTO)
->setUnits(DistanceUnit::MI)
->setCostingOptions((new CostingOptions())
->setAuto((new AutoCostingOptions())
->setUseHighways(0.3)));
$result = $this->apiInstance->route($req);
self::assertEquals($req->getId(), $result->getId());
self::assertEquals(0, $result->getTrip()->getStatus());
Expand All @@ -201,24 +200,18 @@ public function testRoute()
*/
public function testRouteWithNavigationAids()
{
$req = new RouteRequest();
$req->setId('route');
$req->setLocations([$this->locationA, $this->locationB]);
$req->setCosting(CostingModel::AUTO);
$req->setUnits(DistanceUnit::MI);
$req->setFormat(RouteRequest::FORMAT_OSRM);
$req->setBannerInstructions(true);

$autoCostingOptions = new AutoCostingOptions();
$autoCostingOptions->setUseHighways(0.3);
$costingOptions = new CostingOptions();
$costingOptions->setAuto($autoCostingOptions);
$req->setCostingOptions($costingOptions);

$filters = new AnnotationFilters();
$filters->setAction(AnnotationFilters::ACTION__INCLUDE);
$filters->setAttributes(['shape_attributes.speed_limit']);
$req->setFilters($filters);
$req = (new RouteRequest())
->setId('route')
->setLocations([$this->locationA, $this->locationB])
->setCosting(CostingModel::AUTO)
->setUnits(DistanceUnit::MI)
->setFormat(RouteRequest::FORMAT_OSRM)
->setBannerInstructions(true)
->setCostingOptions((new CostingOptions())
->setAuto((new AutoCostingOptions())->setUseHighways(0.3)))
->setFilters((new AnnotationFilters())
->setAction(AnnotationFilters::ACTION__INCLUDE)
->setAttributes(['shape_attributes.speed_limit']));

$result = $this->apiInstance->route($req);
self::assertEquals('Ok', $result->getCode());
Expand All @@ -241,18 +234,17 @@ public function testRouteWithNavigationAids()
*/
public function testTimeDistanceMatrix()
{
$req = new MatrixRequest(array(
'id' => 'matrix',
'sources' => [
array('lat' => 58.891957, 'lon' => 22.726262),
array('lat' => 59.1558, 'lon' => 23.762758),
],
'targets' => [
array('lat' => 59.176153, 'lon' => 23.846605),
array('lat' => 59.562853, 'lon' => 23.096114),
],
'costing' => CostingModel::BICYCLE,
));
$req = (new MatrixRequest())
->setId('matrix')
->setSources([
(new MatrixWaypoint())->setLat(58.891957)->setLon(22.726262),
(new MatrixWaypoint())->setLat(59.1558)->setLon(23.762758),
])
->setTargets([
(new MatrixWaypoint())->setLat(59.176153)->setLon(23.846605),
(new MatrixWaypoint())->setLat(59.562853)->setLon(23.096114),
])
->setCosting(CostingModel::BICYCLE);
$result = $this->apiInstance->timeDistanceMatrix($req);
self::assertEquals($req->getId(), $result->getId());
self::assertSameSize($req->getSources(), $result->getSources());
Expand All @@ -270,13 +262,12 @@ public function testTimeDistanceMatrix()
*/
public function testTraceAttributes()
{
$req = new TraceAttributesRequest(array(
'id' => 'traceAttributes',
'encoded_polyline' => '_grbgAh~{nhF?lBAzBFvBHxBEtBKdB?fB@dBZdBb@hBh@jBb@x@\|@x@pB\x@v@hBl@nBPbCXtBn@|@z@ZbAEbAa@~@q@z@QhA]pAUpAVhAPlAWtASpAAdA[dASdAQhAIlARjANnAZhAf@n@`A?lB^nCRbA\xB`@vBf@tBTbCFbARzBZvBThBRnBNrBP`CHbCF`CNdCb@vBX`ARlAJfADhA@dAFdAP`AR`Ah@hBd@bBl@rBV|B?vB]tBCvBBhAF`CFnBXtAVxAVpAVtAb@|AZ`Bd@~BJfA@fAHdADhADhABjAGzAInAAjAB|BNbCR|BTjBZtB`@lBh@lB\|Bl@rBXtBN`Al@g@t@?nAA~AKvACvAAlAMdAU`Ac@hAShAI`AJ`AIdAi@bAu@|@k@p@]p@a@bAc@z@g@~@Ot@Bz@f@X`BFtBXdCLbAf@zBh@fBb@xAb@nATjAKjAW`BI|AEpAHjAPdAAfAGdAFjAv@p@XlAVnA?~A?jAInAPtAVxAXnAf@tBDpBJpBXhBJfBDpAZ|Ax@pAz@h@~@lA|@bAnAd@hAj@tAR~AKxAc@xAShA]hAIdAAjA]~A[v@BhB?dBSv@Ct@CvAI~@Oz@Pv@dAz@lAj@~A^`B^|AXvAVpAXdBh@~Ap@fCh@hB\zBN`Aj@xBFdA@jALbAPbAJdAHdAJbAHbAHfAJhALbA\lBTvBAdC@bC@jCKjASbC?`CM`CDpB\xAj@tB\fA\bAVfAJdAJbAXz@L|BO`AOdCDdA@~B\z@l@v@l@v@l@r@j@t@b@x@b@r@z@jBVfCJdAJdANbCPfCF|BRhBS~BS`AYbAe@~BQdA',
'shape_match' => TraceAttributesRequest::SHAPE_MATCH_MAP_SNAP,
'costing' => CostingModel::PEDESTRIAN,
'units' => DistanceUnit::MI
));
$req = (new TraceAttributesRequest())
->setId('traceAttributes')
->setEncodedPolyline('_grbgAh~{nhF?lBAzBFvBHxBEtBKdB?fB@dBZdBb@hBh@jBb@x@\|@x@pB\x@v@hBl@nBPbCXtBn@|@z@ZbAEbAa@~@q@z@QhA]pAUpAVhAPlAWtASpAAdA[dASdAQhAIlARjANnAZhAf@n@`A?lB^nCRbA\xB`@vBf@tBTbCFbARzBZvBThBRnBNrBP`CHbCF`CNdCb@vBX`ARlAJfADhA@dAFdAP`AR`Ah@hBd@bBl@rBV|B?vB]tBCvBBhAF`CFnBXtAVxAVpAVtAb@|AZ`Bd@~BJfA@fAHdADhADhABjAGzAInAAjAB|BNbCR|BTjBZtB`@lBh@lB\|Bl@rBXtBN`Al@g@t@?nAA~AKvACvAAlAMdAU`Ac@hAShAI`AJ`AIdAi@bAu@|@k@p@]p@a@bAc@z@g@~@Ot@Bz@f@X`BFtBXdCLbAf@zBh@fBb@xAb@nATjAKjAW`BI|AEpAHjAPdAAfAGdAFjAv@p@XlAVnA?~A?jAInAPtAVxAXnAf@tBDpBJpBXhBJfBDpAZ|Ax@pAz@h@~@lA|@bAnAd@hAj@tAR~AKxAc@xAShA]hAIdAAjA]~A[v@BhB?dBSv@Ct@CvAI~@Oz@Pv@dAz@lAj@~A^`B^|AXvAVpAXdBh@~Ap@fCh@hB\zBN`Aj@xBFdA@jALbAPbAJdAHdAJbAHbAHfAJhALbA\lBTvBAdC@bC@jCKjASbC?`CM`CDpB\xAj@tB\fA\bAVfAJdAJbAXz@L|BO`AOdCDdA@~B\z@l@v@l@v@l@r@j@t@b@x@b@r@z@jBVfCJdAJdANbCPfCF|BRhBS~BS`AYbAe@~BQdA')
->setShapeMatch(TraceAttributesRequest::SHAPE_MATCH_WALK_OR_SNAP)
->setCosting(CostingModel::PEDESTRIAN)
->setUnits(DistanceUnit::MI);
$result = $this->apiInstance->traceAttributes($req);
self::assertEquals($req->getId(), $result->getId());
self::assertEquals(ValhallaLongUnits::MILES, $result->getUnits());
Expand Down

0 comments on commit 3e98cf5

Please sign in to comment.