germania-kg / geodata
地理数据接口和特性
Requires
- php: ^7.4|^8.0
- guzzlehttp/guzzle: ^6.0|^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0|^3.0
- php-coveralls/php-coveralls: ^2.0
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^8.0|^9.0
- spatie/phpunit-watcher: ^1.8
- dev-master
- 3.6.0
- 3.5.8
- 3.5.7
- 3.5.6
- 3.5.5
- 3.5.4
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5.0
- 3.4.5
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.1
- 3.2.0
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.1.1
- 2.1.0
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.1
- 1.0.0
- dev-dependabot/composer/guzzlehttp/psr7-2.5.0
- dev-develop
This package is auto-updated.
Last update: 2024-09-19 23:53:43 UTC
README
Germania KG · GeoData
安装
$ composer require germania-kg/geodata
接口
GeoDataProviderInterface
<?php use Germania\GeoData\GeoDataProviderInterface;
/** * @return null|GeoDataInterface */ public function getGeoData();
GeoDataInterface
<?php use Germania\GeoData\GeoDataInterface;
/** * @return float|null */ public function getLatitude(); /** * @return float|null */ public function getLongitude(); /** * @return float[] */ public function getLatLon(); /** * @return string|null */ public function getSource(); /** * @return string|null */ public function getStatus();
特性
GeoDataProviderTrait
GeoDataProviderTrait 提供了一个公共的 geodata 属性以及由 GeoDataProviderInterface 规定的 getGeoData 方法:
<?php use Germania\GeoData\GeoDataProviderTrait; class MyGeoDataProvider { use GeoDataProviderTrait; } $object = new MyGeoDataProvider; // Property or GeoDataProviderInterface method $object->geodata; $object->getGeoData();
类
GeoDataAbstract
GeoDataAbstract 提供了公共的 latitude 和 longitude 属性以及由 GeoDataInterface 规定的方法
<?php use Germania\GeoData\GeoDataAbstract; class MyGeoData extends GeoDataAbstract { use GeoDataTrait; } $object = new MyGeoData; // Properties echo $object->latitude; echo $object->longitude; // GeoDataProviderInterface methods echo $object->getLatitude(); echo $object->getLongitude(); $coords = $object->getLatLon();
GeoData
GeoData 类继承自 GeoDataAbstract 并实现了 GeoDataInterface 和 GeoDataProviderInterface
<?php use Germania\GeoData\GeoData; // "Null" object $geo = new GeoData(); $coords = $object->getLatLon(); // [ null, null] $coords = $object->getLatitude(); // null $coords = $object->getLongitude(); // null echo $geo->getSource(); // null // With real data $latitude = 54.0; $longitude = 10.0; $description = "provided by Google Maps"; $geo = new GeoData( $latitude, $longitude, $description); $geo->setSource("Corrected manually"); $get->setStatus("Not too exact"); $coords = $object->getLatLon(); // [ 54.0, 10.0] echo $geo->getSource(); // "Corrected manually"
工厂
GeoDataFactory
GeoDataFactory 类提供了一个 fromArray 方法
<?php use Germania\GeoData\GeoDataFactory; use Germania\GeoData\GeoData; $factory = new GeoDataFactory; // All these fields default to null $geodata = $factory([ 'latitude' => 54, 'longitude' => 10, 'source' => "Test case", 'status' => "OK" ]);
GuzzleGeoDataFactory
GuzzleGeoDataFactory 是 Germania 地理编码 API 的客户端。它实现了 StringGeoDataFactoryInterface 并需要配置好的 Guzzle,用于调用 Germania 的 GeoCoder API。
抱歉,API 不是公开的。 您可以使用 Factory 类来自定义基于 HTTP 客户端的 GeoData 工厂。
<?php use Germania\GeoData\GuzzleGeoDataFactory; use GuzzleHttp\Client as GuzzleClient; $guzzle = new GuzzleClient( ... ); $factory = new GuzzleGeoDataFactory($guzzle); $geodata = $factory->fromString("Musterstraße 1, 12345 Musterstadt"); echo get_class( $geodata ); // Germania\GeoData\GeoData
异常: 如果 Guzzle 客户端抛出异常或 API 响应无效,请注意这些异常
<?php use Germania\GeoData\GeoDataExceptionInterface; use Germania\GeoData\GeoDataFactoryRuntimeException; // For 404 ClientExceptions, extends GeoDataFactoryRuntimeException use Germania\GeoData\GeoDataFactoryNotFoundException;
过滤器
NotEmptyGeoDataFilterIterator
接受任何 Traversable 并过滤出 GeoDataInterface 或 GeoDataProviderInterface 项目,这些项目的 getLatitude 和 getLongitude 结果不为空。
开发
$ git clone https://github.com/GermaniaKG/Geodata.git
$ cd Geodata
$ composer install
单元测试
可以将 phpunit.xml.dist
复制到 phpunit.xml
并根据您的需求进行修改,或者保持原样。运行 PhpUnit 测试或 composer 脚本,如下所示:
$ composer test # or $ vendor/bin/phpunit