gnugat/marshaller 问题修复 & 功能扩展

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

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

gnugat/marshaller

最新稳定版本:v2.0.0

Composer 安装命令:

composer require gnugat/marshaller

包简介

A PHP library that converts from one format to another

README 文档

README

A PHP library that converts from one format to another.

Marshaller doesn't try to guess how to convert the given input, instead it relies on MarshallerStrategies implemented by the developers: it gives us full control on the output formats.

To automatically convert an input into specific formats (like XML, JSON or YAML), it might be better to use other tools (e.g. JMS Serializer).

Installation

Marshaller can be installed using Composer:

composer require "gnugat/marshaller:~2.0"

Simple conversion

Let's take the following object:

<?php

class Article
{
    public function __construct($title, $content)
    {
        $this->title = $title;
        $this->content = $content;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function getContent()
    {
        return $this->content;
    }
}

$article = new Article('Nobody expects...', '... The Spanish Inquisition!');

If we want to convert it to the following format:

// ...

array(
    'title' => 'Nobody expects...',
    'content' => '... The Spanish Inquisition!',
);

Then we have first to create a MarshallerStrategy:

// ...

require __DIR__.'/vendor/autoload.php';

use Gnugat\Marshaller\MarshallerStrategy;

class ArticleMarshaller implements MarshallerStrategy
{
    public function supports($toMarshal, $category = null)
    {
        return $toMarshal instanceof Article;
    }

    public function marshal($toMarshal)
    {
        return array(
            'title' => $toMarshal->getTitle(),
            'content' => $toMarshal->getContent(),
        );
    }
}

The second step is to register it in Marshaller:

// ...

use Gnugat\Marshaller\Marshaller;

$marshaller = new Marshaller();
$marshaller->add(new ArticleMarshaller());

Finally we can actually convert the object:

// ...

$marshalledArticle = $marshaller->marshal($article);

Partial conversion

If we need to convert Article into the following partial format:

// ...

array('title' => 'Nobody expects...');

Then we first have to define a new MarshallStrategy:

// ...

class PartialArticleMarshaller implements MarshallStrategy
{
    public function supports($toMarshal, $category = null)
    {
        return $toMarshal instanceof Article && 'partial' === $category;
    }

    public function marshal($toMarshal)
    {
        return array(
            'title' => $toMarshal->getTitle(),
        );
    }
}

Since this MarshallerStrategy has a more restrictive support condition, we'd like it to be checked before ArticleMarshaller. This can be done by registering PartialArticleMarshaller with a higher priority than ArticleMarshaller (in this case with a priority higher than 0):

// ...

$marshaller->add(new PartialArticleMarshaller, 1);

Finally we can call Marshaller, for the partial category:

$marshaller->marshal($article, 'partial');

Collection conversions

In order to avoid this:

// ...

$articles = array($article);
foreach ($articles as $article) {
    $marshaller->marshal($article);
}

We can use the following short cut method:

// ...

$marshaller->marshalCollection($articles);

Further documentation

You can see the current and past versions using one of the following:

You can find more documentation at the following links:

统计信息

  • 总下载量: 359
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-19