Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Rdf Constants Laravel Package

zozlak/rdf-constants

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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.

  2. 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;
    
  3. Key Classes:

    • Rdf (facade) – Primary entry point for all constants.
    • RdfConstants (class) – Direct access to all namespaced constants (e.g., RdfConstants::DC_TITLE).

Implementation Patterns

Common Workflows

  1. 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',
    ];
    
  2. 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');
    }
    
  3. SPARQL Query Building: Construct queries with constants for consistency:

    $query = "PREFIX skos: <{$rdf->SKOS_NAMESPACE}> SELECT ?label WHERE { ?resource skos:prefLabel ?label }";
    
  4. API Response Normalization: Standardize responses using RDF constants as keys:

    return response()->json([
        'title' => $resource->title,
        'dcterms_title' => Rdf::DC_TITLE, // Metadata for clients
    ]);
    
  5. Namespace Resolution: Extract namespaces dynamically for configuration:

    $namespaces = [
        'skos' => Rdf::SKOS_NAMESPACE,
        'dcterms' => Rdf::DC_NAMESPACE,
    ];
    

Integration Tips

  • 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();
    });
    

Gotchas and Tips

Pitfalls

  1. Namespace Conflicts: Avoid collisions with custom constants. Prefix usage:

    // Bad: Assume `DC_TITLE` is unique.
    // Good: Use `Rdf::DC_TITLE` explicitly.
    
  2. Deprecated Constants: Check for typos (e.g., SKOS_COLLECTION was fixed in v1.2.1). Verify with:

    dd(get_declared_constants(true)['user']['rdf'] ?? []);
    
  3. 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, '#');
    
  4. 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";
        }
    }
    

Debugging

  • 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.

Extension Points

  1. 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';
    }
    
  2. 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();
    }
    
  3. Configuration: Load constants from a config file for environment-specific overrides:

    // config/rdf.php
    return [
        'namespaces' => [
            'skos' => env('RDF_SKOS_NAMESPACE', Rdf::SKOS_NAMESPACE),
        ],
    ];
    

Performance

  • Caching: Cache resolved URIs if used frequently (e.g., in loops):
    $cache = Cache::remember('rdf_constants', 60, function () {
        return (new \Zozlak\RdfConstants\Rdf())->getConstants();
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity