PHP code example of wearesho-team / google-autocomplete

1. Go to this page and download the library: Download wearesho-team/google-autocomplete library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wearesho-team / google-autocomplete example snippets




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



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



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

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




$token = 'any_random_string';



use Wearesho\GoogleAutocomplete;

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



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



/** @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();



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



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';
}