wc-develop/php-ofx-reader

基于模板的 OFX 读取器

1.0.3 2024-05-15 12:02 UTC

This package is auto-updated.

Last update: 2024-09-16 16:51:07 UTC


README

PHP Ofx Reader 是一个简单的库,用于读取从金融机构提取的 OFX 文件中的交易。

该库最大的不同之处在于它使用模板来读取文件,因此您可以读取来自任何金融机构的 OFX 文件。

安装

此库依赖于 php 的 mbstring 扩展。

安装很简单,只需使用 Composer

composer require wc-develop/php-ofx-reader

使用

通用模板

该库包含一个通用模板,可以满足市场上大部分 OFX 文件的读取需求。要使用它,请按照以下步骤操作:

<?php

require __DIR__ . '/vendor/autoload.php';

use WcDeveloper\PhpOfxReader\OfxReader;

$path = '/var/www/html/arquivo.ofx';
$ofxReader = new OfxReader($path);
$transactions = $ofxReader->getTransactions();

自定义模板

创建自定义模板非常简单,只需按照以下步骤操作:

1. 创建模板类:(模板可以在项目的任何部分创建

<?php

namespace Seu\NameSpace;

use WcDeveloper\PhpOfxReader\TemplateInterface;

class CustomTemplate implements TemplateInterface
{
    // Informe a tag root do arquivo, na maioria dos casos será ofx
    public function rootTag() : string
    {
        return 'ofx';
    }

    // informe o caminho completo dentro do seu arquivo para chegar até as transações
    // separe com ->
    public function mapTransactions() : string
    {
        return 'BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKTRANLIST->STMTTRN';
    }

    /**
     * Mapeie as transações do seu arquivo OFX
     * Tipo de dados disponíveis:
     *  - money
     *  - date
     *  - string
     * 
     * Estrutura de retorno da array:
     * [
     *  'qualquer-key' => ['key' => 'tag-dentro-do-arquivo-ofx', 'type' => 'tipo-do-dado'],
     *  'key-qualquer' => ['key' => 'tag-dentro-do-arquivo-ofx', 'type' => 'tipo-do-dado'],
     * ]
     *
     * @return array
     */
    public function mapTransaction() : array
    {
        return [
            'valor' => ['key' => 'TRNAMT', 'type' => 'money'],
            'identificacao'=> ['key' => 'CHECKNUM', 'type' => 'string'],
            'historico' => ['key' => 'MEMO', 'type' => 'string'],
            'data_extrato' => ['key' => 'DTPOSTED', 'type' => 'date'],
        ];
    }
}

2. 使用您的自定义模板

<?php

require __DIR__ . '/vendor/autoload.php';

use WcDeveloper\PhpOfxReader\OfxReader;
use Seu\NameSpace\CustomTemplate;

$url = 'https://seu-dominio.com/arquivo.ofx';
$ofxReader = new OfxReader($url, CustomTemplate::class);
$transactions = $ofxReader->getTransactions();

最初,这个库仅读取交易,不久将添加其他信息。