koesie10 / lua-serializer
A Lua serializer/deserializer
Installs: 1 371
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 2
Open Issues: 1
Requires
- php: >=5.4
Requires (Dev)
- php: >=5.5
- doctrine/common: ~2.6
- jms/serializer: ~1.1
- phpunit/phpunit: ~4.4
- symfony/form: ~2.8
- symfony/validator: ~2.8
Suggests
- jms/serializer: Used to serialize/deserialize in e.g. Symfony2.
This package is auto-updated.
Last update: 2024-11-19 01:41:02 UTC
README
This is a very simple Lua serializer/deserializer for PHP, with support for JMS Serializer.
Installation
Install with Composer:
composer require koesie10/lua-serializer
Usage
There is a facade for both serialization and deserialization:
$result = \Vlaswinkel\Lua\Lua::serialize([ 'foo' => 'bar']); /** * Returns: * { * foo = "bar", * } */ $result = \Vlaswinkel\Lua\Lua::deserialize('{ foo = "bar" }') /** * Returns: * [ 'foo' => 'bar' ] */
You can also use all the classes yourself:
$result = \Vlaswinkel\Lua\Serializer::encode([ 'foo' => 'bar' ]); // returns the same as above $parser = new \Vlaswinkel\Lua\Parser(new \Vlaswinkel\Lua\TokenStream(new \Vlaswinkel\Lua\InputStream('{ foo = "bar" }'))); $result = \Vlaswinkel\Lua\LuaToPhpConverter::convertToPhpValue($parser->parse()); // returns the same as above
Integration with JMS Serializer in Symfony2
This serializer can easily be included into Symfony2 with JMSSerializer, by adding the following lines to your services.yml
:
services: app.lua.serialization_visitor: class: Vlaswinkel\Lua\JMS\LuaSerializationVisitor arguments: ["@jms_serializer.naming_strategy"] tags: - { name: jms_serializer.serialization_visitor, format: lua } app.lua.deserialization_visitor: class: Vlaswinkel\Lua\JMS\LuaDeserializationVisitor arguments: ["@jms_serializer.naming_strategy"] tags: - { name: jms_serializer.deserialization_visitor, format: lua } app.lua.array_handler: class: Vlaswinkel\Lua\JMS\LuaArrayCollectionHandler tags: - { name: jms_serializer.subscribing_handler } app.lua.date_handler: class: Vlaswinkel\Lua\JMS\LuaDateHandler tags: - { name: jms_serializer.subscribing_handler } app.lua_constraint_violation_handler: class: Vlaswinkel\Lua\JMS\LuaConstraintViolationHandler tags: - { name: jms_serializer.subscribing_handler } app.lua.form_handler: class: Vlaswinkel\Lua\JMS\LuaFormHandler arguments: ["@translator"] tags: - { name: jms_serializer.subscribing_handler } app.lua_php_collection_handler: class: Vlaswinkel\Lua\JMS\LuaPhpCollectionHandler tags: - { name: jms_serializer.subscribing_handler }
If you want Symfony to recognize Lua as a request/response format, also add the following to your config.yml
:
framework:
request:
formats:
lua: ['application/x-lua', 'application/lua']
In this case I've added application/x-lua
and application/lua
as MIME-types, but as there's no standard for Lua
content types, these can be whatever you like.
You can now use the format lua
for serialization:
public function indexAction() { return $this->get('serializer')->serialize([ 'foo' => 'bar' ], 'lua'); }
Running tests
You can run automated unit tests using PHPUnit after installing dependencies:
vendor/bin/phpunit
License
This library is licensed under the MIT license. See the LICENSE file for details.