harp-orm/serializer 问题修复 & 功能扩展

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

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

harp-orm/serializer

Composer 安装命令:

composer require harp-orm/serializer

包简介

Serialize object properties

README 文档

README

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version

Serialize object/array properties, using different rules for each property.

Usage

use Harp\Serializer;

$serializers = new Serializer\Serializers([
    new Serializer\Native('nativeSerializerdArray'),
    new Serializer\Csv('csvString'),
    new Serializer\Json('jsonProperty'),
]);

$obj = new stdClass();

$obj->nativeSerializerdArray = array('test' => 'param');
$obj->csvString = array('val', 'val2');
$obj->jsonProperty = array('test' => 'asd');

$serializers->serialize($obj);

// Will output:
// stdClass Object
// (
//     [nativeSerializerdArray] => a:1:{s:4:"test";s:5:"param";}
//     [csvString] => val,val2
//     [jsonProperty] => {"test":"asd"}
// )
print_r($obj);

// Will unserialize all the relevant properties
$serializers->unserialize($obj);

Object Serialization

If you have an object that implements Serializable, but does not use the native php serialization you can use Object Serializer, greatly reducing the object's string footprint.

use Serializable;

class SimpleObject implements Serializable
{
    public $prop1;
    public $prop2;

    public function serialize()
    {
        return $this->prop1.','.$this->prop2;
    }

    public function unserialize($data)
    {
        list($this->prop1, $this->prop2) = explode(',', $data);
    }
}

$serializers = new Serializer\Serializers([
    new Serializer\Object('test', 'SimpleObject'),
]);

$obj = new stdClass();
$obj->test = new SimpleObject();
$obj->test->prop1 = 10;
$obj->test->prop2 = 20;

$serializers->serialize($obj);

// Will output:
// stdClass Object
// (
//     [test] => 10,20
// )
print_r($obj);

// Will unserialize all the relevant properties
$serializers->unserialize($obj);

Harp ORM Integration

Serializer is integrated into Harp ORM and gives you the ability to store arbitrary data in your database, by serializing the model's properties.

For example holding an api response in a "response" field.

// Model
class Payment extends AbstractModel
{
    public $id;
    public $response;
}

// Repo
class Payment extends AbstractRepo
{
    public function initialize()
    {
        $this
            ->addSerializers([
                new Serializer\Json('response'),
            ]);
    }
}

$model = new Model\Payment(['response' => $someArray]);

// The response property will get serialized as a json string.
Repo\Payment::get()->save($model);

SerializersTrait

This trait gives you the ability to easily add serializers to another object.

class TestConfig {
    use SerializersTrait;
}

$config = new TestConfig();

$config
    ->serializeCsv('name');

// Return the Asserts object
$config->getSerializers();

Here are all the methods added by this trait.

Method Description
getSerializers() Get the Serializers object
addSerializer(AbstractSerializer) Add arbitrary serializer
serializeCsv($name) Add an Csv serializer
serializeJson($name) Add an Json serializer
serializeNative($name) Add an Native serializer
serializeObject($name, $class) Add an Object serializer

License

Copyright (c) 2014, Clippings Ltd. Developed by Ivan Kerin

Under BSD-3-Clause license, read LICENSE file.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2014-06-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固