juststeveking/laravel-postcodes

围绕postcodes.io的服务包装器

4.0.0 2024-04-20 15:14 UTC

This package is auto-updated.

Last update: 2024-09-20 16:17:12 UTC


README

LaravelPostcodes

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

通过验证规则和宏包装postcodes.io的服务

安装

通过Composer

$ composer require juststeveking/laravel-postcodes

安装后,使用以下命令合并服务的配置

$ php artisan vendor:publish --provider="JustSteveKing\LaravelPostcodes\PostcodesServiceProvider"

如果由于某种原因此方法不起作用,请按照以下步骤操作

  • 将以下内容添加到config/services.php配置文件中
<?php

'postcodes' => [
    'url' => env('POSTCODES_URL', 'https://api.postcodes.io/')
],
  • POSTCODES_URL添加到您的.env文件中,并将值设置为https://api.postcodes.io/

基本用法

您可以使用验证规则

<?php

$this->validate($request, [
    'postcode' => [
        'required',
        'string',
        new Postcode(resolve(PostcodeService::class))
    ]
]);

或者使用验证宏

$this->validate($request, [
    'postcode' => [
        'required',
        'string',
        Rule::postcode()
    ]
]);

如果您想与服务本身交互

<?php 

use JustSteveKing\LaravelPostcodes\Service\PostcodeService;

class SomeController extends Controller
{
    protected $postcodes;

    public function __construct(PostcodeService $service)
    {
        $this->postcodes = $service;
    }

    public function store(Request $request)
    {
        // validation using example above
        $location = $this->postcodes->getPostcode($request->postcode);
    }
}

或者使用外观

<?php 

class SomeController extends Controller
{
    public function store(Request $request)
    {
        // validation using example above
        $location = Postcode::getPostcode($request->postcode);
    }
}

验证

<?php

$service = resolve(PostcodeService::class);

$service->validate('AB10 1AB');

// You can also use the facade:
Postcode::validate('AB10 1AB');

验证邮编

<?php

$service = resolve(PostcodeService::class);

$service->validate('AB10 1AB');

// You can also use the facade:
Postcode::validate('AB10 1AB');

获取邮编信息

<?php

$service = resolve(PostcodeService::class);

$service->getPostcode('AB10 1AB');

// You can also use the facade:
Postcode::getPostcode('AB10 1AB');

批量查找邮编

<?php

$service = resolve(PostcodeService::class);

$service->getPostcodes([
    'AB10 1AB',
    'AB10 1AF',
    'AB10 1AG',
]);

// You can also use the facade:
Postcode::getPostcodes([
    'AB10 1AB',
    'AB10 1AF',
    'AB10 1AG',
]);

获取给定经纬度的最近邮编

<?php

$service = resolve(PostcodeService::class);

$service->nearestPostcodesForGivenLngAndLat(
    0.629806,
    51.792326
);

// You can also use the facade:
Postcode::nearestPostcodesForGivenLngAndLat(
    0.629806,
    51.792326
);

邮编的最近邮编

<?php

$service = resolve(PostcodeService::class);

$service->nearest('AB10 1AB');

// You can also use the facade:
Postcode::nearest('AB10 1AB');

自动完成邮编部分

<?php

$service = resolve(PostcodeService::class);

$service->autocomplete('AB10');

// You can also use the facade:
Postcode::autocomplete('AB10');

查询邮编

<?php

$service = resolve(PostcodeService::class);

$service->query('AB10 1AB');

// You can also use the facade:
Postcode::query('AB10 1AB');

查找已终止的邮编

<?php

$service = resolve(PostcodeService::class);

$service->getTerminatedPostcode('AB1 0AA');

// You can also use the facade:
Postcode::getTerminatedPostcode('AB1 0AA');

查找外码

<?php

$service = resolve(PostcodeService::class);

$service->getOutwardCode('N11');

// You can also use the facade:
Postcode::getOutwardCode('N11');

外码的最近外码

<?php

$service = resolve(PostcodeService::class);

$limit = 80; // Limit needs to be less than 100
$radius = 15000; // Radius needs to be less than 25000
$service->getNearestOutwardCode('N11', $limit, $radius);

// You can also use the facade:
Postcode::getNearestOutwardCode('N11', $limit, $radius);

获取给定经纬度的最近外码

<?php

$service = resolve(PostcodeService::class);

$service->nearestOutwardCodesForGivenLngAndLat(
    0.629806,
    51.792326
);

// You can also use the facade:
Postcode::nearestOutwardCodesForGivenLngAndLat(
    0.629806,
    51.792326
);

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

测试

$ composer test

贡献

请参阅贡献指南行为准则以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件juststevemcd@gmail.com而不是使用问题跟踪器。

致谢

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件