GithubHelp home page GithubHelp logo

andreiavrammsd / serializer Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 115 KB

Very basic serializer/unserializer.

License: MIT License

Dockerfile 0.57% Makefile 1.91% PHP 97.52%
php json unserialize serialize transformer

serializer's Introduction

PHP Serializer

build

Very basic serializer/unserializer/toarray. Also transforms data by types and/or callbacks.

Currently handles only JSON.

Install

composer require andreiavrammsd/serializer

Usage

$input = '{...}';
$class = ObjectClass::class;

$serializer = Serializer\SerializerBuilder::instance()->build();
// OR
$serializer = Serializer\Factory::create();

$object = $serializer->unserialize($input, $class);

$serializer->serialize($object);

$serializer->toArray($object);

See examples and tests.

Property annotations (all annotations are optional)

  • Property: name of key in input. If not set, the variable name is used.
  • Type: If set, the value will be transformed as follows
    • int, float, string, bool, array: will cast value to the type.
    • collection: value will be wrapped by a countable iterator with array access.
    • DateTime: creates a DateTime object, formatting the value by formats given as arguments; first valid format is used.
    • Fully qualified class name: the value will be parsed into the given class.
    • Array of class: the value will be parsed into an array with each element parsed into the given class.
    • Collection of class: the value will be parsed into a collection with each element parsed into the given class.
  • Callback: A callable (function or class method) is accepted (with optional parameters). The value will be passed to the callable (with the optional parameters, if set), and the new value will be the result of the callable.

Examples

@Serializer\Property("first_name")

@Serializer\Type("int")
@Serializer\Type("float")
@Serializer\Type("string")
@Serializer\Type("bool")
@Serializer\Type("array")
@Serializer\Type("collection")
@Serializer\Type("DateTime","Y-m-d H:i", "Y-m-d")
@Serializer\Type("Entity\User")
@Serializer\Type("array[Entity\User]")
@Serializer\Type("collection[Entity\User]")

@Serializer\Callback("strtoupper")
@Serializer\Callback("substr", "0", "3")
@Serializer\Callback("[User\NameFormatter, firstName]")
@Serializer\Callback("[User\NameFormatter, lastName]", "1", "3")

@Serializer\IgnoreNull() // Ignores null values when serializing or converting to array.

Object class annotations

  • Collection: a class annotated with Collection and extending the Collection class will be a collection class with its items of the specified class type.

Examples

@Serializer\Collection("Entity\User")

Development

  • Requirements: Docker, Make
  • Build and install dev container:
    • make build PHPVERSION=7.4
    • make install PHPVERSION=7.4
  • Run QA tools: make PHPVERSION=7.4
  • Work inside dev container: make run PHPVERSION=7.4 then make localqa
  • Remove Docker image: make clean PHPVERSION=7.4

Developed with PhpStorm

JetBrains

serializer's People

Contributors

andreiavrammsd avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

serializer's Issues

type hint "object" in method signature does not seem to work with php 7.1

In class Serializer\Parser\Parser , the following methods:

parseClass(\ReflectionClass $reflectionClass, object $object, array $data)
parseProperties(\ReflectionClass $reflectionClass, object $object, array $data)

have "object" as type hints, which cause the following error with PHP 7.1.12 (my current local version):

Argument 2 passed to Serializer\Parser\Parser::parseClass() must be an instance of Serializer\Parser\object, instance of Apixu\Response\CurrentWeather given, called in [...]/vendor/andreiavrammsd/serializer/src/Parser/Parser.php on line 46

I removed the "object" type hints locally and now it's working.

Would you consider removing these, or raising the php version requirement in composer.json to a more recent version of PHP (It may be working with 7.2, but I did not test it)?

Thanks !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.