solvenfinance / kontomatikapi
Kontomatik包装器
此软件包的规范仓库似乎已不存在,因此已冻结。
Requires
- php: ^5.6 || ^7.0
- guzzlehttp/guzzle: 6.2.*
- hopsey/stddomain: ^1.0
- marc-mabe/php-enum: ^2.2
- zendframework/zend-eventmanager: ^3.0
- zendframework/zend-servicemanager: ^3.1
- zendframework/zend-validator: ^2.8
Requires (Dev)
- phpunit/phpunit: 5.6.*
README
作者:Tomasz Chmielewski
该库是非官方的PHP后端Kontomatik服务的包装器。
该库允许开发人员轻松地从远程Kontomatik服务获取数据,在获取数据时监听客户端-服务器通信,并将XML文件解析为实体。
1. 安装
通过git
git clone git@bitbucket.org:solvenfinance/kontomatikapi.git
通过composer
composer require solvenfinance/kontomatikapi
2. 工作原理
它是如何工作的?
1. KontomatikApi\Source\SourceInterface
该接口定义了从源获取数据的方式。在本具体实现中,KontomatikApi\Source\KontomatikRemote
负责提供成功状态的XML。内部首先执行命令,然后以定义的间隔(默认为1000毫秒)询问远程源命令结果。每次请求时,都会触发KontomatikApi\Source\KontomatikRemote\Event\DataFetchedEvent
。如果已经处于成功状态,则返回准备好的XML。如果远程源返回错误,则抛出KontomatikApi\Source\KontomatikRemote\KontomatikErrorException
。
2. KontomatikApi\Mapper\MapperInterface
该接口定义了解析远程成功状态的XML的方式。在本具体实现中,可以使用KontomatikApi\Mapper\Mapper
。您可以使用KontomatikApi\Mapper\StandardMapperFactory
创建此类实例。更多详细信息请参阅本说明书的第5章。
3. KontomatikApi\KontomatikService
该服务将上述段落中提到的接口全部整合在一起。更多详细信息请参阅本说明书的第3章。
3. Kontomatik服务
要从远程Kontomatik服务获取数据,您只需创建命令对象并执行它。
use KontomatikApi\KontomatikService;
use KontomatikApi\Command\ResultAggregate;
use KontomatikApi\Entity\Account;
use KontomatikApi\Entity\Owner;
use KontomatikApi\Entity\MoneyTransaction;
$service = KontomatikService::buildRemoteKontomatik("https://test.api.kontomatik.com", "superSecretApiKey");
$command = new DefaultImport(
"212321", "secretSignature", "2016-01-01"
);
try {
/** @var ResultAggregate */
$result = $service->execute($command);
foreach ($result as $row) {
if ($row instanceof Account) {
// prints "PL210009990009999090900 ..."
echo (string)$row->iban;
// tylko dla komendy DefaultImport
foreach ($row->moneyTransactions as $transaction) {
// prints "return for new years eve party ..."
echo (string)$transaction->title;
}
}
if ($row instanceof MoneyTransaction) {
// prints "return for new years eve party ..."
echo (string)$row->title;
}
if ($row instanceof Owner) {
// prints "88010109380"
echo (string)$row->pesel;
}
}
} catch (\Exception $e) {
// ... handle connection/validation exceptions
}
4. 监听客户端-服务器通信
您可以将自己的监听器附加到KontomatikRemote数据源从服务器获取XML时进行监听。监听器模式依赖于ZendFramework 3 EventManager模块。如果您熟悉此库,您应该没有问题实现自己的监听器。
use KontomatikApi\KontomatikService;
use KontomatikApi\Source\KontomatikRemote\Event\DataFetchedEvent;
$service = KontomatikService::buildRemoteKontomatik("https://test.api.kontomatik.com", "superSecretApiKey");
$eventManager = $service->getDataSource()->getEventManager();
// prints whole client-server communication
$eventManager->attach(DataFetchedEvent::DATA_FETCHED_EVENT, function (DataFetchedEvent $event) {
echo $event->getXml();
});
// prints only successful result of the command
// you can attach to this listener for logging response XML to the file, etc...
$eventManager->attach(DataFetchedEvent::DATA_FETCHED_EVENT, function (DataFetchedEvent $event) {
$elements = @simplexml_load_string($event->getXml());
$state = $body->command->attributes()['state'];
$commandStatus = CommandStatus::fromNative((string)$state);
if ($commandStatus->isSuccessful()) {
// perform XML log to file...
}
});
5. 将原始XML文件解析为实体
您可以使用独立的接口KontomatikApi\Mapper\MapperInterface
实现来解析之前缓存/存储的XML文件。
use KontomatikApi\Mapper\StandardMapperFactory;
$mapper = StandardMapperFactory::buildStandardMapper();
$result = $mapper->doMapping(file_get_contents('previouslyStoredXmlFile.xml'));
© SOLVEN Finance sp. z o. o.