Download the PHP package nilportugues/serializer without Composer
On this page you can find all versions of the php package nilportugues/serializer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nilportugues/serializer
More information about nilportugues/serializer
Files in nilportugues/serializer
Package serializer
Short Description Serialize PHP variables, including objects, in any format. Support to unserialize it too.
License MIT
Homepage http://nilportugues.com
Informations about the package serializer
Serializer for PHP
- Installation
- Introduction
- Features
- Serialization
- Serializers (JSON, XML, YAML)
- Example
- Custom Serializers
- Data Transformation
- Array Transformer
- Flat Array Transformer
- XML Transformer
- YAML Transformer
- JSON Transformer
- JSend Transformer
- JSON API Transformer
- HAL+JSON Transformer
- Quality
- Contribute
- Author
- License
Installation
Use Composer to install the package:
Introduction
What is serialization?
In the context of data storage, serialization is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and reconstructed later in the same or another computer environment.
Why not serialize()
and unserialize()
?
These native functions rely on having the serialized classes loaded and available at runtime and tie your unserialization process to a PHP
platform.
If the serialized string contains a reference to a class that cannot be instantiated (e.g. class was renamed, moved namespace, removed or changed to abstract) PHP will immediately die with a fatal error.
Is this a problem? Yes it is. Serialized data is now unusable.
Features
- Serialize to JSON, XML and YAML formats.
- Serializes exact copies of the object provided:
- All object properties, public, protected and private are serialized.
- All properties from the current object, and all the inherited properties are read and serialized.
- Handles internal class serialization for objects such as SplFixedArray or classes implementing Traversable.
- Basic Data Transformers provided to convert objects to different output formats.
- Production-ready.
- Extensible: easily write your out
Serializer
format or dataTransformers
.
Serialization
For the serializer to work, all you need to do is pass in a PHP Object to the serializer and a Strategy to implement its string representation.
Serializers (JSON, XML, YAML)
- NilPortugues\Serializer\JsonSerializer
- NilPortugues\Serializer\XmlSerializer
- NilPortugues\Serializer\YamlSerializer
Example
In the following example a $post
object is serialized into JSON.
Code
The object, before it's transformed into an output format, is an array with all the necessary data to be rebuild using unserialize method.
Output
Custom Serializers
If a custom serialization strategy is preferred, the Serializer
class should be used instead. A CustomStrategy
must implement the StrategyInterface
.
Usage is as follows:
Data Transformation
Transformer classes greatly differ from a Strategy
class because these cannot unserialize()
as all class references are lost in the process of transformation.
To obtain transformations instead of the Serializer
class usage of DeepCopySerializer
is required.
The Serializer library comes with a set of defined Transformers that implement the StrategyInterface
.
Usage is as simple as before, pass a Transformer as a $strategy
.
For instance:
Following, there are some examples and its output, given the $post
object as data to be Transformed.
Array Transformer
Flat Array Transformer
XML Transformer
YAML Transformer
Json Transformer
JsonTransformer comes in 2 flavours. For object to JSON transformation the following transformer should be used:
Output
If your desired output is for API consumption, you may like to check out the JsonTransformer library, or require it using:
JSend Transformer
JSend Transformer has been built to transform data into valid JSend specification resources.
Please check out the JSend Transformer or download it using:
JSON API Transformer
JSON API Transformer has been built to transform data into valid JSON API specification resources.
Please check out the JSON API Transformer or download it using:
HAL+JSON Transformer
HAL+JSON Transformer has been built for HAL+JSON API creation. Given an object and a series of mappings a valid HAL+JSON resource representation is given as output.
Please check out the HAL+JSON API Transformer or download it using:
Quality
To run the PHPUnit tests at the command line, go to the tests directory and issue phpunit
.
This library attempts to comply with PSR-2 and PSR-4.
If you notice compliance oversights, please send a patch via pull request.
Contribute
Contributions to the package are always welcome!
- Report any bugs or issues you find on the issue tracker.
- You can grab the source code at the package's Git repository.
Authors
License
The code base is licensed under the MIT license.