germania-kg/geodata

地理数据接口和特性

3.6.0 2023-01-04 14:49 UTC

README

Germania KG · GeoData

Packagist PHP version Tests

安装

$ 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 提供了公共的 latitudelongitude 属性以及由 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 并实现了 GeoDataInterfaceGeoDataProviderInterface

<?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 并过滤出 GeoDataInterfaceGeoDataProviderInterface 项目,这些项目的 getLatitudegetLongitude 结果不为空。

开发

$ 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