PHPackages                             anton-am/json-ld - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. anton-am/json-ld

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

anton-am/json-ld
================

Extremely simple JSON-LD markup generator.

1.0.0(3y ago)01611BSD-2-ClausePHPPHP &gt;=8.0

Since Mar 13Pushed 3y agoCompare

[ Source](https://github.com/Anton-Am/json-ld)[ Packagist](https://packagist.org/packages/anton-am/json-ld)[ RSS](/packages/anton-am-json-ld/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

JSON-LD Generator
=================

[](#json-ld-generator)

[![Latest Stable Version](https://camo.githubusercontent.com/b9213ebc2ec18accacc2a13a108aedd86e074827735957f470c6b62141cb8ff0/687474703a2f2f706f7365722e707567782e6f72672f616e746f6e2d616d2f6a736f6e2d6c642f76)](https://packagist.org/packages/anton-am/json-ld)[![Total Downloads](https://camo.githubusercontent.com/0cd9137d91b95290d44f510c8babeb09cb0c2a812d6ba6e77163f94ed6d41d6a/687474703a2f2f706f7365722e707567782e6f72672f616e746f6e2d616d2f6a736f6e2d6c642f646f776e6c6f616473)](https://packagist.org/packages/anton-am/json-ld)[![Latest Unstable Version](https://camo.githubusercontent.com/4021008ce603dc4b28fdd290fdb42508391b6d524fd807514c1fb9a95636b18c/687474703a2f2f706f7365722e707567782e6f72672f616e746f6e2d616d2f6a736f6e2d6c642f762f756e737461626c65)](https://packagist.org/packages/anton-am/json-ld)[![License](https://camo.githubusercontent.com/c7a5995767d4ded04af897e792ed08562b23a1695d941a72f5bf28ecda544dc8/687474703a2f2f706f7365722e707567782e6f72672f616e746f6e2d616d2f6a736f6e2d6c642f6c6963656e7365)](https://packagist.org/packages/anton-am/json-ld)[![PHP Version Require](https://camo.githubusercontent.com/d02a4e3a5f3922053e3d7a23d64ab33c0a937d472229a59d06cc6190662489c9/687474703a2f2f706f7365722e707567782e6f72672f616e746f6e2d616d2f6a736f6e2d6c642f726571756972652f706870)](https://packagist.org/packages/anton-am/json-ld)

Extremely simple JSON-LD generator.

Dependencies
------------

[](#dependencies)

- PHP 8.0 or higher
- JSON extension

Installation
------------

[](#installation)

- [JSON-LD Generator on Packagist](https://packagist.org/packages/Anton-Am/json-ld)
- [JSON-LD Generator on GitHub](https://github.com/Anton-Am/json-ld)

From the command line run

```
$ composer require anton-am/json-ld

```

or add to your composer.json

```
"anton-am/json-ld": "^1.0.0"

```

Methods
-------

[](#methods)

**/JsonLd/Context.php**

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

**/JsonLd/MultiContext.php**

- `create(array $contextList, int $type = self::TYPE_GRAPH)`
- `generateScripts()`
- `generateArray()`
- `generateGraph()`
- `generate()`

Context Type Classes
--------------------

[](#context-type-classes)

All currently available context types can be found in the [ContextTypes directory](src/ContextTypes)

Examples
--------

[](#examples)

### Quick Example

[](#quick-example)

#### Business

[](#business)

```
use AntonAm\JsonLD\Context;
use AntonAm\JsonLD\ContextTypes\LocalBusiness;

$context = Context::create(LocalBusiness::class, [
    '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

[](#news-article)

```
use AntonAm\JsonLD\Context;
use AntonAm\JsonLD\ContextTypes\NewsArticle;

$context = Context::create(NewsArticle::class, [
    '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

[](#using-the-json-ld-in-a-laracasts-presenter)

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

#### /App/Presenters/BusinessPresenter.php

[](#apppresentersbusinesspresenterphp)

```
