wnx / php-swiss-cantons
通过名称或缩写搜索瑞士州。
5.0.2
2024-05-27 18:27 UTC
Requires
- php: ^8.2
- ext-json: *
- ext-zip: *
- symfony/http-client: ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3
- league/csv: ^9.7
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.1
- rector/rector: ^0.15.24
- symfony/console: ^6.0
README
使用JavaScript?请查看@stefanzweifel/js-swiss-cantons。
安装
安装此包的最简单方法是使用composer。该包需要PHP 8.2。
composer require "wnx/php-swiss-cantons"
使用方法
使用CantonManager
类与此包交互。下面你可以找到一个如何在Laravel框架中使用它的例子。另外,你还可以找到CantonManager
和Canton
的所有公共API方法。
<?php use Wnx\SwissCantons\CantonManager; $cantonManager = new CantonManager(); /** @var \Wnx\SwissCantons\Canton $canton */ $canton = $cantonManager->getByAbbreviation('zh'); $canton = $cantonManager->getByName('Zurigo'); $canton = $cantonManager->getByZipcode(8000); // "Zürich" $cantonName = $canton->setLanguage('de')->getName();
CantonManager
使用CantonManager
查找一个州。它将返回一个新的Canton
实例,或者在找不到缩写、名称或邮编时抛出异常。
getByAbbreviation()
通过缩写查找州。请参阅此列表以获取可用缩写。
$cantonManager = new Wnx\SwissCantons\CantonManager(); /** @var \Wnx\SwissCantons\Canton $canton */ $canton = $cantonManager->getByAbbreviation('GR');
getByName()
通过名称搜索州。名称必须与州的翻译(德语、法语、意大利语、罗曼什语或英语)之一完全匹配。
$cantonManager = new Wnx\SwissCantons\CantonManager(); /** @var \Wnx\SwissCantons\Canton $canton */ $canton = $cantonManager->getByName('Zürich');
getByZipcode()
返回给定邮编的可能州数组。(某些邮编由多个州共享)。
$cantonManager = new Wnx\SwissCantons\CantonManager(); /** @var \Wnx\SwissCantons\Canton[] $cantons */ $cantons = $cantonManager->getByZipcode(3005);
getByZipcodeAndCity()
通过给定的邮编和可选的城市名称查找州。
$cantonManager = new Wnx\SwissCantons\CantonManager(); /** @var \Wnx\SwissCantons\Canton $canton */ $canton = $cantonManager->getByZipcodeAndCity(1003); $canton = $cantonManager->getByZipcodeAndCity(1290, 'Lausanne');
Canton
setLanguage($string = 'en')
设置应用于显示州名称的语言。目前支持以下语言。
- 德语 (
de
) - 法语 (
fr
) - 意大利语 (
it
) - 罗曼什语 (
rm
) - 英语 (
en
,默认)
该方法返回当前Canton
实例,以便更容易地进行方法链。
$canton->setLanguage('rm');
$canton->setLanguage('fr')->getName();
getName()
返回给定州的名称。如果没有先调用setLanguage()
方法,则该方法将返回英文名称。
$canton->getName(); // Grisons $canton->setLanguage('de')->getName(); // Graubünden
getAbbreviation()
返回给定州的官方缩写。
$canton->getAbbreviation(); // e.g. ZH
Cantons
此类用于内部使用,但也可以在你的项目中使用,如果你需要所有州的列表
getAll()
返回一个包含Wnx\SwissCantons\Canton
对象的数组。
use Wnx\SwissCantons\Cantons; $cantons = (new Cantons)->getAll();
getAllAsArray($defaultLanguage = 'en')
返回一个所有州的一维数组。键是缩写。值将是州的翻译名称。默认语言是英语。将有效语言传递给方法以本地化名称。
use Wnx\SwissCantons\Cantons; $cantons = (new Cantons)->getAllAsArray('en'); $cantonsAsArray = $cantons->getAllAsArray(); // var_dump($cantonsAsArray); // [ // 'ZH' => 'Zurich', // 'GE' => 'Geneva', // // ... // ]
安全
如果你在此包中发现安全漏洞,请发送电子邮件至stefan@stefanzweifel.dev。所有安全漏洞都将得到迅速解决。
数据来源
版本控制
我们使用SemVer进行版本控制。有关可用版本,请参阅此存储库上的标签。
致谢
许可协议
本项目采用MIT许可证 - 请参阅许可证文件以获取详细信息。