PHP Classes
elePHPant
Icontem

Oire Serializer: Serialize data types in multiple ways

Recommend this page to a friend!
  Info   View files Documentation   View files View files (8)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-06-11 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 51 All time: 8,900 This week: 565Up
Version License PHP version Categories
oireserializer 1.0MIT/X Consortium ...5PHP 5, Data types
Description Author

This class can serialize data types in multiple ways.

It can take a variable values and returns a string that represents the variable as a text string in different formats.

Currently it supports different formats like JSON, mgspack and igbinary.

The class can also restore the original variable values by unserializing a previously serialized value.

  Performance   Level  
Name: Andre Polykanine A.K.A. ... is available for providing paid consulting. Contact Andre Polykanine A.K.A. ... .
Classes: 3 packages by
Country: Ukraine Ukraine
Age: ???
All time rank: 219134 in Ukraine Ukraine
Week rank: 522 Up8 in Ukraine Ukraine Up
Innovation award
Innovation award
Nominee: 1x

Details

Oiré Serializer ? Data Serializer for Better Storage and Exchange

Build Status MIT License

Serializes various data to different formats. There is also a possibility to additionally encode the output to URL-and filename-safe base64. Depends on Oirė Base64 for encoding binary data to a storable format.

Requirements

Requires PHP 7.1 or later with JSON support enabled. Requires additional PECL extensions for certain serialization modes, see below. If no PECL is available and none of the required extensions is installed in your environment (such a shared hosting), you can freely use Oirė Serializer with JSON.

Installation

Via Composer:

composer require oire/serializer

Or manually. Note that you will need base64.php from Oirė Base64:

require_once("oire/base64.php");
require_once("oire/serializer.php");

Running Tests

Run phpunit in the projects directory.

Usage Examples

Serialization to JSON. Note that you can either call methods consequently or chain them, like this:

use \Oire\Serializer;
$data = ["fruit" => "orange", "vegetable" => "carrot", "money" => 3000, "randomArray" => [1, 2, 3, 4, 5], "Lambė" => "??????????"];
try {
	// You can pass "j", "json" or 1 to setMode() to get JSON
	$jsonSerialized = (new Serializer())->setMode("json")->serialize($data);
} catch(Exception $e) {
	// Handle errors
}

This will output:

{"fruit":"orange","vegetable":"carrot","money":3000,"randomArray":[1,2,3,4,5],"Lambė":"??????????"}

Now unserializing:

// Without chaining it will be like this
$s = new Serializer();
// You may wrap this also with try...catch
$s->setMode("j");
try {
	$unserialized = $s->unserialize($jsonSerialized);
} catch(Exception $e) {
	// Handle errors
}

This will return the original array. If you want to get an object instead, pass false as the third parameter to unserialize(). If you pass true as the second parameter to serialize(), the serialized data will be additionally encoded to URL-and filename-safe base64. This might be particularly useful if you choose a binary format such as MessagePack:

$msgPackSerialized = (new Serializer())->setMode("mp")->serialize($data, true);

This will output:

haVmcnVpdKZvcmFuZ2WpdmVnZXRhYmxlpmNhcnJvdKVtb25lec0LuKtyYW5kb21BcnJheZUBAgMEBaZMYW1iw6u00KPQutGA0LDRl9C90YHRjNC60LA

Supported Serialization Modes

Currently the following modes are supported. Note, if the mode is binary, a raw binary string is returned if $base64 parameter is left as false during serialization which can be uncomfortable for reading or storage. * JSON, non-binary. Pass 1, "j" or "json" to setMode() to set this mode. * MessagePack, binary. Pass 2, "m", "mp", "msgpack" or "messagepack" to setMode() to set this mode. Note that the corresponding PECL extension should be installed for this to work. * Igbinary, binary. Pass 3, "i", "ib", "ig" or "igbinary" to setMode() to set this mode. Note that the corresponding PECL extension should be installed for this to work.

Methods

The methods are documented in the source file, but their description is given below. We recommend to wrap every call in try...catch since Oirė Serializer throws exceptions in case of errors.

  • Class constructor? you might provide a serialization mode when calling the constructor or directly call setMode().
  • setMode(int|string $mode) ? accepts either a numeric representation of the mode or a readable string such as "json". See the Supported Modes section above. Chainable, so returns the current class instance.
  • getMode(bool $asString = false): int|string ? gets the current serialization mode set by setMode(). If $asString is set to true, returns a readable mode name such as "json", a numeric representation is returned otherwise (it is the default behavior). Throws an exception if the mode is not set or if it could not be found.
  • getAvailableModes(bool $json = false): array|string ? gets all available serialization modes. If the $json parameter is set to true, returns them as a JSON string, an associative array is returned otherwise (this is the default behavior).
  • serialize(mixed $data, bool $base64 = false): string ? serializes given data according to the serialization mode set with setMode(). If $base64 is set to true, additionally encodes the serialized data to URL-and filename-safe base64 (particularly useful for binary serialization formats such as MessagePack). If set to false (default), the serialized data is returned as a string, be it binary or not.
  • unserialize(string $data, bool $base64 = false, bool $assoc = true): mixed ? unserializes given data according to the serialization mode set with setMode(). If $base64 is set to true, assumes that the data had been additionally encoded to URL-safe base64 after serialization. If $assoc is set to true (default), returns an associative array, an object is returned otherwise. Note that the last parameter is applicable only to JSON serialization.

License

Copyright © 2017, Andre Polykanine also known as Menelion Elensślė. This software is licensed under an MIT license.

  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetests (1 file)
Plain text file .travis.yml Data Auxiliary data
Plain text file autoload.php Aux. Auxiliary script
Plain text file composer.json Data Auxiliary data
Plain text file LICENSE Lic. License text
Plain text file phpunit.xml Data Auxiliary data
Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file serializer.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file SerializerTest.php Class Class source

Downloadoireserializer-2017-06-11.zip 9KB
Downloadoireserializer-2017-06-11.tar.gz
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
Oire Base64 Download .zip .tar.gz Needed for encoding serialized data to Url-safe base64. Conditional
 Version Control Unique User Downloads Download Rankings  
 100%
Total:51
This week:0
All time:8,900
This week:565Up