wearesho-team/google-autocomplete 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wearesho-team/google-autocomplete

Composer 安装命令:

composer require wearesho-team/google-autocomplete

包简介

Service for searching cities and streets using passed input value

README 文档

README

Build Status codecov

This library allows you to search cities/streets of concrete country using google api.

Changelog

Installation

composer require wearesho-team/google-autocomplete

Usage

Create configuration

<?php

$config = new \Wearesho\GoogleAutocomplete\Config(
    $apiKey = 'your personal api key',
    $country = 'ua', // optional
    $requestUrl = 'https://google.com/' // optional
);

Or use Environment Config to load variables from environment

Variable Required Default value Description
GOOGLE_SERVICE_AUTOCOMPLETE_URL no https://maps.googleapis.com/maps/api/place/autocomplete/json url for google-autocomplete-api service
GOOGLE_SERVICE_AUTOCOMPLETE_COUNTRY no ua ISO alpha-2 country code in lower case
GOOGLE_SERVICE_AUTOCOMPLETE_KEY yes your private key
<?php

$config = new \Wearesho\GoogleAutocomplete\EnvironmentConfig('GOOGLE_SERVICE_AUTOCOMPLETE');

Create service

<?php

/** @var \Wearesho\GoogleAutocomplete\ConfigInterface $config */

$service = new \Wearesho\GoogleAutocomplete\Service(
    $config,
    new \GuzzleHttp\Client()
);

Create search data entity

Session token

A random string which identifies an autocomplete session for billing purposes for user In google-docs sad if this parameter is omitted from an autocomplete request, the request is billed independently. So this service is binding to use it.

Recommended to use hash string.

<?php

$token = 'any_random_string';

Searching cities

<?php

use Wearesho\GoogleAutocomplete;

$searchQuery = new GoogleAutocomplete\Queries\CitySearch(
    $token,
    'Value from input',
    $language = GoogleAutocomplete\Enums\SearchLanguage::RU(),
    $mode = GoogleAutocomplete\Enums\SearchMode::SHORT() // optional
);

Searching streets

<?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
);

If you want to customize your queries use Query Interfaces

Receive suggestions

<?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();

Search mode

Service have 2 modes for searching results:

<?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();

This parameter is optional. By default set FULL() mode.

Yii2 configuration

If you need to configure your yii2 application, you can use this yii extension, that depend on this package

Contributing

To contribute you need to fork this repo and than create a pull request

Search language

Add language code to 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';
}

Frameworks configurations

Add directory with name of needs framework to source directory and create your Bootstrap.php file

Authors

License

统计信息

  • 总下载量: 434
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2018-10-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固