Skip to content

Commit

Permalink
Add Documenation regarding the AppSerializer Class
Browse files Browse the repository at this point in the history
References #53

Signed-off-by: Justin Yost <justin@loadsys.com>
  • Loading branch information
justinyost committed Apr 25, 2015
1 parent 9040ba0 commit ff10079
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
34 changes: 28 additions & 6 deletions Docs/DESERIALIZE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ provided via an HTTP Response Body into CakePHP Request Data arrays.
1. [Basic Controller Setup - Deserializing](#basic-controller-setup---deserializing)
1. [Advanced Setup - Deserializing](#advanced-setup---deserializing)
1. [Setup of Deserializer Class](#setup-of-deserializer-class)
1. [Custom AppSerializer Class](#custom-appserializer-class)
1. [Custom Deserialize Methods](#custom-deserialize-methods)
1. [Custom Deserializer AfterDeserialize Callback](#custom-deserializer-afterdeserialize-callback)

Expand Down Expand Up @@ -41,16 +42,37 @@ While serializing data uses the `$required` and `$optional` properties of the
`UserSerializer` class, deserializing does not. All data passed to the Deserializer
will be passed through to the CakePHP Controller.

## Custom AppSerializer Class ##

Following the pattern of AppModel, AppController, etc in CakePHP,
you can create an AppSerializer class that can extend the base Serializer class
for custom methods that are accessible to all of your Serializer classes.

To use this you can copy the file `app/Plugin/Serializers/Serializer/AppSerializer.php`
and move to `app/Serializer/AppSerializer.php`

All future Serializer Classes can then instead follow this pattern:

``` php
// Serializer/UserSerializer.php
App::uses('AppSerializer', 'Serializer');

class UserSerializer extends AppSerializer {
}
```

All future documentation in the README will follow this pattern for consistency.

## Custom Deserialize Methods ##

If you need to do any formatting or data manipulation when deserializing data,
create a method named after a field with the prefix `deserialize_`. For example:

``` php
// Serializer/UserSerializer.php
App::uses('Serializer', 'Serializers.Serializer');
App::uses('AppSerializer', 'Serializer');

class UserSerializer extends Serializer {
class UserSerializer extends AppSerializer {

/**
* On Deserializing the data, modify the first_name value by converting to
Expand Down Expand Up @@ -85,9 +107,9 @@ not include that property in the CakePHP data array, you can throw a

``` php
// Serializer/UserSerializer.php
App::uses('Serializer', 'Serializers.Serializer');
App::uses('AppSerializer', 'Serializer');

class UserSerializer extends Serializer {
class UserSerializer extends AppSerializer {

/**
* On Deserializing the data, only return the created timestamp if the id ===
Expand Down Expand Up @@ -127,9 +149,9 @@ post processing after all the data has been deserialized.

``` php
// Serializer/UserSerializer.php
App::uses('Serializer', 'Serializers.Serializer');
App::uses('AppSerializer', 'Serializer');

class UserSerializer extends Serializer {
class UserSerializer extends AppSerializer {

/**
* Callback method called after automatic deserialization. Whatever is returned
Expand Down
38 changes: 30 additions & 8 deletions Docs/SERIALIZE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ into JSON and rendering it to the front end.
1. [Basic Controller Setup - Serializing](#basic-controller-setup---serializing)
1. [Advanced Setup - Serializing](#advanced-setup---serializing)
1. [Setup of Serializer Class](#setup-of-serializer-class)
1. [Custom AppSerializer Class](#custom-appserializer-class)
1. [Required Property of Serializer Class](#required-property-of-serializer-class)
1. [Optional Property of Serializer Class](#optional-property-of-serializer-class)
1. [Custom Serialize Methods](#custom-serialize-methods)
Expand Down Expand Up @@ -77,14 +78,35 @@ class UserSerializer extends Serializer {
}
```

## Custom AppSerializer Class ##

Following the pattern of AppModel, AppController, etc in CakePHP,
you can create an AppSerializer class that can extend the base Serializer class
for custom methods that are accessible to all of your Serializer classes.

To use this you can copy the file `app/Plugin/Serializers/Serializer/AppSerializer.php`
and move to `app/Serializer/AppSerializer.php`

All future Serializer Classes can then instead follow this pattern:

``` php
// Serializer/UserSerializer.php
App::uses('AppSerializer', 'Serializer');

class UserSerializer extends AppSerializer {
}
```

All future documentation in the README will follow this pattern for consistency.

## Required Property of Serializer Class ##

You can set the fields that are required to be included in the serialized data, by
adding a `$required` property.

``` php
// Serializer/UserSerializer.php
App::uses('Serializer', 'Serializers.Serializer');
App::uses('AppSerializer', 'Serializer');

class UserSerializer extends Serializer {
public $required = array(
Expand All @@ -109,7 +131,7 @@ an array of optional properties.

``` php
// Serializer/UserSerializer.php
App::uses('Serializer', 'Serializers.Serializer');
App::uses('AppSerializer', 'Serializer');

class UserSerializer extends Serializer {

Expand All @@ -135,9 +157,9 @@ create a method named after a field with the prefix `serialize_`. For example:

``` php
// Serializer/UserSerializer.php
App::uses('Serializer', 'Serializers.Serializer');
App::uses('AppSerializer', 'Serializer');

class UserSerializer extends Serializer {
class UserSerializer extends AppSerializer {

public $required = array(
'id',
Expand Down Expand Up @@ -172,9 +194,9 @@ and the optional property will be ignored.

``` php
// Serializer/UserSerializer.php
App::uses('Serializer', 'Serializers.Serializer');
App::uses('AppSerializer', 'Serializer');

class UserSerializer extends Serializer {
class UserSerializer extends AppSerializer {

public $required = array(
'id',
Expand Down Expand Up @@ -222,9 +244,9 @@ post processing after all the data has been serialized.

``` php
// Serializer/UserSerializer.php
App::uses('Serializer', 'Serializers.Serializer');
App::uses('AppSerializer', 'Serializer');

class UserSerializer extends Serializer {
class UserSerializer extends AppSerializer {

public $required = array(
'id',
Expand Down

0 comments on commit ff10079

Please sign in to comment.