accredifysg / php-json-ld
A PHP implementation of the JSON-LD 1.1 specification.
Requires
- php: ^8.1
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.1 || ^2.0
Requires (Dev)
- guzzlehttp/guzzle: ^7.0
- guzzlehttp/psr7: ^2.0
- laravel/pint: ^1.0
- mockery/mockery: ^1.6
- pestphp/pest: ^2.14
- phpstan/phpstan: ^2.1
Suggests
- psr/http-client-implementation: A PSR-18 client (e.g. guzzlehttp/guzzle) is required if you use HttpDocumentLoader for resolving remote @context URLs.
- psr/http-factory-implementation: A PSR-17 request factory (e.g. guzzlehttp/psr7) is required if you use HttpDocumentLoader.
README
A PHP implementation of the JSON-LD 1.1 specification.
Status: Pre-1.0. v0.1.0 ships the JSON-LD code extracted from accredifysg/verifiable-credentials-php. It is functionally complete for VCv2 / Open Badges v3 expansion but is not yet spec-compliant with JSON-LD 1.1 — see CHANGELOG for known limitations. The public API may change before v1.0.
Goals
- Faithful implementation of the JSON-LD 1.1 algorithms defined in the JSON-LD 1.1 Processing Algorithms and API.
- Pluggable document loader so consumers (e.g. verifiable-credential libraries)
can serve known
@contextURLs from local resources. - No mandatory HTTP client dependency — bring your own (Guzzle is suggested, not required).
- Tested against the official W3C JSON-LD test suite.
Planned scope (v1.0)
- Custom
DocumentLoaderinterface - [~] Expansion (§5.5) — implemented; ~244/385 of the W3C expand suite
- [~] Compaction (§5.6) — implemented incl. container-maps; ~100/246 of the W3C compact suite
- [~] Serialize JSON-LD to RDF (§7 /
toRdf) — implemented; ~311/467 of the W3C toRdf suite (N-Quads output;@json/JCS,rdfDirection, and generalized RDF pending)
Out of scope for v1.0: Flattening, Framing, RDF-to-JSON-LD (fromRdf).
Installation
composer require accredifysg/php-json-ld:^0.1
Requires PHP 8.1+. You also need a PSR-18 HTTP client + PSR-17 request
factory if you use the bundled HttpDocumentLoader (e.g. guzzlehttp/guzzle
and guzzlehttp/psr7), or you can implement DocumentLoader yourself to
serve @context URLs from wherever you like.
Usage
use Accredify\JsonLd\JsonLdProcessor; use Accredify\JsonLd\Loaders\CachingDocumentLoader; use Accredify\JsonLd\Loaders\HttpDocumentLoader; use GuzzleHttp\Client; use GuzzleHttp\Psr7\HttpFactory; $loader = new CachingDocumentLoader( new HttpDocumentLoader(new Client, new HttpFactory), ); $processor = new JsonLdProcessor($loader); $expanded = $processor->expand($yourDocument)->toArray();
If you want to serve known contexts from local files (recommended for
verifiable credentials), implement Accredify\JsonLd\Contracts\DocumentLoader
yourself. See
tests/Algorithms/Characterization/Support/BundledContextLoader.php
for an example.
Compliance
The package is tested against the
official W3C JSON-LD 1.1 test suite,
pulled in as a git submodule at tests/w3c/. See
tests/W3c/README.md for harness layout and how to
run it.
# Run only the project's unit tests (the default) composer test # Run the W3C conformance suite composer test:w3c
A per-algorithm PASS/FAIL matrix will appear in this section once Phase 4 is in progress (see the plan).
Characterization fixtures
tests/Algorithms/Characterization/ holds JSON snapshots of the expander's
output, generated from running the original
accredifysg/verifiable-credentials-php JsonLdProcessor over a set of
sample documents. They are NOT a spec-conformance reference — they pin
the package's current quirky behaviour so that spec-correctness work in
later phases can land each behaviour change in a reviewable diff.
When a Phase 4 PR changes expansion in a way that updates these fixtures, the update should be reviewed for correctness and paired with any matching change in downstream consumers (e.g. VC's signed-credential test fixtures).
License
MIT © Accredify