Child classes:

ArraySerializer
Array serializer.
XmlSerializer
XML serializer.
CsvSerializer
CSV serializer.

Class Details

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 <?xml ...?> 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')));

	


Class Variables

protected mixed $attributes
public static mixed $DATETIME_FORMAT = 'iso8601'

The default format to serialize DateTime objects to.

protected boolean $includes_with_class_name_element = false

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.

Setting this to true will produce the following attributes array when the include option was used:

 $user = array('id' => 1, 'name' => 'Tito',
   'permissions' => array(
     'permission' => array(
       array('id' => 100, 'name' => 'admin'),
       array('id' => 101, 'name' => 'normal')
     )
   )
 );

	

Setting to false will produce this:

 $user = array('id' => 1, 'name' => 'Tito',
   'permissions' => array(
     array('id' => 100, 'name' => 'admin'),
     array('id' => 101, 'name' => 'normal')
   )
 );

	

protected mixed $model
protected mixed $options

Class Methods

public Serialization __construct ( Model $model , array &$options )

Constructs a Serialization object.

  • Model $model - The model to serialize
  • array &$options - Options for serialization
protected void options_to_a ( $key )
  • $key -
public array to_a ( )

Returns the attributes array.

public string to_s ( )

Performs the serialization.

  • abstract:
public string __toString ( )

Returns the serialized object as a string.