- How do I install ergebnis/json-pointer in a Laravel project?
- Run `composer require ergebnis/json-pointer` in your project root. The package has no Laravel dependencies and works with PHP 8.0+. No additional configuration is needed for basic usage.
- Does this package support Laravel’s API resources or validation?
- Yes. Use `ReferenceToken` to parse JSON pointers from request input (e.g., `Request::input('pointer')`) and integrate with Laravel’s `FormRequest` or `Validator` for path-based rules. Example: `Validator::make($data, ['pointer' => 'required|json_pointer'])`.
- Can I use this for JSON:API or GraphQL field selection in Laravel?
- Absolutely. The package’s `JsonPointer` class lets you dynamically traverse nested JSON (e.g., `/data/attributes/address`). Pair it with Laravel’s API resources to include/exclude fields based on pointers.
- What Laravel versions and PHP versions are supported?
- The package works with Laravel 9+ (PHP 8.0+) and Laravel 10+ (PHP 8.1+). For Laravel 9, use version `v3.4.0+` of the package. Always check the [Packagist page](https://packagist.org/packages/ergebnis/json-pointer) for updates.
- How do I handle Unicode characters (e.g., emojis) in JSON pointers?
- The package automatically escapes Unicode characters (e.g., `😆` becomes `~1%F0%9F%98%86` in URI fragments or `~😆` in JSON strings). Use `ReferenceToken::fromString('path/😆')` to create tokens with full Unicode support.
- Is there a way to add custom JSON pointer validation in Laravel?
- Yes. Extend Laravel’s `Validator` with a custom rule. Example: Register a `JsonPointerRule` in a service provider and use it like `Validator::extend('json_pointer', function ($attribute, $value, $parameters) { return ReferenceToken::fromString($value)->isValid(); })`.
- Can I use this with Eloquent or Laravel Scout for nested JSON queries?
- Yes. Add a query builder macro (e.g., `whereJsonPointer()`) to Eloquent in your `AppServiceProvider` to filter JSON columns. For Scout, use pointers to index nested fields in your searchable array.
- What are the alternatives to ergebnis/json-pointer for Laravel?
- Alternatives include manual string manipulation with `json_decode()` or packages like `spatie/json-schema`. However, this package offers strict RFC 6901 compliance, type safety, and built-in conversions between string, JSON, and URI formats—ideal for production APIs.
- How do I test JSON pointer logic in Laravel unit tests?
- Mock `ReferenceToken` instances for predictable behavior. Example: `ReferenceToken::fromJsonString('foo~1bar')->toString()` should return `'foo/bar'`. Use PHPUnit assertions to validate pointer conversions and edge cases (e.g., malformed input).
- Does this package work with Laravel’s caching or file storage?
- Yes. Store JSON pointers as strings in caches (e.g., Redis) or files. Convert between formats (e.g., `toJsonString()` for caching) to ensure compatibility. The package’s immutable value objects prevent unintended modifications.