mattiabasone / fixed-width
该软件包最新版本(dev-master)没有可用的许可信息。
用于解析/生成固定宽度文件的库
dev-master
2024-06-14 15:49 UTC
Requires
- php: ^8.2
- ext-mbstring: *
- symfony/polyfill-php83: ^1.28
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.58
- php-coveralls/php-coveralls: ^2.5.3
- phpunit/phpunit: ^10.5
- vimeo/psalm: ^5.24.0
This package is auto-updated.
Last update: 2024-09-14 16:19:32 UTC
README
本软件包提供了一些用于生成/解析固定宽度(位置)文件的实用工具。该库正在开发中,请在生产中使用时自行承担风险 :D。
安装
composer require mattiabasone/fixed-width
使用方法
给定以下示例对象
<?php namespace MyNameSpace; use MattiaBasone\FixedWidth\FixedWidth; class MyObject implements FixedWidth { public function __construct( #[FixedWidthProperty(from: 0, to: 9)] public string $name, #[FixedWidthProperty(from: 10, to: 19)] public string $surname, #[FixedWidthProperty(from: 20, to: 22)] public int $age ) { } }
您可以将此对象传递给Serializer
类的serialize()
方法。
<?php use MattiaBasone\FixedWidth\Serializer; use MyNameSpace\MyObject; $object = new MyObject("John", "Smith", "39"); echo (new Serializer())->serialize($object); // Prints // "John Smith 39 "
要将字符串反序列化为实现了FixedWidth
接口并具有#[FixedWidthProperty]
属性的对象的字符串
<?php use MattiaBasone\FixedWidth\Serializer; use MyNameSpace\MyObject; $object = "John Smith 39 "; var_dump((new Serializer())->deserialize($object, MyObject::class)); // Prints: // class MyObject#12 (3) { // public string $name => // string(4) "John" // public string $surname => // string(5) "Smith" // public int $age => // int(39) // }