PHP code example of nsu-soft / yii-dadata
1. Go to this page and download the library: Download nsu-soft/yii-dadata 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/ */
nsu-soft / yii-dadata example snippets
'modules' => [
'dadata' => [
'class' => 'nsusoft\dadata\Module',
'token' => 'enter-your-dadata-token',
'secret' => 'enter-your-dadata-secret',
'cachePriority' => [DbHandler::class],
],
],
namespace app\forms;
use app\models\City;
use app\models\Region;
use nsusoft\dadata\helpers\CleanHelper;
use nsusoft\dadata\plugins\TimezoneConverter;
use nsusoft\dadata\validators\AddressValidator;
use yii\base\Model;
class AddressForm extends Model
{
/**
* @var string
*/
public $address;
/**
* @var City
*/
private $city;
/**
* @var Region
*/
private $region;
/**
* @inheritDoc
*/
public function rules(): array
{
return [
[['address'], 'string', 'max' => 255],
[['address'], AddressValidator::class, 'maxPrecision' => AddressValidator::PRECISION_STREET, 'minPrecision' => AddressValidator::PRECISION_BUILDING],
];
}
/**
* @return bool
*/
private function saveRegion(): bool
{
$address = CleanHelper::address($this->address);
$this->region = new Region();
$this->region->name = $address->region;
$this->region->type = $address->regionType;
$this->region->type_full = $address->regionTypeFull;
$this->region->name_with_type = $address->regionWithType;
$this->region->fias_id = $address->regionFiasId;
$this->region->timezone = TimezoneConverter::toIana($address);
return $this->region->save();
}
/**
* @return bool
*/
private function saveCity(): bool
{
$address = CleanHelper::address($this->address);
$this->city = new City();
$this->city->name = $address->city;
$this->city->type = $address->cityType;
$this->city->type_full = $address->cityTypeFull;
$this->city->name_with_type = $address->cityWithType;
$this->city->fias_id = $address->cityFiasId;
$this->city->region_id = $this->region->id;
return $this->city->save();
}
/**
* @return bool
*/
public function save(): bool
{
if (!$this->validate()) {
return false;
}
return $this->saveRegion() && $this->saveCity();
}
}
namespace app\controllers;
use nsusoft\dadata\helpers\SuggestHelper;
use yii\web\Controller;
class SuggestController extends Controller
{
/**
* @param string $query
* @return array
*/
public function actionAddress(string $query): array
{
$items = [];
foreach (SuggestHelper::address($query) as $suggest) {
$items[] = $suggest->value;
}
return $items;
}
}