safeaccess / inline
Safe nested data access with dot notation for PHP.
Package info
github.com/felipesauer/safeaccess-inline
Language:TypeScript
pkg:composer/safeaccess/inline
Fund package maintenance!
Requires
- php: ^8.2
- ext-json: *
- ext-libxml: *
- ext-simplexml: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- infection/infection: ^0.32.6
- pestphp/pest: ^3.0
- phpstan/phpstan: ^2.0
Suggests
- ext-yaml: For improved YAML parsing performance (optional - a built-in minimal parser is used by default)
Provides
None
Conflicts
None
Replaces
None
README
Safe Access Inline
A query engine for untrusted external data — with security validation, advanced filtering, and immutable writes. PHP & TypeScript, identical API, zero production dependencies.
The problem
Reading nested data from external sources — API responses, uploaded files, config from third parties — requires more than null-safe access. You also need to defend against XXE in XML, anchor bombs in YAML, prototype pollution in JSON, PHP stream wrapper injection, and payload size abuse. Without a dedicated tool, that's boilerplate you write manually for every format, every endpoint.
Without this library (XML from an external API):
// PHP libxml_disable_entity_loader(true); $xml = simplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOENT); if ($xml === false) { throw new RuntimeException('Invalid XML'); } // validate keys against magic methods, superglobals, stream wrappers... // enforce depth and key count limits... $host = isset($xml->database->host) ? (string) $xml->database->host : null;
With this library:
$host = Inline::fromXml($input)->get('database.host'); // XXE blocked, forbidden keys validated, depth enforced — by default
The same applies to JSON, YAML, TOML, INI, ENV, NDJSON, and CSV/TSV. Every format is parsed with security validation enabled out of the box. No configuration required for safe defaults.
When to use this — and when not to
Use this library when your data comes from outside your application: API responses, user-uploaded files, third-party webhooks, config files you don't control.
Use ?? or data_get when your data is already trusted and internal. If you control the data source entirely and have no security concerns, native operators are simpler. This library is for the case where you don't.
What makes it different
Security by default. Every public entry point validates input before it reaches your code. Forbidden key blocking, payload size limits, and depth enforcement are on by default — not something you opt into.
A real query engine, not just dot notation. PathQuery supports wildcard expansion, recursive descent, slices, multi-index selection, projections, and filter expressions with comparisons, logical operators, string functions, and arithmetic — none of which exist natively in PHP or JavaScript.
Cross-language behavioral parity. The PHP and TypeScript packages expose the same API and produce identical output for the same input. One mental model, two runtimes.
Immutable writes. set(), remove(), and merge() return new instances. The original is never modified.
Zero production dependencies. No transitive risk.
Packages
| Package | Language | Install |
|---|---|---|
safeaccess/inline |
PHP 8.2+ | composer require safeaccess/inline |
@safeaccess/inline |
TypeScript (ESM) | npm install @safeaccess/inline |
Both packages expose the same public API surface and are tested for behavioral parity.
Installation
PHP
composer require safeaccess/inline
Requirements: PHP 8.2+, extensions: json, simplexml, libxml
Optional: ext-yaml for improved YAML parsing performance (a built-in minimal parser is used by default).
TypeScript
npm install @safeaccess/inline
Requirements: Node.js 22+
Quick start
PHP
use SafeAccess\Inline\Inline; $accessor = Inline::fromJson('{"user": {"name": "Alice", "age": 30}}'); $accessor->get('user.name'); // 'Alice' $accessor->get('user.email', 'N/A'); // 'N/A' (default when missing) $accessor->has('user.age'); // true $accessor->getOrFail('user.name'); // 'Alice' (throws if missing) // Immutable writes - original is never modified $updated = $accessor->set('user.email', 'alice@example.com'); $updated->get('user.email'); // 'alice@example.com' $accessor->has('user.email'); // false (original unchanged)
TypeScript
import { Inline } from '@safeaccess/inline'; const accessor = Inline.fromJson('{"user": {"name": "Alice", "age": 30}}'); accessor.get('user.name'); // 'Alice' accessor.get('user.email', 'N/A'); // 'N/A' (default when missing) accessor.has('user.age'); // true accessor.getOrFail('user.name'); // 'Alice' (throws if missing) // Immutable writes - original is never modified const updated = accessor.set('user.email', 'alice@example.com'); updated.get('user.email'); // 'alice@example.com' accessor.has('user.email'); // false (original unchanged)
Security
Safe Access Inline applies security validation by default on every public entry point. All keys pass through SecurityGuard and SecurityParser before being accessible.
What gets blocked
PHP (packages/php)
| Category | Examples | Reason |
|---|---|---|
| PHP magic methods | __construct, __destruct, __wakeup, __sleep, __toString, __call, __get, __set, __invoke, ... |
Prevent triggering PHP magic behavior via data keys |
| Prototype pollution | __proto__, constructor, prototype |
Prevent prototype pollution attacks |
| PHP superglobals | GLOBALS, _GET, _POST, _COOKIE, _REQUEST, _SERVER, _ENV, _FILES, _SESSION |
Prevent superglobal variable access |
| Stream wrapper URIs | php://input, php://filter, phar://..., data://..., file://..., ... |
Prevent stream wrapper injection |
TypeScript (packages/js)
| Category | Examples | Reason |
|---|---|---|
| Prototype pollution | __proto__, constructor, prototype |
Prevent prototype pollution attacks |
| Legacy prototype manipulation | __defineGetter__, __defineSetter__, __lookupGetter__, __lookupSetter__ |
Prevent legacy prototype tampering |
| Property shadow | hasOwnProperty |
Overriding it can bypass guard checks |
| Node.js globals | __dirname, __filename |
Prevent path-injection via dynamic property access |
| Protocol / stream URIs | javascript:, blob:, ws://, wss://, node:, file://, http://, https://, ftp://, data:, ... |
Prevent URI injection and XSS vectors |
Keys starting with __ are matched case-insensitively. Stream wrapper URIs and protocol schemes are matched by prefix.
Per-runtime design: The PHP and TypeScript packages intentionally block different keys because each package only guards against threats relevant to its own runtime. PHP magic methods and superglobals have no meaning in a JavaScript runtime;
__dirname,__filename, and browser protocol schemes (javascript:,blob:,ws://) have no equivalent PHP attack surface.
Format-specific protections
| Format | Protection |
|---|---|
| XML | Rejects <!DOCTYPE — prevents XXE (XML External Entity) attacks |
| YAML | Blocks unsafe tags, anchors (&), aliases (*), and merge keys (<<) |
| TOML | Rejects duplicate keys and redefined tables; enforces nesting depth |
| All | Forbidden key validation on every parsed key |
Structural limits
| Limit | Default | Description |
|---|---|---|
maxPayloadBytes |
10 MB (10,485,760) | Maximum raw string input size |
maxKeys |
10,000 | Maximum total key count across the entire structure |
maxDepth |
512 | Maximum structural nesting depth |
maxResolveDepth |
100 | Maximum recursion for path resolution and deep merge |
maxCountRecursiveDepth |
100 | Maximum recursion when counting keys |
Custom forbidden keys
// PHP $guard = new SecurityGuard(extraForbiddenKeys: ['secret', 'internal_token']); $accessor = Inline::withSecurityGuard($guard)->fromJson($data);
// TypeScript const guard = new SecurityGuard(512, ['secret', 'internal_token']); const accessor = Inline.withSecurityGuard(guard).fromJson(data);
Disabling validation for trusted input
// PHP $accessor = Inline::withStrictMode(false)->fromJson($trustedPayload);
// TypeScript const accessor = Inline.withStrictMode(false).fromJson(trustedPayload);
Warning: Disabling strict mode skips all validation — forbidden keys, payload size, depth and key-count limits. Only use with application-controlled input.
For vulnerability reports, see SECURITY.md.
PathQuery — advanced querying
PathQuery goes well beyond dot notation. It is the part of this library with no native equivalent in PHP or JavaScript.
Basic dot notation
| Syntax | Example | Description |
|---|---|---|
key.key |
user.name |
Nested key access |
key.0.key |
users.0.name |
Numeric key (array index) |
key\.with\.dots |
config\.db\.host |
Escaped dots in key names |
$ or $.path |
$.user.name |
Optional $ root prefix (stripped) |
Full PathQuery syntax
| Syntax | Example | Description |
|---|---|---|
[0] |
users[0] |
Bracket index access |
* or [*] |
users.* |
Wildcard — expand all children |
..key |
..name |
Recursive descent — find key at any depth |
..['a','b'] |
..['name','age'] |
Multi-key recursive descent |
[0,1,2] |
users[0,1,2] |
Multi-index — select multiple indices |
['a','b'] |
['name','age'] |
Multi-key — select multiple keys |
[0:5] |
items[0:5] |
Slice — indices 0 through 4 |
[::2] |
items[::2] |
Slice with step — every 2nd item |
[::-1] |
items[::-1] |
Reverse slice |
[?expr] |
users[?age>18] |
Filter predicate expression |
.{fields} |
.{name, age} |
Projection — select fields |
.{alias: src} |
.{fullName: name} |
Aliased projection |
Filter expressions
// PHP $data = Inline::fromJson('[ {"name": "Alice", "age": 25, "role": "admin"}, {"name": "Bob", "age": 17, "role": "user"}, {"name": "Carol", "age": 30, "role": "admin"} ]'); // Comparison: ==, !=, >, <, >=, <= $data->get('[?age>18]'); // Alice and Carol // Logical: && and || $data->get('[?age>18 && role==\'admin\']'); // Alice and Carol // Built-in functions: starts_with, contains, values $data->get('[?starts_with(@.name, \'A\')]'); // Alice $data->get('[?contains(@.name, \'ob\')]'); // Bob // Arithmetic in predicates: +, -, *, / $orders = Inline::fromJson('[{"price": 10, "qty": 5}, {"price": 3, "qty": 2}]'); $orders->get('[?@.price * @.qty > 20]'); // first order only
// TypeScript const data = Inline.fromJson(`[ {"name": "Alice", "age": 25, "role": "admin"}, {"name": "Bob", "age": 17, "role": "user"}, {"name": "Carol", "age": 30, "role": "admin"} ]`); // Comparison: ==, !=, >, <, >=, <= data.get('[?age>18]'); // Alice and Carol // Logical: && and || data.get('[?age>18 && role=="admin"]'); // Alice and Carol // Built-in functions: starts_with, contains, values data.get('[?starts_with(@.name, "A")]'); // Alice data.get('[?contains(@.name, "ob")]'); // Bob // Arithmetic in predicates: +, -, *, / const orders = Inline.fromJson('[{"price": 10, "qty": 5}, {"price": 3, "qty": 2}]'); orders.get('[?@.price * @.qty > 20]'); // first order only
Supported formats
Each format has a dedicated accessor with automatic parsing and security validation applied on load.
JSON
// PHP $accessor = Inline::fromJson('{"users": [{"name": "Alice"}, {"name": "Bob"}]}'); $accessor->get('users.0.name'); // 'Alice'
// TypeScript const accessor = Inline.fromJson('{"users": [{"name": "Alice"}, {"name": "Bob"}]}'); accessor.get('users.0.name'); // 'Alice'
YAML
// PHP $yaml = <<<YAML database: host: localhost port: 5432 credentials: user: admin YAML; $accessor = Inline::fromYaml($yaml); $accessor->get('database.credentials.user'); // 'admin'
// TypeScript const yaml = `database: host: localhost port: 5432 credentials: user: admin`; const accessor = Inline.fromYaml(yaml); accessor.get('database.credentials.user'); // 'admin'
TOML
// PHP $toml = <<<TOML [database] host = "localhost" port = 5432 [[servers]] name = "alpha" [[servers]] name = "beta" TOML; $accessor = Inline::fromToml($toml); $accessor->get('database.host'); // 'localhost' $accessor->get('servers.1.name'); // 'beta'
// TypeScript const toml = `[database] host = "localhost" port = 5432 [[servers]] name = "alpha" [[servers]] name = "beta"`; const accessor = Inline.fromToml(toml); accessor.get('database.host'); // 'localhost' accessor.get('servers.1.name'); // 'beta'
Supports tables, arrays of tables, dotted keys, inline arrays and tables, and the scalar types (integer, float, boolean, string). Duplicate keys and redefined tables are rejected; datetimes are preserved as strings.
XML
// PHP $xml = '<config><database><host>localhost</host><port>5432</port></database></config>'; $accessor = Inline::fromXml($xml); $accessor->get('database.host'); // 'localhost' // Also accepts SimpleXMLElement $accessor = Inline::fromXml(simplexml_load_string($xml));
// TypeScript const accessor = Inline.fromXml('<config><database><host>localhost</host></database></config>'); accessor.get('database.host'); // 'localhost'
INI
// PHP $accessor = Inline::fromIni("[database]\nhost=localhost\nport=5432"); $accessor->get('database.host'); // 'localhost'
// TypeScript const accessor = Inline.fromIni('[database]\nhost=localhost\nport=5432'); accessor.get('database.host'); // 'localhost'
ENV (dotenv)
// PHP $accessor = Inline::fromEnv("APP_NAME=MyApp\nAPP_DEBUG=true\nDB_HOST=localhost"); $accessor->get('DB_HOST'); // 'localhost'
// TypeScript const accessor = Inline.fromEnv('APP_NAME=MyApp\nAPP_DEBUG=true\nDB_HOST=localhost'); accessor.get('DB_HOST'); // 'localhost'
NDJSON (Newline-Delimited JSON)
Each line is parsed as an independent JSON object and indexed from 0 by its position in the input. Blank lines and trailing newlines are skipped. Security validation is applied to each line individually.
// PHP $ndjson = '{"id":1,"name":"Alice"}' . "\n" . '{"id":2,"name":"Bob"}'; $accessor = Inline::fromNdjson($ndjson); $accessor->get('0.name'); // 'Alice' $accessor->get('1.name'); // 'Bob'
// TypeScript const accessor = Inline.fromNdjson('{"id":1,"name":"Alice"}\n{"id":2,"name":"Bob"}'); accessor.get('0.name'); // 'Alice' accessor.get('1.name'); // 'Bob'
CSV / TSV
The first row is the header; each subsequent row becomes a record indexed from 0 by position, keyed by the header columns. All values are strings — CSV has no type system, so 007 stays "007". Quoted fields may contain the delimiter, escaped quotes (""), and embedded newlines. Duplicate header columns, rows whose field count differs from the header, and unterminated quotes are rejected. Header columns are validated against forbidden keys.
// PHP $csv = "name,age,city\nAlice,30,Porto\nBob,25,Lisbon"; $accessor = Inline::fromCsv($csv); $accessor->get('0.name'); // 'Alice' $accessor->get('1.city'); // 'Lisbon' // Tab-separated $accessor = Inline::fromTsv("name\tage\nAlice\t30"); $accessor->get('0.age'); // '30'
// TypeScript const accessor = Inline.fromCsv('name,age,city\nAlice,30,Porto\nBob,25,Lisbon'); accessor.get('0.name'); // 'Alice' accessor.get('1.city'); // 'Lisbon' const tsv = Inline.fromTsv('name\tage\nAlice\t30'); tsv.get('0.age'); // '30'
Array / Object
// PHP $accessor = Inline::fromArray(['users' => [['name' => 'Alice'], ['name' => 'Bob']]]); $accessor->get('users.0.name'); // 'Alice' $accessor = Inline::fromObject((object) ['name' => 'Alice', 'age' => 30]); $accessor->get('name'); // 'Alice'
// TypeScript const accessor = Inline.fromArray({ users: [{ name: 'Alice' }, { name: 'Bob' }] }); accessor.get('users.0.name'); // 'Alice' const objAccessor = Inline.fromObject({ name: 'Alice', age: 30 }); objAccessor.get('name'); // 'Alice'
Any (custom format via integration)
// PHP - requires implementing ParseIntegrationInterface $accessor = Inline::withParserIntegration(new MyCsvIntegration())->fromAny($csvString); $accessor->get('0.column_name');
// TypeScript - requires implementing ParseIntegrationInterface const accessor = Inline.withParserIntegration(new MyCsvIntegration()).fromAny(csvString); accessor.get('0.column_name');
Dynamic (by TypeFormat enum)
// PHP use SafeAccess\Inline\Enums\TypeFormat; $accessor = Inline::from(TypeFormat::Json, '{"key": "value"}'); $accessor->get('key'); // 'value'
// TypeScript import { Inline, TypeFormat } from '@safeaccess/inline'; const accessor = Inline.from(TypeFormat.Json, '{"key": "value"}'); accessor.get('key'); // 'value'
Reading & writing
All accessor methods are identical in PHP and TypeScript.
PHP
$accessor = Inline::fromJson('{"a": {"b": 1, "c": 2}}'); // Read $accessor->get('a.b'); // 1 $accessor->get('a.missing', 'default'); // 'default' $accessor->getOrFail('a.b'); // 1 (throws PathNotFoundException if missing) $accessor->has('a.b'); // true $accessor->all(); // ['a' => ['b' => 1, 'c' => 2]] $accessor->count(); // 1 (root keys) $accessor->count('a'); // 2 (keys under 'a') $accessor->keys(); // ['a'] $accessor->keys('a'); // ['b', 'c'] $accessor->getMany([ 'a.b' => null, 'a.x' => 'fallback', ]); // ['a.b' => 1, 'a.x' => 'fallback'] $accessor->getRaw(); // original JSON string // Write (immutable - every write returns a new instance) $updated = $accessor->set('a.d', 3); $updated = $updated->remove('a.c'); $updated = $updated->merge('a', ['e' => 4]); $updated = $updated->mergeAll(['f' => 5]); $updated->all(); // ['a' => ['b' => 1, 'd' => 3, 'e' => 4], 'f' => 5] // Readonly mode - block all writes $readonly = $accessor->readonly(); $readonly->get('a.b'); // 1 (reads work) $readonly->set('a.b', 99); // throws ReadonlyViolationException
TypeScript
const accessor = Inline.fromJson('{"a": {"b": 1, "c": 2}}'); // Read accessor.get('a.b'); // 1 accessor.get('a.missing', 'default'); // 'default' accessor.getOrFail('a.b'); // 1 (throws PathNotFoundException if missing) accessor.has('a.b'); // true accessor.all(); // { a: { b: 1, c: 2 } } accessor.count(); // 1 accessor.count('a'); // 2 accessor.keys(); // ['a'] accessor.keys('a'); // ['b', 'c'] accessor.getMany({ 'a.b': null, 'a.x': 'fallback', }); // { 'a.b': 1, 'a.x': 'fallback' } accessor.getRaw(); // original JSON string // Write (immutable) const updated = accessor.set('a.d', 3).remove('a.c').merge('a', { e: 4 }).mergeAll({ f: 5 }); updated.all(); // { a: { b: 1, d: 3, e: 4 }, f: 5 } // Readonly mode const readonly = accessor.readonly(); readonly.get('a.b'); // 1 readonly.set('a.b', 99); // throws ReadonlyViolationException
Schema validation
Beyond security, you can validate the shape of the data: which paths must exist, their type, and additional constraints. A schema maps dot-notation paths to pipe-separated rules. The first part is the base type — string, int, float, number, bool, array, object, null, any — and a trailing ? marks the path optional. Validation runs against the already-parsed value, so a CSV or TOML string field checked as int fails (those formats carry no numeric type).
Additional parts add constraints, applied once the type matches:
| Constraint | Applies to | Meaning |
|---|---|---|
min:N |
number | value >= N |
min:N |
string / array | length / size >= N |
max:N |
number / string / array | value or length / size <= N |
enum:a,b,c |
string / number | value must be one of the listed options |
pattern:REGEX |
string | must match the (unanchored) regular expression |
email |
string | must look like an email address |
url |
string | must be an http(s) URL |
uuid |
string | must be a UUID |
each:(RULE) |
array | every element must satisfy RULE (may nest) |
'db.port' => 'int|min:1|max:65535'
'user.email' => 'string|email'
'status' => 'string|enum:active,inactive,pending'
'code' => 'string|pattern:^[A-Z]{3}$'
'nickname' => 'string|min:2|max:20?' // optional; validated when present
'scores' => 'array|min:1|each:(int|min:0)' // every element validated
'emails' => 'array|each:(string|email)'
A path may contain * wildcards to apply a rule to every matching element. 'users.*.email' => 'string|email' validates the email of every user; failures report the expansion index (users.*.email.2). An absent or empty collection matches nothing and passes — require it separately with a concrete rule like 'users' => 'array|min:1'.
validate() returns a result you can inspect; assert() throws SchemaValidationException on failure and returns the accessor for chaining. A malformed rule (bad min argument, empty enum, invalid regex, unknown constraint) throws AccessorException — a mistake in the schema, not the data.
PHP
$accessor = Inline::fromJson('{"db":{"host":"localhost","port":5432}}'); // Inspect without throwing $result = $accessor->validate([ 'db.host' => 'string|min:1', 'db.port' => 'int|min:1|max:65535', 'db.ssl' => 'bool?', // optional ]); $result->isValid(); // true $result->errors(); // [] (array of SchemaError when invalid) // Assert and chain (throws SchemaValidationException on failure) $host = $accessor->assert(['db.host' => 'string'])->get('db.host');
TypeScript
const accessor = Inline.fromJson('{"db":{"host":"localhost","port":5432}}'); // Inspect without throwing const result = accessor.validate({ 'db.host': 'string', 'db.port': 'int', 'db.ssl': 'bool?', // optional }); result.valid; // true result.errors; // [] (SchemaError[] when invalid) // Assert and chain (throws SchemaValidationException on failure) const host = accessor.assert({ 'db.host': 'string' }).get('db.host');
Each SchemaError carries path, expected (the rule), actual (the type found, or missing), and a human-readable message. For per-field reporting (forms, API responses), result.errorsByPath() groups the messages into a map keyed by path. An unrecognised rule throws AccessorException — that's a mistake in the schema, not the data.
Configure
Customize security, caching, and parsing via the builder pattern:
PHP
use SafeAccess\Inline\Inline; use SafeAccess\Inline\Security\SecurityGuard; use SafeAccess\Inline\Security\SecurityParser; $accessor = Inline::withSecurityGuard(new SecurityGuard(extraForbiddenKeys: ['secret'])) ->withSecurityParser(new SecurityParser(maxDepth: 5)) ->withStrictMode(true) ->fromJson($untrustedInput);
TypeScript
import { Inline, SecurityGuard, SecurityParser } from '@safeaccess/inline'; const accessor = Inline.withSecurityGuard(new SecurityGuard(512, ['secret'])) .withSecurityParser(new SecurityParser({ maxDepth: 5 })) .withStrictMode(true) .fromJson(untrustedInput);
Builder methods
| Method | Description |
|---|---|
withSecurityGuard(guard) |
Custom forbidden-key rules and depth limits |
withSecurityParser(parser) |
Custom payload size and structural limits |
withPathCache(cache) |
Path segment cache for repeated lookups |
withParserIntegration(integration) |
Custom format parser for fromAny() |
withStrictMode(false) |
Disable security validation (trusted input only) |
Error handling
All exceptions extend AccessorException, making it easy to catch every library error in a single block.
PHP
use SafeAccess\Inline\Inline; use SafeAccess\Inline\Exceptions\AccessorException; use SafeAccess\Inline\Exceptions\InvalidFormatException; use SafeAccess\Inline\Exceptions\SecurityException; use SafeAccess\Inline\Exceptions\PathNotFoundException; use SafeAccess\Inline\Exceptions\ReadonlyViolationException; try { $accessor = Inline::fromJson($untrustedInput); $value = $accessor->getOrFail('config.key'); } catch (InvalidFormatException $e) { // Malformed JSON, XML, INI, or NDJSON input } catch (SecurityException $e) { // Forbidden key, payload too large, depth/key-count exceeded } catch (PathNotFoundException $e) { // Path does not exist in the data } catch (ReadonlyViolationException $e) { // Write attempted on a readonly accessor } catch (AccessorException $e) { // Catch-all for any SafeAccess error }
TypeScript
import { Inline, AccessorException, InvalidFormatException, SecurityException, PathNotFoundException, ReadonlyViolationException, } from '@safeaccess/inline'; try { const accessor = Inline.fromJson(untrustedInput); const value = accessor.getOrFail('config.key'); } catch (e) { if (e instanceof InvalidFormatException) { /* malformed input */ } if (e instanceof SecurityException) { /* security violation */ } if (e instanceof PathNotFoundException) { /* path not found */ } if (e instanceof ReadonlyViolationException) { /* readonly violation */ } if (e instanceof AccessorException) { /* any library error */ } }
Exception hierarchy
| Exception | Extends | When |
|---|---|---|
AccessorException |
RuntimeException / Error |
Root — catch-all for any library error |
SecurityException |
AccessorException |
Forbidden key, payload too large, structural limits exceeded |
InvalidFormatException |
AccessorException |
Malformed JSON, XML, INI, NDJSON |
YamlParseException |
InvalidFormatException |
Unsafe or malformed YAML |
TomlParseException |
InvalidFormatException |
Malformed TOML, duplicate keys, or redefined tables |
CsvParseException |
InvalidFormatException |
Malformed CSV/TSV, duplicate columns, or field-count mismatch |
SchemaValidationException |
AccessorException |
assert() when data fails the schema |
PathNotFoundException |
AccessorException |
getOrFail() on a missing path |
ReadonlyViolationException |
AccessorException |
Write on a readonly accessor |
UnsupportedTypeException |
AccessorException |
Unknown accessor class in make() |
ParserException |
AccessorException |
Reserved for custom parser-level errors |
Advanced usage
Path cache
Cache parsed path segments for repeated lookups:
// PHP - implement PathCacheInterface $cache = new MyPathCache(); $accessor = Inline::withPathCache($cache)->fromJson($data); $accessor->get('deeply.nested.path'); // parses path $accessor->get('deeply.nested.path'); // cache hit - skips parsing
// TypeScript - implement PathCacheInterface const cacheMap = new Map(); const cache: PathCacheInterface = { get: (path) => cacheMap.get(path) ?? null, set: (path, segments) => { cacheMap.set(path, segments); }, has: (path) => cacheMap.has(path), clear: () => { cacheMap.clear(); return cache; }, }; const accessor = Inline.withPathCache(cache).fromJson(data);
Custom format integration
Add support for custom data formats by implementing ParseIntegrationInterface:
// PHP class CsvIntegration implements ParseIntegrationInterface { public function assertFormat(mixed $raw): bool { return is_string($raw) && str_contains($raw, ','); } public function parse(mixed $raw): array { // Parse CSV to associative array return $parsed; } } $accessor = Inline::withParserIntegration(new CsvIntegration())->fromAny($csvString);
// TypeScript const csvIntegration: ParseIntegrationInterface = { assertFormat: (raw: unknown) => typeof raw === 'string' && (raw as string).includes(','), parse: (raw: unknown) => { // Parse CSV to object return parsed; }, }; const accessor = Inline.withParserIntegration(csvIntegration).fromAny(csvString);
API reference
Inline facade
Static factory methods
| Method | Input | Returns |
|---|---|---|
fromArray(data) |
Array / plain object | ArrayAccessor |
fromObject(data) |
Object | ObjectAccessor |
fromJson(data) |
JSON string |
JsonAccessor |
fromXml(data) |
XML string or SimpleXMLElement |
XmlAccessor |
fromYaml(data) |
YAML string |
YamlAccessor |
fromToml(data) |
TOML string |
TomlAccessor |
fromIni(data) |
INI string |
IniAccessor |
fromEnv(data) |
dotenv string |
EnvAccessor |
fromNdjson(data) |
NDJSON string |
NdjsonAccessor |
fromCsv(data) |
CSV string |
CsvAccessor |
fromTsv(data) |
TSV string |
TsvAccessor |
fromAny(data, integration?) |
Any format | AnyAccessor |
from(typeFormat, data) |
TypeFormat enum |
AccessorsInterface |
make(accessorClass, data) |
Accessor class | AbstractAccessor |
Accessor read methods
| Method | Returns |
|---|---|
get(path, default?) |
Value at path, or default |
getOrFail(path) |
Value or throws PathNotFoundException |
getAt(segments, default?) |
Value at key segments |
has(path) |
boolean |
hasAt(segments) |
boolean |
getMany(paths) |
Record<string, unknown> |
all() |
All parsed data |
count(path?) |
Element count |
keys(path?) |
Key names |
getRaw() |
Original input |
Accessor validation methods
| Method | Returns |
|---|---|
validate(schema) |
SchemaResult (.valid / .errors / .errorsByPath()), never throws |
assert(schema) |
this when valid; throws SchemaValidationException |
Accessor write methods (immutable)
| Method | Description |
|---|---|
set(path, value) |
Set at path |
setAt(segments, value) |
Set at key segments |
remove(path) |
Remove at path |
removeAt(segments) |
Remove at key segments |
merge(path, value) |
Deep-merge at path |
mergeAll(value) |
Deep-merge at root |
Modifier methods
| Method | Description |
|---|---|
readonly(flag?) |
Block all writes |
strict(flag?) |
Toggle security validation |
TypeFormat enum
Array · Object · Json · Xml · Yaml · Toml · Ini · Env · Ndjson · Csv · Tsv · Any
Comparison
| Feature | Safe Access Inline | lodash.get |
Laravel data_get |
jmespath |
|---|---|---|---|---|
| Language | PHP + TypeScript | JavaScript | PHP | Multi |
| Security validation | Built-in, on by default | None | None | None |
| Forbidden key blocking | Yes | No | No | No |
| Payload size limits | Yes | No | No | No |
| Immutable writes | Yes | No | No | N/A |
| Readonly mode | Yes | No | No | N/A |
| Multi-format support | 9 formats | Object only | Array only | Object only |
| Custom format integration | Yes | No | No | No |
| Wildcard / filter / slice | Yes | No | Partial (*) |
Yes |
| Zero prod dependencies | Yes | lodash | Laravel | jmespath |
| Cross-language parity | PHP + TypeScript | JavaScript only | PHP only | Multi |
Project structure
packages/php/ - PHP package (PSR-4, Pest, PHPStan, Infection)
packages/js/ - TypeScript package (ESM, Vitest, Stryker)
Contributing
See CONTRIBUTING.md for development setup, commit conventions, and pull request guidelines.
License
MIT © Felipe Sauer