Stud.IP  5.4
ObjectBuilder Class Reference

Static Public Member Functions

static build ($data, $expected_class=null)
 
static buildMany ($array, $expected_class=null)
 
static isSerializedObject ($variable)
 
static export ($object)
 
static exportAsJson ($object)
 

Data Fields

const OBJECT_IDENTIFIER = '__SERIALIZED_CLASS__'
 

Detailed Description

Object builder class

This class can be used to serialize/unserialize objects without using PHP's native functions. This should be used in contexts where the serialized data is exposed to the user.

PHP's native implementation has proven to be rather insecure in the past and since json_encode() cannot handle objects, we need a less complex solution that has less magic going on in the background.

Like unserialize, the magic methods __sleep() and __wakeup() are supported. Accordingly, objects are created without invoking the constructor.

Author
Jan-Hendrik Willms GPL2 or any later version

Member Function Documentation

◆ build()

static build (   $data,
  $expected_class = null 
)
static

Restores an object that has previously been converted to an array. Like unserialize, an expected class may be passed and the method will throw an exception when the created object is not of that or a derived type.

Object data may either be passed as an array or as a json encoded string which will be decoded prior to object creation.

Parameters
mixed$dataAssociative array or json encoded string
mixed$expected_classExpected class name of objects (optional)
Returns
mixed Object of almost arbitrary type
Exceptions
InvalidArgumentExceptionwhen either the data contains no object
UnexpectedValueExceptionwhen the object is not of the expected type

◆ buildMany()

static buildMany (   $array,
  $expected_class = null 
)
static

Restores a collection of objects that have previously been converted to a arrays. This essentially iterates over the passed array and invokes build() on each item.

Parameters
array$arrayAssociative array or json encoded string
mixed$expected_classExpected class name of objects (optional)
Returns
array as collection of objects
Exceptions
InvalidArgumentExceptionwhen either the data contains no objects or an object is not of the expected type
See also
ObjectBuilder::build

◆ export()

static export (   $object)
static

Returns all properties (public, protected and private) from given object as associative array as well as the information about the class itself. Be aware that values will only be returned if they differ from the default value. This should ensure a small footprint.

Parameters
mixed$objectArbitrary object
Returns
array containing the serialized object
Exceptions
InvalidArgumentExceptionwhen given object is actually not an object

◆ exportAsJson()

static exportAsJson (   $object)
static

Returns the exported object as a JSON encoded string. This is just a convenience method that saves you from wrapping the call to export() in json_encode() every single time.

Parameters
mixed$objectArbitrary object
Returns
string containing the serialized object as a JSON string
Exceptions
InvalidArgumentExceptionwhen given object is actually not an object

◆ isSerializedObject()

static isSerializedObject (   $variable)
static

Checks whether the passed variable contains an object. A variable must be an array and must contain the magic string constant as an array index to be considered an object.

Parameters
mixed$variableVariable to check

Field Documentation

◆ OBJECT_IDENTIFIER

const OBJECT_IDENTIFIER = '__SERIALIZED_CLASS__'

The documentation for this class was generated from the following file: