2017 © Pedro PelĂĄez
 

library ltsv-encoder

LTSV encoder based on Symfony Serializer component

image

satooshi/ltsv-encoder

LTSV encoder based on Symfony Serializer component

  • Friday, March 1, 2013
  • by satooshi
  • Repository
  • 1 Watchers
  • 3 Stars
  • 172 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 1 % Grown

The README.md

LTSV encoder

Build Status, (*1)

LTSV encoder implementation in PHP based on Symfony Serializer component., (*2)

Labeled Tab-separated Values, (*3)

Installation

To install ltsv-encoder with Composer just add the following to your composer.json file:, (*4)

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

Then, you can install the new dependencies by running Composer’s update command from the directory where your composer.json file is located:, (*5)

# 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 page for this component is https://packagist.org/packages/satooshi/ltsv-encoder, (*6)

autoloader is installed ./vendor/autoloader.php. If you use LTSV encoder in your php script, just add:, (*7)

require_once 'vendor/autoload.php';

If you use Symfony2, autoloader has to be detected automatically., (*8)

Or you can use git clone command:, (*9)

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

Usage

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');

result in:, (*10)

// $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');

result in:, (*11)

// $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');

result in:, (*12)

// $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');

result in:, (*13)

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

options

You can pass the serializer context options to the last argument in each method. This context was introduced in Symfony 2.2 Serializer component., (*14)

<?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);

The Versions