承接 nsu-soft/yii-dadata 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

nsu-soft/yii-dadata

最新稳定版本:0.6

Composer 安装命令:

composer require nsu-soft/yii-dadata

包简介

DaData module for Yii 2 Framework

README 文档

README

Installation

If you don't have Composer, you may install it by following instructions at getcomposer.org.

Install this library using the following command:

composer require nsu-soft/yii-dadata

Configuration

Add the following lines to your configuration file:

'modules' => [
    'dadata' => [
        'class' => 'nsusoft\dadata\Module',
        'token' => 'enter-your-dadata-token',
        'secret' => 'enter-your-dadata-secret',
        'cachePriority' => [DbHandler::class],
    ],
],

Read more about modules configuration in Yii2.

Migrations

Apply migrations, if you want to save responses from DaData to your local database:

yii migrate --migrationPath=@vendor/nsu-soft/yii-dadata/src/migrations

Usage

Clean address:

<?php

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

Suggest address:

<?php

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

See src/helpers and src/validators directories for more usage examples.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-02-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固