2017 © Pedro Peláez
 

library robertserializer

My serializer

image

robertlabrie/robertserializer

My serializer

  • Sunday, October 25, 2015
  • by robertlabrie
  • Repository
  • 1 Watchers
  • 0 Stars
  • 16 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

robertserializer

Why

RobertSerializer was born out of the desire to easily store objects in MongoDB. The existing PHP serializer is fine for storing in session, but is useless for a document DB. When I asked around, the consensus seemed to be using something called an "ODM (Object Document Model)" which is essentially a property map between the Mongo document and my PHP class. I didn't feel like maintaining one of those! So, RobertSerializer is what I came up with., (*1)

How it works

I leverage PHPs' reflection class to recursively crawl the input object properties and feed the data back out as an array. Mongo likes arrays, so that's why it's an array, but you could easily called json_encode() on it and store it in RavenDB if that's your thing., (*2)

What's the catch?

I need to know your objects type when I deserialize later, so I add an extra element to the array __TYPE__ (you can change it). For this reason, it's a good idea to use namespaces. Also, probably if you change your class namespace, you won't be able to deserialize an object. If anyone uses this and that's a problem, we'll let you define a map (boo!) so that you can survive a refactoring., (*3)

Why a special field?

BSON seemed ugly, (*4)

How to use it

composer require RobertSerializer, (*5)

#do the necessary composer bits
$rs = new \RobertSerializer\RobertSerializer();
$in = new \Example\Foo();
$ser = $rs->serialize($in);
//$ser is an array that you can pass around

$out = $rs->deserialize($ser);
//$out is an instance of Foo(), exactly how you remember it!


The Versions

25/10 2015

dev-master

9999999-dev

My serializer

  Sources   Download

GPL v2

by Robert Labrie