
🔗 Project on GitHub
View the full source code and contribute on GitHub:
https://github.com/BcommeBois/oihana-php-schema
✨ Key Features
- ✔️ Full modeling of Schema.org entities
- 🧩 Automatic JSON-LD serialization (
JsonSerializable
) - 🪄 Recursive object hydration (including nested types and union types)
- 🧠 Internal reflection system (
oihana\reflections
) - 🎯 Safe property access via constants (e.g.
Prop::NAME
) - 📚 Extensible architecture for custom ontologies
- 🔐 Support for ArangoDB metadata (
_id
,_key
,_rev
,_from
,_to
)
📦 Installation
This library requires PHP 8.4+ and is installed via Composer:
composer require oihana/php-schema
🚀 Quick Example
Here is a basic usage example using Person
and PostalAddress
:
use org\schema\Person;
use org\schema\PostalAddress;
use org\schema\constants\Prop;
$person = new Person
([
Prop::ID => '2555' ,
Prop::NAME => 'John Doe' ,
Prop::ADDRESS => new PostalAddress
([
Prop::STREET_ADDRESS => '2 chemin des Vergers' ,
Prop::POSTAL_CODE => '49170'
])
]);
echo json_encode($person, JSON_PRETTY_PRINT);
JSON-LD output:
{
"@type": "Person",
"@context": "https://schema.org",
"id": "2555",
"name": "John Doe",
"address":
{
"@type": "PostalAddress",
"@context": "https://schema.org",
"streetAddress": "2 chemin des Vergers",
"postalCode": "49170"
}
}
🧠 Internal Architecture
All schema entities inherit from a base class Thing
, which handles metadata, public properties, and JSON-LD serialization logic:
- Constructor: Hydrates the object via array or stdClass
- JSON Serialization: Reflects public properties and adds
@type
and@context
- Reflection helpers: Provided via internal
ReflectionTrait
🔐 Property Constants
The Prop
class enumerates all known schema properties as constants for safe access:
use org\schema\constants\Prop;
$event = new Event
([
Prop::NAME => 'Oihana Conf 2025',
Prop::LOCATION => new Place( [ Prop::NAME => 'Nantes' ] )
]);
✅ Running Unit Tests
Run all unit tests:
composer run-script test
Run a specific test:
composer run test ./tests/org/schema/ThingTest.php
📄 License
This project is licensed under the Mozilla Public License 2.0 (MPL-2.0).
👤 About the Author
- Name: Marc ALCARAZ (aka eKameleon)
- Website: www.ooop.fr
- Email: marc@ooop.fr