PHPackages                             beastbytes/schema-dot-org-helper - 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. beastbytes/schema-dot-org-helper

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

beastbytes/schema-dot-org-helper
================================

A helper for generating Schema.org JSON-LD

v2.0.0(3y ago)010PHPPHP ^8.0

Since Jun 24Pushed 3y ago1 watchersCompare

[ Source](https://github.com/beastbytes/schema-dot-org-helper)[ Packagist](https://packagist.org/packages/beastbytes/schema-dot-org-helper)[ RSS](/packages/beastbytes-schema-dot-org-helper/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (10)Versions (3)Used By (0)

Schema.org Helper (schema-dot-org-helper)
=========================================

[](#schemaorg-helper-schema-dot-org-helper)

A Helper for generating Schema.org schemas in JSON-LD.

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist beastbytes/schemadotorg

```

or add

```
"beastbytes/schema-dot-org": "^2.0"
```

to the require section of your composer.json.

Usage
-----

[](#usage)

To generate a schema:

```
// In the view
use BeastBytes\SchemaDotOrg\SchemaDotOrg;

$mapping = [
    // define mapping
];
$model = [
    // model can be an array or an object
];
$schema = SchemaDotOrg::generate($model, $mapping);
// Multiple schemas can be generated
```

The generated schema can be rendered directly

```
echo SchemaDotOrg::generate($model, $mapping);
```

or registered with the view:

```
$this->registerJs(SchemaDotOrg::generate($model, $mapping));
```

Defining a Schema Mapping
-------------------------

[](#defining-a-schema-mapping)

A schema mapping is an array that defines the mapping of model properties to Schema.org properties; it is of the form:

```
$mapping = [
    'Type' => [
        'schemaDotOrgProperty' => 'model.property', // or
        'model.property' // if the Schema.org and property names are the same
    ]
];
```

Where a Schema.org property is defined as a Schema.org type, the type is a nested array:

```
[
    'Type' => [
        'schemaDotOrgProperty' => [
            'NestedType' => [
                // ...
            ]
        ]
    ]
]
```

If a Schema.org property is to be a string literal, prepend with SchemaDotOrg::STRING\_LITERAL :

```
[
    'Type' => [
        'schemaDotOrgProperty' => SchemaDotOrg::STRING_LITERAL . 'Literal value'
    ]
]
```

If a Schema.org property is a SchemaDotOrg Enumeration value, prepend with SchemaDotOrg::ENUMERATION :

```
[
    'Type' => [
        'schemaDotOrgProperty' => SchemaDotOrg::ENUMERATION . 'EnumerationName'
    ]
]
```

If a Schema.org property is an array of values - usually nested types - specify the mapping as an array. The key must be or start with SchemaDotOrg::ARRAY. If it is just SchemaDotOrg::ARRAY the mapping parent key is the model property, else the model property is the remainder of the key; both forms are shown below:

```
[
    'EducationalOrganization' => [
        'name',
        'alumni' => [
            SchemaDotOrg::ARRAY => [ // the model property is 'alumni'
                'Person' => [
                    'familyName',
                    'givenName'
                ]
            ]
        ]
    ]
]
```

```
[
    'EducationalOrganization' => [
        'name',
        'alumni' => [
            SchemaDotOrg::ARRAY . 'pastPupils' => [ // the model property is 'pastPupils'
                'Person' => [
                    'familyName',
                    'givenName'
                ]
            ]
        ]
    ]
]
```

Example schema mapping definition:

```
[
    'LocalBusiness' => [ // @type always begins with an uppercase letter
        'name' => 'org', // maps the 'org' property of the model to the Schema.org 'name' property
        'address' => [ // the Schema.org 'address' property is a PostalAddress type
            'PostalAddress' => [ // @type
                'adr.streetAddress', // no need for mapping if the Schema.org and model property names are the same
                'addressLocality' => 'adr.locality', // define the mapping if different property names
                'addressRegion' => 'adr.region',
                'adr.postalCode'
            ]
        ],
        'location' => [
            'Place' => [
                'additionalProperty' => [
                    'PropertyValue' => [
                        'propertyID' => SchemaDotOrg::STRING_LITERAL . 'what3words',
                        'value' => 'adr.what3words',
                    ],
                ],
                'latitude',
                'longitude',
            ],
        ],
        'email',
        'telephone' => 'tel.cell.value',
        'currenciesAccepted' => SchemaDotOrg::STRING_LITERAL . 'GBP',
        'image' => SchemaDotOrg::STRING_LITERAL . 'https://example.com/images/logo.svg',
        'makesOffer' => [
            'Offer' => [
                'name',
                'description',
                'price',
                'priceCurrency' => SchemaDotOrg::STRING_LITERAL . 'GBP',
                'availability' => SchemaDotOrg::ENUMERATION . 'InStock'
            ]
        ]
    ]
]
```

Example JSON-LD generated using the above schema mapping:

```

{
  "@context": "https://schema.org",
  "@type": "LocalBusiness",
  "name": "Business Name",
  "address": {
    "@type": "PostalAddress",
    "streetAddress": "99 Fencott Road",
    addressLocality: "Fencott",
    addressRegion: "Oxon",
    postalCode: "OX5 2RD"
  }
  "location": {
    "@type": "Place",
    "additionalProperty": {
      "@type": "PropertyValue",
      "propertyID": "what3words",
      "value": "tangent.migrate.commander"
    },
    "latitude": 51.84095049377005,
    "longitude": -1.1709238113995422,
  },
  "email": "getintouch@example.com",
  "telephone": "01865 369248",
  "currenciesAccepted": "GBP",
  "image": "https://example.com/images/logo.svg",
  "makesOffer": {
    "@type": "Offer",
    "name": "Awesome Product",
    "description": "The ony product you will ever need",
    "price": 999.99,
    "priceCurrency": "GBP",
    "availability": "https://schema.org/InStock"
  }
}

```

Twig Templates
--------------

[](#twig-templates)

To use the helper in a Twig templates either include it in CommonViewInjection (in the examples it is assigned to the schemaDotOrg variable) or in the template

```
{% set schemaDotOrg = get('BeastBytes\\SchemaDotOrg\\SchemaDotOrg') %}
```

Then in the template either:

to echo the schema immediately:

```
{{ schemaDotOrg.generate(model, mapping) }}
```

or to register the schema with the view:

```
{% do this.registerJs(schemaDotOrg.generate(model, mapping)) %}
```

### Defining the Mapping

[](#defining-the-mapping)

- In Twig templates the mapping must define both the SchemaDotOrg property and the model property, even if they have the same name.
- To use the SchemaDotOrg class constants use Twig's constant() function and concatenate the string

For example:

```
{
    Offer: {
        name: 'name',
        description: 'description',
        price: 'price',
        priceCurrency: constant('STRING_LITERAL', schemaDotOrg) ~ 'GBP',
        availability: constant('ENUMERATION', schemaDotOrg) ~ 'InStock'
    }
}
```

Testing
=======

[](#testing)

All testing is carried out from the root directory.

Unit testing
------------

[](#unit-testing)

The package is tested with [PHPUnit](https://phpunit.de/). To run tests: composer test

Mutation testing
----------------

[](#mutation-testing)

The package tests are checked with [Infection mutation framework](https://infection.github.io/) with [Infection Static Analysis Plugin](https://github.com/Roave/infection-static-analysis-plugin). To run it: composer infection

Static analysis
---------------

[](#static-analysis)

The code is statically analyzed with [Psalm](https://psalm.dev/). To run static analysis: composer psalm

License
=======

[](#license)

The Schema.org Helper is free software. It is released under the terms of the BSD License. Please see [LICENSE](./license.md) for more information.

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

2

Last Release

1101d ago

Major Versions

v1.0.0 → v2.0.02023-06-28

### Community

Maintainers

![](https://www.gravatar.com/avatar/b0b5133b5137b26eeb25056449c28364ac0af31836eefba78912a9c8f0743aa3?d=identicon)[BeastBytes](/maintainers/BeastBytes)

---

Top Contributors

[![beastbytes](https://avatars.githubusercontent.com/u/1470144?v=4)](https://github.com/beastbytes "beastbytes (38 commits)")

---

Tags

json-ldschema-orgyii3helperJSON-LDstructured-dataschema.org

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/beastbytes-schema-dot-org-helper/health.svg)

```
[![Health](https://phpackages.com/badges/beastbytes-schema-dot-org-helper/health.svg)](https://phpackages.com/packages/beastbytes-schema-dot-org-helper)
```

###  Alternatives

[brotkrueml/schema

Embedding schema.org vocabulary - API and view helpers for schema.org markup

34653.7k16](/packages/brotkrueml-schema)[yiisoft/html

Handy library to generate HTML

58611.3k51](/packages/yiisoft-html)[torann/json-ld

Extremely simple JSON-LD markup generator.

149641.1k1](/packages/torann-json-ld)[brick/schema

Schema.org library for PHP

5184.8k1](/packages/brick-schema)[simialbi/yii2-schema-org

Schema.org yii2 representation and helpers for json-ld generation in Yii framework

18106.5k4](/packages/simialbi-yii2-schema-org)[devrabiul/laravel-seo-manager

Laravel SEO Manager is an SEO tool that improves SEO by adding recommended meta tags.

396.7k](/packages/devrabiul-laravel-seo-manager)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
