简单/表示器

概念验证:使用方法链语法的对象序列化/反序列化

dev-master 2016-02-10 18:08 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:58:35 UTC


README

Build Status Latest Version on Packagist Total Downloads Software License

SensioLabsInsight Coverage Status Quality Score Code Climate

概念验证表示器对象,具有链语法规则符号。执行对象序列化和对象恢复。

目前不支持嵌套值。

动机

拥有一个具有表示逻辑的对象,能够将复杂对象转换为数组/字符串表示,反之亦然。根据相同的规则。例如:序列化AJAX数据输出,并在POST操作中恢复后端域模型。拥有无持久性或域逻辑的表示。
或用于后端应用程序中的某些数据映射。

示例

查看测试以获取最新版本。

假设有一个包含无法简单json编码的数据的对象

class Post
{
    public $title = 'Cool story bro';
    public $status = 1;
    public $pubDate;

    public function __construct()
    {
        $this->pubDate = new \DateTime();
    }
}

创建具有表示规则的Representer类。您可以重命名选项,分配默认值(如果为null),并指定自定义geter/setter

class PostRepresenter
{
    use \einfach\representer\Representer;

    public function rules()
    {
        return [
            $this->property('title')
                ->rename('titleAs')
                ->def('Hi there!'),

            $this->property('status'),

            $this->property('pubDate')
                ->getter([$this, 'showDate'])
                ->setter([$this, 'extractDate'])
        ];
    }

    public function showDate($object, $attributeName)
    {
        return $object->$attributeName->format('Y-m-d');
    }

    public function extractDate($object, $attributeName, $value)
    {
        return \DateTime::createFromFormat('Y-m-d', $value);
    }
}

表示

$post = new Post();
$projection = PostRepresenter::one($post)->toArray();

集合表示

$post1 = new Post();
$post2 = new Post();
$post3 = new Post();
$posts = [$post1, $post2, $post3];
$projection = PostRepresenter::collection($posts)->toArray();

从表示中恢复对象

从表示数组数据中恢复对象

$restoredPost = PostRepresenter::restore(Post::class)->fromArray($projection);

从表示中恢复对象集合

$restoredPosts = PostRepresenter::restoreCollection(Post::class)->fromArray($collectionProjection);

序列化

您可以直接将对象序列化为JSON或YAML。序列化能力应通过相应的特性添加

class PostRepresenter
{
    use \einfach\representer\Representer;
    use \einfach\representer\serializer\JSON;
    ....
}

$projection = PostRepresenter::one($post)->toJSON();
class PostRepresenter
{
    use \einfach\representer\Representer;
    use \einfach\representer\serializer\YAML;
    ....
}

$projection = PostRepresenter::one($post)->toYAML();

集合表示也是如此。

反序列化

与序列化非常相似,它具有相反的功能

  • fromArray
  • fromJSON
  • fromYAML
class PostRepresenter
{
    use \einfach\representer\Representer;
    use \einfach\representer\serializer\JSON;
    ....
}

$projection = PostRepresenter::one($post)->toJSON();
$object = PostRepresenter::one($post)->fromJSON($projection);

功能想法

  • 特性组合(表示器不是继承的,而是通过特性添加的)
  • 序列化/反序列化(《toJSON》,《toYAML》)
  • 反序列化(《fromJSON》,《fromYAML》,《fromArray》)
  • 集合表示 ::collection->collection()
  • 包装集合 ->wrap('items')->removeWrap() 用于 ->collection()
  • 逆属性声明(允许投影中任何属性名称,不与源耦合)
  • 属性规则:render_null(管理默认值?示例 rename: function($object, $attr) { return uppercase($attr); }
  • 属性装饰/嵌套序列化(《->representer(ArtistRepresenter::class)->class(Artist::class))
  • 嵌套属性 ->property('artist')->nested([ $this->property('films')->..., $this->property('name')->... ])
  • “数组到数组”和“对象到对象”表示的能力
  • 强制转换(->int->float->string)。将复杂类型/类强制转换为DateTime?的方式
  • ::one::collection中的外部选项(应传递给所有$callables)
  • 检查表示器继承是否覆盖规则(尝试使用->inherit(true)进行部分覆盖)
  • 尝试使用表示器混合(通过特性?)
  • 使用https://github.com/phpbench/phpbench进行基准测试

致谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。