andreiavrammsd/serializer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

andreiavrammsd/serializer

最新稳定版本:v1.0.0

Composer 安装命令:

composer require andreiavrammsd/serializer

包简介

JSON string to object. Object/array to JSON.

README 文档

README

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

统计信息

  • 总下载量: 9.52k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-25