awaitcz / smartform
Nette 的 SmartForm HTTP 客户端
v1.0.1
2024-01-19 22:47 UTC
Requires
- php: >= 8.1
- ext-json: *
- guzzlehttp/guzzle: ^7.7
- guzzlehttp/psr7: ^2.4.4
- nette/di: ^3.1
- nette/utils: >=3
- orisai/coding-standard: ^3.8
Requires (Dev)
- mockery/mockery: ^1.6
- nette/tester: ^2.5
- phpstan/phpstan: ^1.0.0
- phpstan/phpstan-deprecation-rules: ^1.0.0
- phpstan/phpstan-nette: ^1.2
- phpstan/phpstan-strict-rules: ^1.0.0
- slevomat/coding-standard: ^8.13
- squizlabs/php_codesniffer: ^3.7
README
SmartForm.cz API 集成,受 Contributte/GoSms 启发,为 Nette 框架提供
内容
需求
在 smartform.cz 上创建账户,并从 https://admin.smartform.cz/wsUser/wsListYear 复制 clientId 和密码。
安装
extensions: smartForm: Awaitcz\SmartForm\DI\SmartFormExtension smartForm: clientId: R3sZyjLPLP token: qLa1vcNn testMode: false # Optional, default false
使用
现在我们为您准备了三个客户端:AddressClient
、CompanyClient
和 EmailClient
。
所有方法在响应状态不是 200 时都会抛出带有错误信息和代码的 ClientException 异常;
AddressClient
whisper()
- Whisper addressvalidate()
- Validate address
CompanyClient
whisper()
- Whisper companyvalidate()
- Validate company
EmailClient
validate()
- Validate email
<?php namespace App; use Awaitcz\SmartForm\Client\AddressClient; use Awaitcz\SmartForm\Client\CompanyClient; use Awaitcz\SmartForm\Client\EmailClient; use Awaitcz\SmartForm\Entity\Address\Validate\ValidateAddress; use Awaitcz\SmartForm\Entity\Address\Whisper\Query\WhisperAddressByMunicipalityAndDistrict; use Awaitcz\SmartForm\Entity\Address\Whisper\Query\WhisperAddressByStreetAndNumberMunicipalityPostCode; use Awaitcz\SmartForm\Entity\Address\Whisper\Query\WhisperAddressByStreetNumberMunicipalityPostCode; use Awaitcz\SmartForm\Entity\Address\Whisper\Query\WhisperAddressByWholeAddress; use Awaitcz\SmartForm\Entity\Address\Whisper\WhisperAddress; use Awaitcz\SmartForm\Entity\Company\Validate\ValidateCompany; use Awaitcz\SmartForm\Entity\Company\Whisper\Query\WhisperCompanyByNameQuery; use Awaitcz\SmartForm\Entity\Company\Whisper\WhisperCompany; use Awaitcz\SmartForm\Entity\Country; use Awaitcz\SmartForm\Entity\Email\Validate\ValidateEmail; use Awaitcz\SmartForm\Exception\ClientException; final class ExampleService { public function __construct( private readonly AddressClient $addressClient, private readonly CompanyClient $companyClient, private readonly EmailClient $emailClient, ) { } public function tryWhisperAddress(): void { try { $whisperedAddressResponse = $this->addressClient->whisper(new WhisperAddress( new WhisperAddressByWholeAddress('Na Pavím vrchu 1949/2'), country: Country::CzechRepublic, )); $addresses = $whisperedAddressResponse->getResult(); foreach($addresses as $address) { dump([ $address->wholeAddress, // Na Pavím vrchu 1949/2, 15000 Praha 5 - Smíchov ]); break; } } catch (ClientException $e) { dump($e->getCode()); dump($e->getMessage()); } } public function tryValidateCompany(): void { try { $validatedCompanyResponse = $this->companyClient->validate( new ValidateCompany(registrationNumber: '75848015') ); $companies = $whisperedAddressResponse->getResult(); foreach($companies as $company) { dump([ $company->registrationNumber, // 75848015 $company->naceCodes, // 27900, 620 ]); break; } } catch (ClientException $e) { dump($e->getCode()); dump($e->getMessage()); } } }