2017 © Pedro Peláez
 

symfony-bundle json-ld-bundle

Schema.org JSON-LD generator Symfony Bundle.

image

secit-pl/json-ld-bundle

Schema.org JSON-LD generator Symfony Bundle.

  • Friday, November 10, 2017
  • by secit
  • Repository
  • 1 Watchers
  • 3 Stars
  • 2,152 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 12 Versions
  • 27 % Grown

The README.md

Schema.org JSON-LD Symfony Bundle

Schema.org JSON-LD generator for the Symfony 2.8 and 3.0+., (*1)

PHP 7 support

As of release 3.3.2 of the https://github.com/secit-pl/schema-org all types and properties should be suffixed. This bundle allows using both class versions (the new suffixed classes and the deprecated non suffixed), but be aware that only suffixed classes will work properly on the PHP 7., (*2)

Another reason for moving to the new class naming schema is the fact that all non suffixed classes will be removed in the release 3.4 of the secit-pl/schema-org., (*3)

Installation

From the command line run, (*4)

$ composer require secit-pl/json-ld-bundle

Update your AppKernel by adding the bundle declaration, (*5)

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            ...
            new SecIT\JsonLdBundle\JsonLdBundle(),
        ];

        ...
    }
}

Usage

Basic Usage

First of all you need to create a Transformer which will transform your object to schema.org data mapping., (*6)

namespace Test\TestBundle\JsonLd;

use SecIT\JsonLdBundle\Transformer\TransformerInterface;
use SecIT\SchemaOrg\Mapping\DataType\TextType;
use SecIT\SchemaOrg\Mapping\Property\NameProperty;
use SecIT\SchemaOrg\Mapping\Type\ThingType;

class TestTransformer implements TransformerInterface
{
    public function transform($object)
    {
        return (new ThingType())
            ->setName(new NameProperty(
                new TextType($object->getName())
            ));
    }
}

As the basis for this of this bundle is the https://github.com/secit-pl/schema-org so the transformer should return the object accepted by the JSON-LD Generator., (*7)

Next you need to register the transformer as service using the secit.jsonld_transformer tag in your services.yml., (*8)

services:
    test.object_transformer:
        class: Test\TestBundle\JsonLd\TestTransformer
        tags:
            - { name: secit.jsonld_transformer, class: Test\TestBundle\Classes\ClassToBeTransformedToJsonLd }

If you want to assign more than one class to the same transformer you can add multiple tags to the same service., (*9)

services:
    test.object_transformer:
        class: Test\TestBundle\JsonLd\TestTransformer
        tags:
            - { name: secit.jsonld_transformer, class: Test\TestBundle\Classes\Class1 }
            - { name: secit.jsonld_transformer, class: Test\TestBundle\Classes\Class2 }
            - { name: secit.jsonld_transformer, class: Test\TestBundle\Classes\Class3 }
            ...

From now you can transform the specified in the tag class attribute object (in the following example the \Test\TestBundle\Classes\ClassToBeTransformedToJsonLd) to the JSON-LD as following:, (*10)

$object = new \Test\TestBundle\Classes\ClassToBeTransformedToJsonLd();
$object->setName('Some name');
echo $this->getContainer()->get('secit.json_ld')->generate($object);

The output should be something like this:, (*11)

<script type="application/ld+json">{"@context":"http:\/\/schema.org","@type":"Thing","name":"Some name"}</script>

Advanced usage

In many situations it's required to have a nested transformers to not implement whole logic in the single class. To use nested transformers your Transformer should implement JsonLdAwareInterface. If you don't want to implement the interface methods by your own you can use the JsonLdAwareTrait., (*12)

Here is the simple example of how to use it:, (*13)

PersonTransformer.php, (*14)

namespace Test\TestBundle\JsonLd;

use SecIT\JsonLdBundle\DependencyInjection\JsonLdAwareInterface;
use SecIT\JsonLdBundle\DependencyInjection\JsonLdAwareTrait;
use SecIT\JsonLdBundle\Transformer\TransformerInterface;
use SecIT\SchemaOrg\Mapping\DataType\TextType;
use SecIT\SchemaOrg\Mapping\Property\NameProperty;
use SecIT\SchemaOrg\Mapping\Type\PersonType;

class PersonTransformer implements TransformerInterface
{
    public function transform($person)
    {
        return (new PersonType())
            ->setName(new NameProperty(
                new TextType($person->name)
            ));
    }
}

ArticleTransformer.php, (*15)

namespace Test\TestBundle\JsonLd;

use SecIT\JsonLdBundle\DependencyInjection\JsonLdAwareInterface;
use SecIT\JsonLdBundle\DependencyInjection\JsonLdAwareTrait;
use SecIT\JsonLdBundle\Transformer\TransformerInterface;
use SecIT\SchemaOrg\Mapping\DataType\TextType;
use SecIT\SchemaOrg\Mapping\Property\AuthorProperty;
use SecIT\SchemaOrg\Mapping\Property\NameProperty;
use SecIT\SchemaOrg\Mapping\Type\ArticleType;

class ArticleTransformer implements TransformerInterface, JsonLdAwareInterface
{
    use JsonLdAwareTrait;

    public function transform($article)
    {
        return (new ArticleType())
            ->setName(new NameProperty(
                new TextType($article->name)
            ))
            ->setAuthor(new AuthorProperty(
                $this->getJsonLd()->transform($article->author)
            ));
    }
}

Example input object:, (*16)

$author = new Person();
$author->name = 'Jon Smith';

$article = new Article();
$article->name = 'Example article';
$article->author = $author;

The output:, (*17)

<script type="application/ld+json">{"@context":"http:\/\/schema.org","@type":"Article","name":"Example article","author":{"@type":"Person","name":"Jon Smith"}}</script>

Twig Support

This bundle also provides the Twig extension which allows to render JSON-LD directly from the Twig templates., (*18)

TestController:, (*19)

namespace Test\TestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class TestController extends Controller
{
    public function testAction()
    {
        return $this->render('TestBundle:Test:example.html.twig', [
            'object' => new Foo(),
        ]);
    }
}

example.html.twig:, (*20)

{{ object|json_ld }}

The output:, (*21)

<script type="application/ld+json">{"@context":"http:\/\/schema.org", ... }</script>

The Versions

10/11 2017

dev-master

9999999-dev http://secit.pl

Schema.org JSON-LD generator Symfony Bundle.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

schema bundle generator json-ld symfony schema.org structured-data

10/11 2017

2.2.1

2.2.1.0 http://secit.pl

Schema.org JSON-LD generator Symfony Bundle.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

schema bundle generator json-ld symfony schema.org structured-data

06/11 2017

2.2.0

2.2.0.0 http://secit.pl

Schema.org JSON-LD generator Symfony Bundle.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

schema bundle generator json-ld symfony schema.org structured-data

10/08 2017

2.1.0

2.1.0.0 http://secit.pl

Schema.org JSON-LD generator Symfony Bundle.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

schema bundle generator json-ld symfony schema.org structured-data

06/01 2017

2.0.0

2.0.0.0 http://secit.pl

Schema.org JSON-LD generator Symfony Bundle.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

schema bundle generator json-ld symfony schema.org structured-data

22/12 2016

1.3.2

1.3.2.0 http://secit.pl

Schema.org JSON-LD generator Symfony Bundle.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

schema bundle generator json-ld symfony schema.org structured-data

22/12 2016

1.3.1

1.3.1.0 http://secit.pl

Schema.org JSON-LD generator Symfony Bundle.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

schema bundle generator json-ld symfony schema.org structured-data

22/12 2016

1.3.0

1.3.0.0 http://secit.pl

Symfony wrapper bundle for the torann/json-ld JSON-LD generator.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

schema bundle generator json-ld symfony schema.org structured-data

21/12 2016

1.2.0

1.2.0.0 http://secit.pl

Symfony wrapper bundle for the torann/json-ld JSON-LD generator.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

schema bundle generator json-ld symfony schema.org structured-data

21/12 2016

1.1.0

1.1.0.0 http://secit.pl

Symfony wrapper bundle for the torann/json-ld JSON-LD generator.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

schema bundle generator json-ld symfony schema.org structured-data

21/12 2016

1.0.1

1.0.1.0 http://secit.pl

Symfony wrapper bundle for the torann/json-ld JSON-LD generator.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

schema bundle generator json-ld symfony schema.org structured-data

21/12 2016

1.0.0

1.0.0.0 http://secit.pl

Symfony wrapper bundle for the torann/json-ld JSON-LD generator.

  Sources   Download

MIT

The Requires

 

by Tomasz Gemza

schema bundle generator json-ld symfony schema.org structured-data