zol/fixed-column-width-parser-bundle

此包已被放弃,不再维护。未建议替代包。

为 fixed-column-width-parser 提供Symfony集成

0.1.0 2014-07-09 09:25 UTC

This package is auto-updated.

Last update: 2022-02-01 12:36:52 UTC


README

fixed-column-width-parser 库 提供Symfony集成。

安装

通过 Composer

$ composer install zol/fixed-column-width-parser-bundle

然后,在您的AppKernel中启用该包

public function registerBundles()
{
    $bundles = array(
        // [...]
        new Zol\Bundle\FixedColumnWidthParserBundle\ZolFixedColumnWidthParserBundle()
    );

    return $bundles;
}

配置

包配置是可选的。每个定义的架构都将被读取和验证以创建一个具有预配置架构的解析器服务。

zol_fixed_column_width_parser:
    # An array of schemas
    schemas:
        # If it's a directory, bundle will load all YAML files found in this directory
        -  %kernel.root_dir%/../src/Zol/MyBundle/Resources/config/schemas/
        -  %kernel.root_dir%/../src/Zol/OtherBundle/Resources/config/schemas/Item.yml

架构参考

目前,此包仅支持YAML架构格式。此架构参考可能已过时,请参阅库架构参考以获取更新信息。

# The following key will be used to define the parser service name
# Here, the service name will be : zol.parser.fixed_column_width.item
item:
    # Ignored lines, null if none
    # First line is indexed by 1
    # Optionnal, null by default
    ignore: [1, 8 , 9]

    # Header line, null if missing
    # Optionnal, null by default
    header:
        field-name: length
        field-name: length

    # Define entry schema
    # Required
    entry:
        field-name: length
        field-name: length

    # Use header values as entry field names
    # If true, entry field names will be replaced with header values
    # Optionnal, false by default
    header-as-field-name: false

    # Ignore empty line
    # Optionnal, true by default
    ignore-empty-lines: true

    # Multiple files in one
    # If true, you must define separator
    # Optionnal, default false,
    multiple: false

    # Separator, only used if multiple is true
    # Define files separator
    separator:
        field: length # Separator field
        values: [ 'value', 'value'] # Field values considered as separator
        ignore: true # Ignore separation line

使用

架构验证

此包提供架构验证服务:zol.schema_validator.fixed_column_width

$validator = $container->get('zol.schema_validator.fixed_column_width');

// Throw \Zol\Parser\FixedColumnWidth\SchemaValidationException
$validator->validateSchema([], true);

// Return false
$validator->validateSchema([], false);

// Return true
$validator->validateSchema(['entry' => [1]], true);

解析器

此包提供通用解析器服务:zol.parser.fixed_column_width。但您也可以使用预先配置的解析器服务,该服务的名称将根据您的YAML架构定义命名,例如:zol.parser.fixed_column_width.item

// Generic parser
$genericParser = $container->get('zol.parser.fixed_column_width');
$genericParser->parse('file.dat', ['entry' => [1]]); // return array file content

// Configuration defined parser
$itemParser = $container->get('zol.parser.fixed_column_width.item');
$itemParser->parse('item.dat'); // return array file content

测试

$ ./vendor/bin/atoum