SchemaWrapper
in package
SchemaWrapper
- Lightweight wrapper around Spatie\SchemaOrg (Schema and returned types).
- Proxies all method calls to underlying Spatie types (magic __call / __callStatic).
- Fluent: if proxied method returns a Schema object (or type) we wrap it again.
- Provides helpers: toScript(), toJsonLd(), addGraphItem(), mergeGraph(), listPublicMethods(), unwrap().
Usage examples are below the class.
Table of Contents
Properties
- $graph : array<string|int, object>
- Optional graph items that will be included when rendering @graph.
- $instance : object
- Underlying Spatie object (Schema type instance) — can be any object returned by Schema::<type>().
- $logger : LoggerInterface|null
- Optional logger for debug (not required).
Methods
- __call() : mixed
- Magic proxy for instance methods.
- __callStatic() : static
- Static factory: forward static calls to Spatie\SchemaOrg\Schema::<type>(...$args)
- __construct() : mixed
- Create wrapper from an underlying Spatie Schema object (or any object).
- __toString() : string
- __toString renders JSON-LD script
- addGraphItem() : $this
- Add an item (raw Spatie wrapper or wrapper instance) to the internal @graph list.
- createType() : static
- Convenience: create wrapper by calling Schema::<type>(...$args) with case-insensitive name.
- debugInfo() : array<string, mixed>
- Dump debug info — optional
- listPublicMethods() : array<string|int, string>
- List public methods of a class (helper). If no class provided, list methods of Schema class.
- mergeGraph() : $this
- Merge (append) graph items from another wrapper.
- toJsonLd() : string
- Render JSON-LD string for this instance.
- toScript() : string
- Return full <script type="application/ld+json">...</script> block
- unwrap() : object
- Return raw underlying object (Spatie type). Useful for advanced operations.
- encodeJson() : string
- Encode JSON with stable flags.
- jsonSerializeSafe() : array<string|int, mixed>|mixed
- Helper: safe jsonSerialize of objects that provide jsonSerialize() method, otherwise attempt to call toArray()/toArrayable or cast to array.
- publicPropertiesToArray() : array<string|int, mixed>
- Attempt to convert public properties of object to array (fallback).
Properties
$graph
Optional graph items that will be included when rendering @graph.
private
array<string|int, object>
$graph
= []
underlying spatie objects or arrays (jsonSerialize results)
$instance
Underlying Spatie object (Schema type instance) — can be any object returned by Schema::<type>().
private
object
$instance
$logger
Optional logger for debug (not required).
private
LoggerInterface|null
$logger
Methods
__call()
Magic proxy for instance methods.
public
__call(string $name, array<int, mixed> $arguments) : mixed
If proxied call returns an object (Spatie Schema type) we wrap it and return wrapper for chaining. If it returns primitive/array we return it directly.
Parameters
- $name : string
- $arguments : array<int, mixed>
__callStatic()
Static factory: forward static calls to Spatie\SchemaOrg\Schema::<type>(...$args)
public
static __callStatic(string $name, array<int, mixed> $arguments) : static
Example: SchemaWrapper::person()->name('Jan')->toScript();
Parameters
- $name : string
- $arguments : array<int, mixed>
Return values
static__construct()
Create wrapper from an underlying Spatie Schema object (or any object).
public
__construct(object $instance[, LoggerInterface|null $logger = null ]) : mixed
Parameters
- $instance : object
-
object returned from Spatie\SchemaOrg\Schema::
() or any other Spatie object - $logger : LoggerInterface|null = null
-
optional PSR-3 logger
__toString()
__toString renders JSON-LD script
public
__toString() : string
Return values
stringaddGraphItem()
Add an item (raw Spatie wrapper or wrapper instance) to the internal @graph list.
public
addGraphItem(object|array<string|int, mixed> $item) : $this
Parameters
- $item : object|array<string|int, mixed>
Return values
$thiscreateType()
Convenience: create wrapper by calling Schema::<type>(...$args) with case-insensitive name.
public
static createType(string $type[, array<int, mixed> $args = [] ]) : static
Example: SchemaWrapper::createType('person', ['name'=>'x']) -> equivalent to Schema::person()->name('x')
Parameters
- $type : string
- $args : array<int, mixed> = []
Return values
staticdebugInfo()
Dump debug info — optional
public
debugInfo() : array<string, mixed>
Return values
array<string, mixed>listPublicMethods()
List public methods of a class (helper). If no class provided, list methods of Schema class.
public
static listPublicMethods([string|null $class = null ]) : array<string|int, string>
Parameters
- $class : string|null = null
Return values
array<string|int, string> —list of public method names
mergeGraph()
Merge (append) graph items from another wrapper.
public
mergeGraph(SchemaWrapper $other) : $this
Parameters
- $other : SchemaWrapper
Return values
$thistoJsonLd()
Render JSON-LD string for this instance.
public
toJsonLd([bool $pretty = false ]) : string
If graph items exist the output will be a JSON-LD with "@graph": [...]
Parameters
- $pretty : bool = false
-
If true formats JSON (for debug)
Return values
string —JSON (not wrapped in <script>)
toScript()
Return full <script type="application/ld+json">...</script> block
public
toScript([bool $pretty = false ]) : string
Parameters
- $pretty : bool = false
Return values
stringunwrap()
Return raw underlying object (Spatie type). Useful for advanced operations.
public
unwrap() : object
Return values
objectencodeJson()
Encode JSON with stable flags.
private
encodeJson(mixed $data[, bool $pretty = false ]) : string
Parameters
- $data : mixed
- $pretty : bool = false
Return values
stringjsonSerializeSafe()
Helper: safe jsonSerialize of objects that provide jsonSerialize() method, otherwise attempt to call toArray()/toArrayable or cast to array.
private
jsonSerializeSafe(object $obj) : array<string|int, mixed>|mixed
Parameters
- $obj : object
Return values
array<string|int, mixed>|mixedpublicPropertiesToArray()
Attempt to convert public properties of object to array (fallback).
private
publicPropertiesToArray(object $obj) : array<string|int, mixed>
Parameters
- $obj : object