wearesho-team / google-autocomplete
此包的最新版本(2.2.0)没有提供许可证信息。
用于搜索城市和街道的服务,使用传递的输入值
2.2.0
2018-10-24 14:13 UTC
Requires
- php: >7.1
- guzzlehttp/guzzle: ^6.3
- horat1us/environment-config: ^1.2
- myclabs/php-enum: ^1.6
- wearesho-team/base-collection: ^1.0.0
Requires (Dev)
- phpunit/phpunit: ^7.3
- squizlabs/php_codesniffer: ^3.2
- yiisoft/yii2: 2.0.15.1
This package is auto-updated.
Last update: 2024-09-14 03:26:23 UTC
README
此库允许您使用Google API搜索特定国家的城市/街道。
安装
composer require wearesho-team/google-autocomplete
用法
创建配置
<?php $config = new \Wearesho\GoogleAutocomplete\Config( $apiKey = 'your personal api key', $country = 'ua', // optional $requestUrl = 'https://google.com/' // optional );
或者使用 环境配置 从环境中加载变量
<?php $config = new \Wearesho\GoogleAutocomplete\EnvironmentConfig('GOOGLE_SERVICE_AUTOCOMPLETE');
创建服务
<?php /** @var \Wearesho\GoogleAutocomplete\ConfigInterface $config */ $service = new \Wearesho\GoogleAutocomplete\Service( $config, new \GuzzleHttp\Client() );
创建搜索数据实体
会话令牌
一个随机字符串,用于识别用于计费的自动完成会话,对于Google Docs中的用户。如果从自动完成请求中省略此参数,则请求将独立计费。因此,此服务绑定使用它。
建议使用哈希字符串.
<?php $token = 'any_random_string';
搜索城市
<?php use Wearesho\GoogleAutocomplete; $searchQuery = new GoogleAutocomplete\Queries\CitySearch( $token, 'Value from input', $language = GoogleAutocomplete\Enums\SearchLanguage::RU(), $mode = GoogleAutocomplete\Enums\SearchMode::SHORT() // optional );
搜索街道
<?php use Wearesho\GoogleAutocomplete; $searchQuery = new GoogleAutocomplete\Queries\StreetSearch( $token, 'Value from input', $language = GoogleAutocomplete\Enums\SearchLanguage::RU(), $city = 'city name', // optional $type = 'avenue', // optional $mode = GoogleAutocomplete\Enums\SearchMode::SHORT() // optional );
如果您想自定义查询,请使用 查询接口
接收建议
<?php /** @var \Wearesho\GoogleAutocomplete\Service $service */ /** @var \Wearesho\GoogleAutocomplete\Queries\Interfaces\SearchQueryInterface $searchQuery */ $service->load($searchQuery); // invoke query $suggestions = $service->getResults(); // get collection object of locations // or use it fluent $suggestions = $service->load($searchQuery)->getResults(); $values = $suggestions->jsonSerialize();
搜索模式
服务提供 2种模式 搜索结果
<?php use Wearesho\GoogleAutocomplete\Enums\SearchMode; /** * This mode provide service for returning short names of locations * * @example "Paris" * "Kharkov" */ $mode = SearchMode::SHORT(); /** * This mode provide service for returning full names of locations * * @example "Paris, France" * "Kharkov, Kharkiv Oblast, Ukraine" */ $mode = SearchMode::FULL();
此参数是可选的。默认设置为 FULL() 模式。
Yii2配置
如果您需要配置您的yii2应用程序,您可以使用这个 yii扩展,它依赖于此包
贡献
要贡献,您需要fork此存储库,然后创建一个pull请求
搜索语言
将语言代码添加到 SearchLanguage.php
<?php namespace Wearesho\GoogleAutocomplete\Enums; use MyCLabs\Enum\Enum; /** * Interface SearchLanguage * @package Wearesho\GoogleAutocomplete\Enums * * @method static static RU() * @method static static UK() * * @method static static YOUR_LANGUAGE_KEY() */ final class SearchLanguage extends Enum { protected const RU = 'ru'; protected const UK = 'uk'; protected const YOUR_LANGUAGE_KEY = 'language_code'; }
框架配置
将需要框架命名的目录添加到 源目录 并创建您的Bootstrap.php文件