PHPackages                             brick/schema - 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. brick/schema

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

brick/schema
============

Schema.org library for PHP

0.2.0(1y ago)5184.8k↓38.4%6[5 issues](https://github.com/brick/schema/issues)[1 PRs](https://github.com/brick/schema/pulls)1MITPHPPHP ^8.1CI passing

Since Oct 14Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/brick/schema)[ Packagist](https://packagist.org/packages/brick/schema)[ GitHub Sponsors](https://github.com/BenMorel)[ RSS](/packages/brick-schema/feed)WikiDiscussions main Synced today

READMEChangelog (3)Dependencies (3)Versions (5)Used By (1)

Brick\\Schema
-------------

[](#brickschema)

[![](https://raw.githubusercontent.com/brick/brick/master/logo.png)](https://raw.githubusercontent.com/brick/brick/master/logo.png)

A PHP library to read [schema.org](https://schema.org) structured data from HTML pages.

[![Latest Stable Version](https://camo.githubusercontent.com/67ced0d5342890231f416d392b391f6b854d715f4cf73b13367952bd9fd1314e/68747470733a2f2f706f7365722e707567782e6f72672f627269636b2f736368656d612f762f737461626c65)](https://packagist.org/packages/brick/schema)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](http://opensource.org/licenses/MIT)

### Introduction

[](#introduction)

This library extracts structured data (Microdata, RDFa Lite &amp; JSON-LD) from HTML pages using [brick/structured-data](https://github.com/brick/structured-data), and maps any schema.org information found to IDE- and static analysis- friendly objects implementing schema.org interfaces.

### Installation

[](#installation)

This library is installable via [Composer](https://getcomposer.org/):

```
composer require brick/schema
```

### Requirements

[](#requirements)

This library requires PHP 8.1 or later. It makes use of the following extensions:

- [dom](https://www.php.net/manual/en/book.dom.php)
- [json](https://www.php.net/manual/en/book.json.php)
- [libxml](https://www.php.net/manual/en/book.libxml.php)

These extensions are enabled by default, and should be available in most PHP installations.

### Project status &amp; release process

[](#project-status--release-process)

This library is under development. It is likely to change fast in the early `0.x` releases. However, the library follows a strict BC break convention:

The current releases are numbered `0.x.y`. When a non-breaking change is introduced (adding new methods, fixing bugs, optimizing existing code, etc.), `y` is incremented.

**When a breaking change is introduced, a new `0.x` version cycle is always started.**

It is therefore safe to lock your project to a given release cycle, such as `0.2.*`.

If you need to upgrade to a newer release cycle, check the [release history](https://github.com/brick/schema/releases)for a list of changes introduced by each further `0.x.0` version.

### Quickstart

[](#quickstart)

First of all, you need to instantiate a `SchemaReader`:

```
use Brick\Schema\SchemaReader;

// read all available formats:
$schemaReader = SchemaReader::forAllFormats();

// or read a single format:
// $schemaReader = SchemaReader::forMicrodata();
// $schemaReader = SchemaReader::forRdfaLite();
// $schemaReader = SchemaReader::forJsonLd();
```

Then, you can proceed to reading an HTML document:

```
// The URL the document was retrieved from. This will be used only to resolve relative
// URLs in property values. No attempt will be performed to connect to this URL.
$url = 'https://example.com/product/123';

$things = $schemaReader->readHtml($html, $url);               // An HTML document as a string
// or $things = $schemaReader->readHtmlFile($htmlFile, $url); // A path to an HTML file
// or $things = $schemaReader->read($domDocument, $url);      // A DOMDocument instance
```

This returns an array of `Thing` instances, which is the [base class](https://schema.org/Thing) from which all schema.org objects inherit.

All the objects returned implement `Thing`, but more importantly, they implement the interface(s) defined in the markup, such as `Person` or `Product`. These objects pass `instanceof` checks, and allow IDE autocompletion and static analysis by listing their available properties.

Schema.org interfaces live under the `Brick\Schema\Interfaces` namespace:

- `Brick\Schema\Interfaces\Thing`
    - `Brick\Schema\Interfaces\Person`
    - ...

Every property of every interface, for example `Person::$birthDate` is an instance of `SchemaTypeList`, which is a container for zero or more values. Each value may be another `Thing`, or a plain text `string`.

With that in mind, let's see an example:

```
use Brick\Schema\Interfaces as Schema;

foreach ($things as $thing) {
    if ($thing instanceof Schema\Product) {
        // Your IDE should now provide autocompletion for available Product properties:
        // category, color, gtin, sku, offers, ...

        foreach ($thing->offers as $offer) {
            // You should always check if the Thing matches the expected type,
            // even if the schema.org property documents a single type (here, Offer).
            // See the Caveats section below for an explanation why.

            if ($offer instanceof Schema\Offer) {
                // Yes! we do have an offer, let's check its price.
                // Don't forget that all properties have zero or more values, let's take the first one:

                $price = $offer->price->getFirstValue();

                // For the same reason as above (see Caveats), always check the type of the value,
                // it could very well be a nested Thing instance, or null if there is no value.

                if (is_string($price)) {
                    echo $price;
                }

                // There are also helper functions in the SchemaTypeList object when you expect a string.
                // For example, this will return the first string value, or null if not found:

                $priceCurrency = $offer->priceCurrency->toString();

                if ($priceCurrency !== null) {
                    echo $priceCurrency;
                }
            }
        }
    }
}
```

Note: if you're attempting to access a property that's not defined on any of the types the `Thing` object implements, an exception will be thrown.

### Caveats

[](#caveats)

While the schema.org properties are well defined, they are designed to be quite lenient in terms of what *values* they accept. While they do document expected types for every property (for example, a [Product](https://schema.org/Product)'s offers may only be of the `Offer` type), in practice this library, in accordance with the schema.org datamodel, **accepts any `Thing` or `string` in any field.**

You should therefore take all documented interface property types with a grain of salt, and ***always* perform checks such as `instanceof` or `is_string()` in your code**.

To quote the [schema.org website](https://schema.org/docs/datamodel.html):

> Conformance
>
> Although it might be helpful for search applications if structured data markup always followed schema.org very strictly, in practice this is unrealistic. Our schemas also continue to evolve in response to feedback, discussion and new applications of the data. Where possible we amend existing definitions incrementally rather than introducing lots of new properties for similar use cases. **We have consequently based schema.org on a very flexible datamodel, and take a pragmatic view of conformance.**
>
> We expect schema.org properties to be used with new types, both from schema.org and from external extensions. **We also expect that often, where we expect a property value of type Person, Place, Organization or some other subClassOf Thing, we will get a text string, even if our schemas don't formally document that expectation.** In the spirit of "some data is better than none", search engines will often accept this markup and do the best we can. Similarly, some types such as Role and URL can be used with all properties, and we encourage this kind of experimentation amongst data consumers.

###  Health Score

50

—

FairBetter than 95% of packages

Maintenance64

Regular maintenance activity

Popularity45

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 98.2% 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 ~1033 days

Total

3

Last Release

386d ago

PHP version history (2 changes)0.1.0PHP &gt;=7.2

0.2.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/57189121968030f0770811b461cc92f9c19c08f5c4767292f2ede48b7277cfad?d=identicon)[BenMorel](/maintainers/BenMorel)

---

Top Contributors

[![BenMorel](https://avatars.githubusercontent.com/u/1952838?v=4)](https://github.com/BenMorel "BenMorel (56 commits)")[![GrahamCampbell](https://avatars.githubusercontent.com/u/2829600?v=4)](https://github.com/GrahamCampbell "GrahamCampbell (1 commits)")

---

Tags

schemabrickJSON-LDstructured-dataschema.orgmicrodatardfa lite

###  Code Quality

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/brick-schema/health.svg)

```
[![Health](https://phpackages.com/badges/brick-schema/health.svg)](https://phpackages.com/packages/brick-schema)
```

###  Alternatives

[torann/json-ld

Extremely simple JSON-LD markup generator.

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

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

34653.7k16](/packages/brotkrueml-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)[brick/structured-data

Microdata, RDFa Lite &amp; JSON-LD structured data reader

26198.8k1](/packages/brick-structured-data)[bramdeleeuw/silverstripe-schema

Add schema to a Silverstripe page

109.4k2](/packages/bramdeleeuw-silverstripe-schema)[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)
