Download the PHP package stopsopa/jms-serializer-lite without Composer

On this page you can find all versions of the php package stopsopa/jms-serializer-lite. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package jms-serializer-lite

Build Status Coverage Status Latest Stable Version

DEPRECATED

Created in 2016 - quite old now and not maintained.

That was actually usefull a lot back then. But please don't use it now.

Why?

Usually first choice of library for Symfony 2/3 to dump data from db to provide any RESTful feeds is jms/serializer. This tool is designed to serialize and unserialize data to xml, json, or yml and back to initial data structures. But usually there is need to just provide data into one direction - to json feeds. Additionally usually there is need to serialize the same object in different ways. In jms/serializer and similar complex tools usually you can use "groups", unfortunately this solution is not flexible enough to deal with real life situations.

So what does this library special do?

This library gives you ability to serialize any nested data structures (usually ORM objects) to any array structure, ready to json_encode in simplest possible way, without loosing flexibility and without loosing inheritance to provide new serialization format by changing (overriding) old format. This library is also framework agnostic.

Readme

Installation

composer require stopsopa/jms-serializer-lite

Documentation

When You have ORM entities like ...

Article:
    id
    title
    content
    comments # <one-to-many with Comment entity>

Comment
    id
    article # <many-to-one with Article entity>
    user # <many-to-one with User entity>
    content

User
    id
    login
    name
    surname
    comments # <one-to-many with Comment entity>

... and there is need to serialize Article to RESTful feed:

The simplest way to start do that using this library is to create simple class (e.g.) NewDumper that extends class Stopsopa\LiteSerializer\Dumper ...

... and use it to get array ready to json_encode like ...

After executing this you will end up with error ...

Article exception screen

... that means that You need to implement method dumpMyProject_Article to "explain" new class how to transform entity to flat array ...

... now when You run this code again You will have what You need.

Nested entities

To serialize Article with all it's comments like ...

... simply change method dumpMyProject_Article to ...

... now when You try to execute it, You will see that NewDump require method dumpMyProject_Comment ...

Comment exception screen

... and that's it.

Serialize from different angles

Worth to mention is fact that class prepared above is ready to serialize classes Article and Comment for different use cases ...

Shorter syntax and helper

All logic explained above can be much more condensed using helper 'toArray':

... let's hold for a minute at this code, and try to understand what's going on here.

    So "key side" of array (the left side with 'id', 'name', 'body', 'comments') is side where we declare keys for result array - that's obvious.

    Right side (the value side of array with 'id', 'title', 'content', 'comments') is little more sophisticated. This values are passed to AbstractEntity->get() method, these are "paths" describing how to get values for these keys. See more about AbstractEntity in another page.

 

Note:

It is good idea to read chapter about AbstractEntity before continue reading this documentation.

 

Default values

You can also specify default value to prevent throwing AbstractEntityException if path is wrong (wrong i mean if leads to nowhere). But to do that on the right hand side you need to use extended version of options ...

... so from now on if in object Article field 'title' will be missing you won't see Exception but method will return (like nothing happened) value 'defaultname'.

When i say "missing" it means:

... if all above can't reach value then "path is wrong" and value is missing because there is no value under this path.

 

Note:

Empty string or null value are still valid values it doesn't mean that path is wrong.

 

If there is need to replace false value by something else it should be done like this:

Excluding/Omitting entities

Sometimes we don't want to have some particular entities in feed. To skip them just throw DumperContinueException:

Save keys

By default dumper don't maintains key association during iteration:

... return ...

but when you change ...

... keys will be maintained ...

DumperInterface

There is special interface Stopsopa\LiteSerializer\DumpToArrayInterface. Implement this interface to declare how to dump entity right in entity itself.

Default types serialization (integer, string, float, etc.)

Anything else that is not array and is not object is serialized by method dumpPrimitives. You can easily change way of serializing even such values by overriding this method.

Force mode

As you probably noticed everything what is 'foreachable' will be iterated and each element of "collection" will be serialized individually. But there is one situation when this behaviour "can" (don't must) be wrong. When entity implements interface Traversable and in json feed you need fields that are not accessible through 'foreach' on this class. In such situation would be good to have mechanizm to decide manually if serialize such class in normal mode or let dumper to iterate through. You can achieve this like:

... or You can dump these entity manually (not in 'toArray' method) like:

... naaa, too long.

Scope and stack

As we saw higher in documentation (link), it is possible to use one dumper class to dump bunch of entities from different perspective. Doing this though sometimes You might want to change way of working individual dumping methods. For example if You want to dump article it is good in feed provide also information about user that created this article, but You don't need in this use case all informations about user. But if You will dump User and his all articles it would be good to provide all information about user and less from article. To distinguish these use cases in method itself You can use two private properties available in all methods - scope and stack.

Example of using 'stack':

Example of using 'scope':


All versions of jms-serializer-lite with dependencies

PHP Build Version
Package Version
Requires php Version ^5.3 || ^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package stopsopa/jms-serializer-lite contains the following files

Loading the files please wait ....