Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Serializer Laravel Package

botanick/serializer

Laravel/PHP serializer package for converting objects and arrays to structured formats and back. Aims to simplify data transformation with configurable normalization/denormalization for APIs, DTOs, and persistence layers.

View on GitHub
Deep Wiki
Context7

Serializer

Build Status

This library allows you to serialize PHP structures of any complexity into PHP primitives: scalars, arrays, arrays of scalars, arrays of arrays, etc. The result can be then safely stored somewhere or passed anywhere using serialize(), json_encode() or any other way to convert plain arrays into string.

This library contains built-in serializers for:

  • null
  • scalars (bool, int, float, string)
  • PHP resource type
  • arrays and \Traversable
  • \DateTime
  • objects (which are flexibly configurable via custom configurations)

Installation

This library is managed via composer. You can install it by calling:

$ composer require botanick/serializer

or manually adding it to your composer.json:

    "require": {
        "botanick/serializer": "dev-master"
    }

Usage

Serializer usage is split into two general steps.

First of all, you should construct data serializers you are going to use. For example, if you are going to serialize only arrays of DateTimes, then your code will look like:

use Botanick\Serializer\Serializer;

$arraySerializer = new Serializer\DataSerializer\ArraySerializer();
$dateTimeSerializer = new Serializer\DataSerializer\DateTimeSerializer();
// for detailed info on DateTimeSerializer options see an according section
$dateTimeSerializer->setDefaultOptions(array('format' => 'Y-m-d H:i:s'));

Then you need serializer to know about data serializers it should use:

use Botanick\Serializer\Serializer;

$serializer = new Serializer\Serializer();
$serializer->addDataSerializer($arraySerializer, -9999);
$serializer->addDataSerializer($dateTimeSerializer, -8888);

That's all! Now you are able to serialize your data:

$data = array(
    new \DateTime(),
    'yesterday' => new \DateTime('1 day ago'),
    'dates' => array(
        'some-date' => \DateTime::createFromFormat('H:i:s d.m.Y', '11:22:33 01.02.2003'),
        'another-date' => \DateTime::createFromFormat('d.m.Y', '01.02.2003')
    )
);
$serializedData = $serializer->serialize($data);
var_export($serializedData);

// you will see something like this:
// array (
//   0 => '2015-12-08 16:42:54',
//   'yesterday' => '2015-12-07 16:42:54',
//   'dates' => 
//   array (
//     'some-date' => '2003-02-01 11:22:33',
//     'another-date' => '2003-02-01 16:42:54',
//   ),
// )

Documentation

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor