fabiomez/data-extractor 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fabiomez/data-extractor

Composer 安装命令:

composer require fabiomez/data-extractor

包简介

Library for data extraction from common resources like string or a CSV row from files

README 文档

README

Why

Everytime that I had to parse a file like a CNAB, EDI or any CSV file, the process became very similar and always returning an array that I could not ever trust on what kind of index they will bring back. Some times breaking the application because some data did not come. So, I decided to write this library that works with objects like models for extracted data. The way the data will be extracted is wrote on models attributes docblock, and this will grant to developers an auto-complete on models attributes with a proper description on how the extraction was made, and optionaly, a descrition of the meaning of this data. What I really missed on arrays.

Instalation

Execute composer require fabiomez/data-extractor

Or add fabiomez/data-extractor: "*" to required section of your composer.json file.

Usage

The Data Extractor works above models attributes. A model can be any class with public attributes that have the @stractable tag on its docblock.

At version 1, Data extractor brings 3 types of data getters, being: substring, array and regex.

Each type of value getter require its own docblok tags that must be a subtag from @extractable tag.

Substring

The substring value getter works just like substr PHP function where,

  • {@start} is the initial position, based on 0 index.
  • {@length} is the length of the desired text
/**
 * @extractable
 *   {@start integer}
 *   {@length integer}
 */

Array

The index is a simple array index tha must be extracted (say, from an CSV file). Both numeric or associative index.

/**
 * @extractable
 *   {@index mixed}
 */

Regex

Regex uses patterns to match the desired data, where:

  • {@pattern} must be any valid pattern tha must match the desired data
  • {@index} is a numeric index of the matched data from pattern
/**
 * @extractable
 *   {@pattern string}
 *   {@index integer}
 */

The Model

Write a class with public attributes with docblock description to guide the Extractor

class MyModel
{
    /**
     * @extractable
     *    {@start 0}
     *    {@length 10}
     * @otherTag from prop 1
     */
    public $prop1;

    /**
     * @extractable
     *    {@start 10}
     *    {@length 11}
     * @otherTag from prop 2
     */
    public $prop2;
}

Getting the extractor

The extractor can be directly instantiated or created via factory

Directly

use Fabiomez\DataExtractor\Extractor;
use Fabiomez\DataExtractor\DocBlockParser;
use Fabiomez\DataExtractor\ValueGetters\ArrayValueGetter;
use Fabiomez\DataExtractor\ValueGetters\RegexValueGetter;
use Fabiomez\DataExtractor\ValueGetters\SubstringValueGetter;

//Array extractor
$extractor = new Extractor(
    DocBlockParser::createInstance(),
    new ArrayValueGetter()
);

//Regex extractor
$extractor = new Extractor(
    DocBlockParser::createInstance(),
    new RegexValueGetter()
);

//Substring extractor
$extractor = new Extractor(
    DocBlockParser::createInstance(),
    new SubstringValueGetter()
);

Via Factory

use Fabiomez\DataExtractor\ExtractorFactory;

$factory = new ExtractorFactory();

//Array extractor
$extractor = $factory->createArrayExtractor();

//Regex extractor
$extractor = $factory->createRegexExtractor();

//Substring extractor
$extractor = $factory->createSubstringExtractor();

Extracting the data from source

The extraction process can use the model namespace or an instance;

//By namespace
$extractedModel = $extractor->extract(MyModel::class, 'First dataSecond Data');

//By instance
$extractedModel = $extractor->extract(new MyModel(), 'First dataSecond Data');

echo $extractedModel->prop1; //will give 'First data'
echo $extractedModel->prop2; //will give 'Second data'

Optionaly a callback can be provided on third paramenter to touch the model after the extraction

$extractedModel = $extractor->extract(
    MyModel::class,
    'First dataSecond Data',
    function ($model, $propertiesSchema) {
        foreach ($propertiesSchema as $property => $schema) {
            $model->{$property} .= $schema['otherTag'];
        }
    }
);

echo $extractedModel->prop1; //will give 'First data from prop 1'
echo $extractedModel->prop2; //will give 'Second data from prop 2'

fabiomez/data-extractor 适用场景与选型建议

fabiomez/data-extractor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 03 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「phpdoc」 「data extraction」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 fabiomez/data-extractor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fabiomez/data-extractor 我们能提供哪些服务?
定制开发 / 二次开发

基于 fabiomez/data-extractor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-25