2017 © Pedro Peláez
 

library json-ld

Extremely simple JSON-LD markup generator.

image

torann/json-ld

Extremely simple JSON-LD markup generator.

  • Wednesday, June 27, 2018
  • by torann
  • Repository
  • 15 Watchers
  • 86 Stars
  • 37,836 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 41 Forks
  • 8 Open issues
  • 15 Versions
  • 14 % Grown

The README.md

JSON-LD Generator

Build Status Latest Stable Version Total Downloads Patreon donate button Donate weekly to this project using Gratipay Donate to this project using Flattr Donate to this project using Paypal, (*1)

Extremely simple JSON-LD generator., (*2)

Installation

From the command line run, (*3)

$ composer require torann/json-ld

Methods

/JsonLd/Context.php, (*4)

  • create($context, array $data = [])
  • getProperties()
  • generate()

Context Types

  • article
  • audiobook
  • beach
  • blog_posting
  • book
  • breadcrumb_list
  • contact_point
  • corporation
  • creative_work
  • duration
  • event
  • geo_coordinates
  • image_object
  • invoice
  • list_item
  • local_business
  • media_object
  • music_album
  • music_group
  • music_playlist
  • music_recording
  • news_article
  • offer
  • order
  • organization
  • person
  • place
  • postal_address
  • price_specification
  • product
  • rating
  • recipe
  • review
  • sculpture
  • search_box
  • thing
  • video_object
  • web_page
  • web_site

Examples

Quick Example

Business

$context = \JsonLd\Context::create('local_business', [
    'name' => 'Consectetur Adipiscing',
    'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor',
    'telephone' => '555-555-5555',
    'openingHours' => 'mon,tue,fri',
    'address' => [
        'streetAddress' => '112 Apple St.',
        'addressLocality' => 'Hamden',
        'addressRegion' => 'CT',
        'postalCode' => '06514',
    ],
    'geo' => [
        'latitude' => '41.3958333',
        'longitude' => '-72.8972222',
    ],
]);

echo $context; // Will output the script tag

News Article

$context = \JsonLd\Context::create('news_article', [
    'headline' => 'Article headline',
    'description' => 'A most wonderful article',
    'mainEntityOfPage' => [
        'url' => 'https://google.com/article',
    ],
    'image' => [
        'url' => 'https://google.com/thumbnail1.jpg',
        'height' => 800,
        'width' => 800,
    ],
    'datePublished' => '2015-02-05T08:00:00+08:00',
    'dateModified' => '2015-02-05T09:20:00+08:00',
    'author' => [
        'name' => 'John Doe',
    ],
    'publisher' => [
        'name' => 'Google',
        'logo' => [
          'url' => 'https://google.com/logo.jpg',
          'width' => 600,
          'height' => 60,
        ]
    ],
]);

echo $context; // Will output the script tag

Using the JSON-LD in a Laracasts Presenter

Even though this example shows using the JSON-LD inside of a Laracasts\Presenter presenter, Laravel is not required for this package., (*5)

/App/Presenters/BusinessPresenter.php

<?php

namespace App\Presenters;

use JsonLd\Context;
use Laracasts\Presenter\Presenter;

class BusinessPresenter extends Presenter
{
    /**
     * Create JSON-LD object.
     *
     * @return \JsonLd\Context
     */
    public function jsonLd()
    {
        return Context::create('local_business', [
            'name' => $this->entity->name,
            'description' => $this->entity->description,
            'telephone' => $this->entity->telephone,
            'openingHours' => 'mon,tue,fri',
            'address' => [
                'streetAddress' => $this->entity->address,
                'addressLocality' => $this->entity->city,
                'addressRegion' => $this->entity->state,
                'postalCode' => $this->entity->postalCode,
            ],
            'geo' => [
                'latitude' => $this->entity->location->lat,
                'longitude' => $this->entity->location->lng,
            ],
        ]);
    }
}

Generate the Tags

The generator comes with a __toString method that will automatically generate the correct script tags when displayed as a string., (*6)

Inside of a Laravel View, (*7)

echo $business->present()->jsonLd();

Inside of a Laravel View, (*8)

{!! $business->present()->jsonLd() !!}

Custom Context Type

The first argument for the create($context, array $data = []) method also accepts class names. This is helpful for custom context types., (*9)

<?php

namespace App\JsonLd;

use JsonLd\ContextTypes\AbstractContext;

class FooBar extends AbstractContext
{
    /**
     * Property structure
     *
     * @var array
     */
    protected $structure = [
        'name' => null,
        'description' => null,
        'image' => null,
        'url' => null,
    ];
}
$context = \JsonLd\Context::create(\App\JsonLd\FooBar::class, [
    'name' => 'Foo Foo headline',
    'description' => 'Bar bar article description',
    'url' => 'http://google.com',
]);

echo $context; // Will output the script tag

The Versions

27/06 2018

dev-master

9999999-dev

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD-2-Clause BSD 2-Clause

The Requires

  • php >=5.5

 

The Development Requires

schema generator json-ld structured-data

27/06 2018

0.0.13

0.0.13.0

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD-2-Clause

The Requires

  • php >=5.5

 

The Development Requires

schema generator json-ld structured-data

24/05 2018

0.0.12

0.0.12.0

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD-2-Clause

The Requires

  • php >=5.5

 

The Development Requires

schema generator json-ld structured-data

01/02 2018

0.0.11

0.0.11.0

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD-2-Clause

The Requires

  • php >=5.5

 

The Development Requires

schema generator json-ld structured-data

18/08 2017

0.0.10

0.0.10.0

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD 2-Clause

The Requires

  • php >=5.5

 

The Development Requires

schema generator json-ld structured-data

13/02 2017

0.0.9

0.0.9.0

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD 2-Clause

The Requires

  • php >=5.5

 

The Development Requires

schema generator json-ld structured-data

15/12 2016

0.0.8

0.0.8.0

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD 2-Clause

The Requires

  • php >=5.5

 

The Development Requires

schema generator json-ld structured-data

14/10 2016

0.0.7

0.0.7.0

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD 2-Clause

The Requires

  • php >=5.5

 

The Development Requires

schema generator json-ld structured-data

04/10 2016

dev-patch-1

dev-patch-1

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD 2-Clause

The Requires

  • php >=5.5

 

The Development Requires

schema generator json-ld structured-data

21/09 2016

0.0.6

0.0.6.0

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD 2-Clause

The Requires

  • php >=5.5

 

The Development Requires

schema generator json-ld structured-data

07/09 2016

0.0.5

0.0.5.0

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD 2-Clause

The Requires

  • php >=5.5

 

The Development Requires

schema generator json-ld structured-data

30/06 2016

0.0.4

0.0.4.0

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD 2-Clause

The Requires

  • php >=5.5

 

The Development Requires

schema generator json-ld structured-data

22/03 2016

0.0.3

0.0.3.0

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD 2-Clause

The Requires

  • php >=5.6

 

The Development Requires

schema generator json-ld structured-data

08/01 2016

0.0.2

0.0.2.0

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD 2-Clause

The Requires

  • php >=5.6

 

The Development Requires

schema generator json-ld structured-data

08/01 2016

0.0.1

0.0.1.0

Extremely simple JSON-LD markup generator.

  Sources   Download

BSD 2-Clause

The Requires

  • php >=5.5.9

 

The Development Requires

schema generator json-ld structured-data