jd/csv-objects

库,用于根据指定定义将CSV或XLSX文件导入到对象数组中

安装: 708

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 3

开放问题: 2

类型:项目

dev-master 2024-01-01 09:28 UTC

This package is not auto-updated.

Last update: 2024-09-23 13:03:16 UTC


README

Build status

CSV到对象

库,用于根据指定定义将CSV或XLSX文件导入到对象数组中

说明

要将文件转换为对象数组,您必须提供一个 ImportDefinition。此对象通过传递特定格式的数组创建。我们建议使用YAML来定义此文件,因为它易于阅读。此格式可能相当复杂,因此提供了两个示例

基本导入定义

name: Fruits definition                                  # (Optional) Name of the import definition
columns:                                                 # (Required) An associative array with the headings of the columns in the file that will be imported
    Name:   { fruit: '#Name#' }                          # 'fruit' is a shortname for the object being created for the list (defined below) and #Name# will be the argument passed to the constructor. The hashes indicate that it should replace it with the value on that column                                         
    Weight: ~                                            # Null indicates that it can be ignored
classes:                                                 # (Required) An associative array with the mapped classes 
    fruit: 'CSVObjects\ImportBundle\Tests\Objects\Fruit' # The first one will be the class that will be returned 

完整导入定义

所有额外属性都是可选的。

name: Fruits definition
columns:
    File Version:  { expect: 38 }                                                                                             # Expects indicates that the row value must always be as stated
    Name:          { fruit: ['#Name#', '#Colour#', '#Origin#', '#Origin - City#', '#Class#', '#Expiry date#', '#Contract#'] } # The constructor now takes an array of arguments
    Weight:        ~
    Colour:        { validate: ['red', 'yellow'] }                                                                            # Valid values that this column could have
    Origin:        { map: { 'Granada': 'Spain', 'Malaga': 'Spain', 'Dover': 'UK' } }                                          # It will apply this alias to the data on this column. If no validate is provided, non-matching values will convert into null
    Origin - City: ~
    Class:         { extract: 'Class (.*)', validate: ['A+', 'A', 'B', 'C'] }                                                 # It is like mapping but using the expecified regular expression to do the conversion. The desired part must be in brackets. Functions can optionally be combined when required
    Expiry date:   { validate: 'date', sourceFormat: 'd/m/Y', format: 'Y-m-d' }                                               # If validate is not an array, it can be an special value. 'date' will check that it is a date. Optionally, you can specify the source format using 'sourceFormat' or reformat it by specifying 'format'
    Contract:      { contract: ['#Contract#'] }
copy:                                                                                                                         # If specified, if will add more columns to the row, copying from the referenced columns before being processed
    Origin - City: Origin
classes:
    fruit:    ['CSVObjects\ImportBundle\Tests\Objects\Fruit', 'getFruitFromFullInfo']                                         # If the mapped class is an array, it will make a static call to the specified method to create the class
    contract: ['CSVObjects\ImportBundle\Tests\Objects\Contract', 'getContractFromId' ]

对于对象映射,您还可以使用 ['@service_name', 'method'] 表示法中的服务。

更复杂的示例,请参阅 school-results.ymlschool-results.csv