spiriitlabs / poliris-bundle
CSV 的 Poliris 包
dev-main
2023-05-17 07:12 UTC
Requires
- php: >=7.2
- symfony/framework-bundle: ^5.4|^6.0
- symfony/serializer: ^5.4|^6.0
- symfony/yaml: ^5.4|^6.0
Requires (Dev)
- symfony/phpunit-bridge: ^5.4|^6.0
This package is auto-updated.
Last update: 2024-08-30 01:29:08 UTC
README
此包提供 Poliris 生成 CSV。
当前版本是 V4.1.17。
安装
将 spiriitlabs/poliris-bundle 添加到您的 composer.json
文件中
$ php composer.phar require "spiriitlabs/poliris-bundle"
如何使用
CSV Poliris 非常长。超过 333 列。
这里我们建议使用模式构建器来创建它。
一些类
Column
类通过将索引与值关联来表示 CSV 文件中的位置
$column1 = new Column(1, 'Agence id'); // a string $column2 = new Column(2, true); // OUI / NON $column2 = new Column(2, new \DateTimeImmutable('2023-05-01')); // format d/m/Y $column2 = new Column(2, object); // return toString on object $column2 = new Column(2, 1); // 1
如果使用字符串,我们将删除空格和 EOL。
示例
beautiful house with garage
转换为
beautiful house with garage
之后,我们有很多类,例如 "Exterieur"
如果我们看构造函数
public function __construct( $ascenseur, $calme, $piscine, $vueDegagee, $entree, $visAVis, $monteCharge ) { $this->ascenseur = new Column(41, $ascenseur); $this->calme = new Column(63, $calme); $this->piscine = new Column(65, $piscine); $this->vueDegagee = new Column(78, $vueDegagee); $this->entree = new Column(189, $entree); $this->visAVis = new Column(192, $visAVis); $this->monteCharge = new Column(197, $monteCharge); }
在这个例子中,CSV 文件包含有关物业外部特征的详细数据,这些特征在连续的列中不是顺序排列的。为了简化数据处理,我们决定使用 Column
类来合并它们。
每个属性都由一个 Column
实例表示,其中索引指定 CSV 文件中的列位置,而值包含关联的数据。这种方法允许更容易地访问物业的外部特征,即使它们在 CSV 文件中分散。
共有 31 个类。 models
使用
$builder = new AnnonceExportBuilder(); foreach ($lines as $line) { try { $this->createLine($builder, $line); } catch (\Throwable $throwable) { // log } } return $builder->build();
function createLine() { $builder ->startLine() ->withIdentifiant( 'somewhere', 'agence_id', Annonce::ANNONCE_TYPE_VENTE, 'REF_BA123' ) ->withType( getType(), getSubType() )
重要 - 无验证
对数据进行验证。为什么?CSV 文件可以有数千行,乘以大约 300 列,而 PHP 很难跟上。
历史上,曾使用 Validator 组件的验证类,但它们太占用内存了。
一个解决方案是对 CSV 进行批量处理,并一次验证几行。然而,目前这不是议程上的事项。