class Serialization (View source)

Base class for Model serializers.

All serializers support the following options:

  • only: a string or array of attributes to be included.
  • except: a string or array of attributes to be excluded.
  • methods: a string or array of methods to invoke. The method's name will be used as a key for the final attributes array along with the method's returned value
  • include: a string or array of associated models to include in the final serialized product.
  • only_method: a method that's called and only the resulting array is serialized
  • skip_instruct: set to true to skip the declaration.

Example usage:

include the attributes id and name

run $model->encoded_description() and include its return value

include the comments association

include posts association with its own options (nested)

$model->to_json(array( 'only' => array('id','name', 'encoded_description'), 'methods' => array('encoded_description'), 'include' => array('comments', 'posts' => array('only' => 'id')) ));

except the password field from being included

$model->to_xml(array('except' => 'password')));

Properties

protected $model
protected $options
protected $attributes
static $DATETIME_FORMAT The default format to serialize DateTime objects to.
protected boolean $includes_with_class_name_element Set this to true if the serializer needs to create a nested array keyed on the name of the included classes such as for xml serialization.

Methods

__construct(Model $model, array $options)

Constructs a {@link Serialization} object.

options_to_a($key)

No description

array
to_a()

Returns the attributes array.

string
__toString()

Returns the serialized object as a string.

string
to_s()

Performs the serialization.

Details

Serialization __construct(Model $model, array $options)

Constructs a {@link Serialization} object.

Parameters

Model $model The model to serialize
array $options &$options Options for serialization

Return Value

Serialization

final protected options_to_a($key)

Parameters

$key

final array to_a()

Returns the attributes array.

Return Value

array

final string __toString()

Returns the serialized object as a string.

Return Value

string

See also

to_s

abstract string to_s()

Performs the serialization.

Return Value

string