gnugat / marshaller-bundle

该软件包已被废弃且不再维护。作者建议使用 symfony/serializer 软件包。

Symfony 中的 Marshaller 集成

安装: 285

依赖: 0

建议者: 0

安全: 0

星星: 0

观察者: 2

分支: 0

类型:symfony-bundle

v2.0.0 2015-05-30 10:23 UTC

This package is auto-updated.

Last update: 2022-02-01 12:47:46 UTC


README

MarshallerSymfony 中的集成。

安装

可以使用 Composer 安装 MarshallerBundle。

composer require "gnugat/marshaller-bundle:~2.0"

然后我们需要在我们的应用程序中注册它

<?php
// File: app/AppKernel.php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Gnugat\MarshallerBundle\GnugatMarshallerBundle(),
        );
        // ...
    }

    // ...
}

简单转换

让我们以以下对象为例

<?php
// File: src/AppBundle/Entity/Article.php

namespace AppBundle\Entity;

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;
    }
}

如果我们想将其转换为以下格式

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

那么我们首先需要创建一个 MarshallerStrategy

<?php
// File: src/AppBundle/Marshaller/ArticleMarshaller.php

use AppBundle\Entity\Article;
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(),
        );
    }
}

第二步是将其定义为服务

# File: app/config/services.yml
services:
    app.article_marshaller:
        class: AppBundle\Marshaller\ArticleMarshaller
        tags:
            - { name: gnugat_marshaller }

注意:由于 gnugat_marshaller 标签,ArticleMarshaller 将注册到主要的 gnugat_marshaller.marshaller 服务中。

最后,我们可以真正地将对象转换为

<?php
// File: src/AppBundle/Controller/ArticleController.php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;

class ArtcileController extends Controller
{
    /**
     * @Route("/api/v1/articles")
     * @Method({"GET"})
     */
    public function listAction()
    {
        $articles = $this->get('app.article_repository')->findAll();

        return new JsonResponse($this->get('gnugat_marshaller.marshaller')->marshalCollection($articles), 200);
    }

    /**
     * @Route("/api/v1/articles/{id}")
     * @Method({"GET"})
     */
    public function viewAction(Article $article)
    {
        return new JsonResponse($this->get('gnugat_marshaller.marshaller')->marshal($article), 200);
    }
}

注意gnugat_marshaller.marshaller 也可以对 Article 集合调用 ArticleMarshaller

部分转换

如果我们需要将 Article 转换为以下格式

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

那么我们首先需要定义一个新的 MarshallerStrategy

// File: src/AppBundle/Marshaller/ArticleMarshaller.php

use AppBundle\Entity\Article;
use Gnugat\Marshaller\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(),
        );
    }
}

由于这个 MarshallerStrategy 有一个更严格的 support 条件,我们希望在 ArticleMarshaller 之前进行检查。这可以通过将 PartialArticleMarshaller 注册为比 ArticleMarshaller 更高的优先级(在这种情况下,优先级高于 0)来实现。

# File: app/config/services.yml
services:
    app.article_marshaller:
        class: AppBundle\Marshaller\PartialArticleMarshaller
        tags:
            - { name: gnugat_marshaller, priority: 1 }

最后,我们可以调用 Marshaller,对于 partial 类别

<?php
// File: src/AppBundle/Controller/ArticleController.php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;

class ArtcileController extends Controller
{
    /**
     * @Route("/api/v1/articles")
     * @Method({"GET"})
     */
    public function listAction()
    {
        $articles = $this->get('app.article_repository')->findAll();

        return new JsonResponse($this->get('gnugat_marshaller.marshaller')->marshalCollection($articles, 'partial'), 200);
    }

    /**
     * @Route("/api/v1/articles/{id}")
     * @Method({"GET"})
     */
    public function viewAction(Article $article)
    {
        return new JsonResponse($this->get('gnugat_marshaller.marshaller')->marshal($article), 200);
    }
}

进一步文档

您可以使用以下任一方式查看当前和以前的版本

您可以在以下链接中找到更多文档