cs278/serialization-helpers

Helpers to work with serialized strings

v1.0.1 2015-08-07 00:55 UTC

This package is not auto-updated.

Last update: 2024-11-19 03:42:32 UTC


README

Build Status Scrutinizer Code Quality

Helpers for dealing with strings created by the serialize() function in PHP.

Usage

isSerialized($value) — Test if a supplied value is a PHP serialized string, returns true iff the syntax looks correct. This function may produce false negatives because Zend PHP’s unserialize() implementation will work on malformed strings.

isSerialized($value, &$result) — As above but also returns the unserialized value by reference.

unserialize($input) — Converts the serialized input into a PHP data type, returns the resulting data type. If an error occurs during the unserialize operation a SyntaxError will be thrown.

Examples

Test if a value is serialized:

isSerialized($value)

<?php

use Cs278\SerializationHelpers\isSerialized;

isSerialized('b:1');
// bool(false)

isSerialized('d:2.71828');
// bool(true)

Unserialize with error handling:

<?php

use Cs278\SerializationHelpers\unserialize;
use Cs278\SerializationHelpers\Exception\SyntaxError;

try {
    return unserialize('s:"foobar";');
} catch (SyntaxError $e) {
    $logger->warning('Input, `{input}` was not valid serialized data', array(
        'input' => $e->getInput(),
    ));

    return null;
}