Installation:
composer require zozlak/rdf-constants
Add to composer.json if not using autoloading:
"autoload": {
"psr-4": {
"App\\": "app/",
"Zozlak\\RdfConstants\\": "vendor/zozlak/rdf-constants/src/"
}
}
Run composer dump-autoload.
First Use Case:
Access constants via the Zozlak\RdfConstants\Rdf facade or class:
use Zozlak\RdfConstants\Rdf;
// Example: Get a SKOS constant
$collectionUri = Rdf::SKOS_COLLECTION;
Key Classes:
Rdf (facade) – Primary entry point for all constants.RdfConstants (class) – Direct access to all namespaced constants (e.g., RdfConstants::DC_TITLE).Semantic Web Integration: Use constants to define RDF/SKOS/DCAT properties in models or API responses:
$model->properties = [
Rdf::DC_TITLE => 'My Resource',
Rdf::SKOS_PREF_LABEL => 'Preferred Label',
];
Dynamic Property Handling: Validate or sanitize RDF properties using constants:
$allowedProperties = [
Rdf::DC_TITLE,
Rdf::DC_DESCRIPTION,
Rdf::SKOS_NOTE,
];
if (!in_array($request->property, $allowedProperties)) {
abort(400, 'Invalid RDF property');
}
SPARQL Query Building: Construct queries with constants for consistency:
$query = "PREFIX skos: <{$rdf->SKOS_NAMESPACE}> SELECT ?label WHERE { ?resource skos:prefLabel ?label }";
API Response Normalization: Standardize responses using RDF constants as keys:
return response()->json([
'title' => $resource->title,
'dcterms_title' => Rdf::DC_TITLE, // Metadata for clients
]);
Namespace Resolution: Extract namespaces dynamically for configuration:
$namespaces = [
'skos' => Rdf::SKOS_NAMESPACE,
'dcterms' => Rdf::DC_NAMESPACE,
];
Laravel Eloquent: Use constants in accessors/mutators for RDF-aware models:
public function getSkosLabelAttribute()
{
return $this->attributes[Rdf::SKOS_PREF_LABEL] ?? null;
}
Form Requests:
Validate RDF properties in FormRequest classes:
public function rules()
{
return [
'property' => 'required|in:'.implode(',', [
Rdf::DC_TITLE,
Rdf::SKOS_ALTERNATE,
]),
];
}
Service Providers: Bind constants to the container for dependency injection:
$this->app->bind('rdf.constants', function () {
return new \Zozlak\RdfConstants\Rdf();
});
Namespace Conflicts: Avoid collisions with custom constants. Prefix usage:
// Bad: Assume `DC_TITLE` is unique.
// Good: Use `Rdf::DC_TITLE` explicitly.
Deprecated Constants:
Check for typos (e.g., SKOS_COLLECTION was fixed in v1.2.1). Verify with:
dd(get_declared_constants(true)['user']['rdf'] ?? []);
Namespace URIs:
Constants like SKOS_NAMESPACE return full URIs (e.g., http://www.w3.org/2004/02/skos/core#). Trim trailing # if needed:
$prefix = rtrim(Rdf::SKOS_NAMESPACE, '#');
Immutable Constants: Constants are static. For dynamic values (e.g., locale-specific labels), extend the class:
class ExtendedRdf extends \Zozlak\RdfConstants\Rdf {
public static function SKOS_LABEL($lang = 'en') {
return "http://www.w3.org/2004/02/skos/core#{$lang}Label";
}
}
Verify Constants: Dump all available constants to debug:
dd((new \Zozlak\RdfConstants\Rdf())->getConstants());
SPARQL Validation: Test queries with a tool like W3C SPARQL Validator to ensure constants resolve correctly.
Custom Namespaces: Extend the base class to add domain-specific constants:
class AppRdf extends \Zozlak\RdfConstants\Rdf {
public const MYAPP_NAMESPACE = 'http://example.com/ns#';
public const MYAPP_CUSTOM_PROP = self::MYAPP_NAMESPACE . 'customProp';
}
Localization: Override label constants for multilingual support:
class LocalizedRdf extends \Zozlak\RdfConstants\Rdf {
public const SKOS_PREF_LABEL = 'http://example.com/skos/prefLabel_'.app()->getLocale();
}
Configuration: Load constants from a config file for environment-specific overrides:
// config/rdf.php
return [
'namespaces' => [
'skos' => env('RDF_SKOS_NAMESPACE', Rdf::SKOS_NAMESPACE),
],
];
$cache = Cache::remember('rdf_constants', 60, function () {
return (new \Zozlak\RdfConstants\Rdf())->getConstants();
});
How can I help you explore Laravel packages today?