2017 © Pedro Peláez
 

library marshaller

A PHP library that converts from one format to another

image

gnugat/marshaller

A PHP library that converts from one format to another

  • Saturday, May 30, 2015
  • by gnupat
  • Repository
  • 2 Watchers
  • 6 Stars
  • 330 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Marshaller SensioLabsInsight Travis CI

A PHP library that converts from one format to another., (*1)

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., (*2)

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)., (*3)

Installation

Marshaller can be installed using Composer:, (*4)

composer require "gnugat/marshaller:~2.0"

Simple conversion

Let's take the following object:, (*5)

<?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:, (*6)

// ...

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

Then we have first to create a MarshallerStrategy:, (*7)

// ...

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:, (*8)

// ...

use Gnugat\Marshaller\Marshaller;

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

Finally we can actually convert the object:, (*9)

// ...

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

Partial conversion

If we need to convert Article into the following partial format:, (*10)

// ...

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

Then we first have to define a new MarshallStrategy:, (*11)

// ...

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):, (*12)

// ...

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

Finally we can call Marshaller, for the partial category:, (*13)

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

Collection conversions

In order to avoid this:, (*14)

// ...

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

We can use the following short cut method:, (*15)

// ...

$marshaller->marshalCollection($articles);

Further documentation

You can see the current and past versions using one of the following:, (*16)

You can find more documentation at the following links:, (*17)

The Versions

30/05 2015

dev-master

9999999-dev https://gnugat.github.io/marshaller

A PHP library that converts from one format to another

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

serializer translator converter marshaller

30/05 2015

v2.0.0

2.0.0.0 https://gnugat.github.io/marshaller

A PHP library that converts from one format to another

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

serializer translator converter marshaller

20/05 2015

v1.1.0

1.1.0.0

A PHP library that converts from one format to another

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

serializer translator converter marshaller

18/05 2015

v1.0.0

1.0.0.0

A PHP library that converts from one format to another

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

serializer translator converter marshaller