satooshi/ltsv-encoder

基于 Symfony Serializer 组件的 LTSV 编码器

0.2.0 2013-03-01 14:53 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:29:37 UTC


README

Build Status

基于 Symfony Serializer 组件 的 PHP LTSV 编码器实现。

标签分隔值

安装

要使用 Composer 安装 ltsv-encoder,只需将以下内容添加到您的 composer.json 文件中

// composer.json
{
    // ...
    require: {
        // ...
        "satooshi/ltsv-encoder": "dev-master"
    }
}

然后,您可以通过从您的 composer.json 文件所在目录运行 Composer 的 update 命令来安装新依赖项

# install
$ php composer.phar install
# update
$ php composer.phar update satooshi/ltsv-encoder

# or you can simply execute composer command if you set composer command to
# your PATH environment variable
$ composer install
$ composer update satooshi/ltsv-encoder

此组件的 Packagist 页面为 https://packagist.org.cn/packages/satooshi/ltsv-encoder

自动加载器已安装 ./vendor/autoloader.php。如果您的 PHP 脚本中使用 LTSV 编码器,只需添加

require_once 'vendor/autoload.php';

如果使用 Symfony2,自动加载器必须自动检测。

或者,您可以使用 git clone 命令

# HTTP
$ git clone https://github.com/satooshi/ltsv-encoder.git
# SSH
$ git clone git@github.com:satooshi/ltsv-encoder.git

用法

decode($data, $format, array $context = array())

<?php

use Contrib\Component\Serializer\Factory;

// deserialize
$str = "label1:value1\tlabel2:value2";
$serializer = Factory::createSerializer();
$data = $serializer->decode($str, 'ltsv');

结果为

// $data
[
  'label1' => "value1",
  'label2' => "value2",
]

encode($data, $format, array $context = array())

<?php

use Contrib\Component\Serializer\Factory;

// encode
$serializer = Factory::createSerializer();
$str = $serializer->encode($data, 'ltsv');

结果为

// $str
"label1:value1\tlabel2:value2"

serialize($data, $format, array $context = array())

<?php

use Contrib\Component\Serializer\Factory;

// encode
$data = new SerializableEntity(array('id' => 1, 'name' => 'hoge'));
$serializer = Factory::createSerializer();
$str = $serializer->serialize($data, 'ltsv');

结果为

// $str
"id:1\tname:hoge"

deserialize($data, $type, $format, array $context = array())

<?php

use Contrib\Component\Serializer\Factory;

// deserialize
$str = "id:1\tname:hoge";
$serializer = Factory::createSerializer();
$data = $serializer->deserialize($str, 'SerializableEntity', 'ltsv');

结果为

// $data
class SerializableEntity {
  protected $id =>
  int(1)
  protected $name =>
  string(4) "hoge"
}

选项

您可以将序列化器上下文选项传递给每个方法的最后一个参数。此上下文是在 Symfony 2.2 序列化器组件中引入的。

<?php

use Contrib\Component\Serializer\Factory;

$format = 'ltsv';

// you can change these default options
$context =
[
    'to_encoding' =>'UTF-8',
    'from_encodeing' => 'auto',
    'strict' => false,
    'store_context' => false,
];

$serializer = Factory::createSerializer();
$serializer->decode($data, $format, $context);
$serializer->encode($data, $format, $context);
$serializer->serialize($data, $format, $context);
$serializer->deserialize($data, $type, $format, $context);

// change options
$context =
[
    'strict' => true,
];

// recreate serializer object
$serializer = Factory::createSerializer();
$serializer->decode($data, $format, $context);
$serializer->encode($data, $format, $context);
$serializer->serialize($data, $format, $context);
$serializer->deserialize($data, $type, $format, $context);