wearesho-team / google-autocomplete
Service for searching cities and streets using passed input value
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-12-14 04:05:17 UTC
README
This library allows you to search cities/streets of concrete country using google api.
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
<?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