Dot Access Data

repository·main·Indexed 20 days ago

https://github.com/dflydev/dflydev-dot-access-data

A PHP library for accessing and manipulating deeply nested data structures using dot (a.b.c) or slash (a/b/c) notation. It provides a Data class implementing ArrayAccess for setting, getting, appending, and removing values, as well as importing and exporting associative arrays. Requires PHP 7.1 or higher (version 1.0 for PHP 5.3+).

Tokens
2.3K
Snippets
16
Records
18
Agent score
72%

What's inside dflydev-dot-access-data

  1. Access deep data structures using dot notation

    main

    Dot Access Data allows you to interact with deep, nested data structures using dot notation (e.g., a.b.c) or slash notation (e.g., a/b/c) as path delimiters. It provides a Data class that can be initialized with an existing array and supports setting, getting, appending, and removing values at specific paths.

    use Dflydev\
    DotAccessData\
    Data;
    
    $data = new Data(['a' => ['b' => 'value']]);
    echo $data->get('a.b'); // "value"
  2. Use Data as an ArrayAccess object

    main

    The Data class implements PHP's ArrayAccess interface, allowing you to use standard array syntax for common operations:

    • $data['key'] calls offsetGet (which uses get())
    • $data['key'] = 'value' calls offsetSet (which uses set())
    • unset($data['key']) calls offsetUnset (which uses remove())
    • isset($data['key']) calls offsetExists (which uses has())
    $data['user.name'] = 'Alice'; // Uses set()
    $val = $data['user.name'];    // Uses get()
    unset($data['user.name']);  // Uses remove()
  3. Access Data as an array using ArrayAccess

    main

    The Data class implements the ArrayAccess interface, allowing you to use standard array syntax for common operations. This is useful for a more native PHP feel when working with the object.

    • Get: $data['key'] is equivalent to $data->get('key').
    • Set: $data['key'] = 'value' is equivalent to $data->set('key', 'value').
    • Check existence: isset($data['key']) is equivalent to $data->has('key').
    • Remove: unset($data['key']) removes the key.
    // Get
    $data->get('name') === $data['name'];
    
    // Set
    $data['name'] = 'Dewey'; // equivalent to $data->set('name', 'Dewey')
    
    // Check
    isset($data['name']) === $data->has('name');
    
    // Remove
    unset($data['name']);
  4. Use the Data class to manage nested data

    main

    The Data class is the primary entry point. You can use it to manipulate nested structures via dot or slash notation.

    Key Methods:

    • set(string $path, $value): Sets a value at the specified path. If the path contains intermediate keys, they are created as nested structures.
    • get(string $path, $default = null): Retrieves the value at the path. If the path does not exist, it returns the $default value. If no default is provided and the path is missing, it throws a MissingPathException.
    • append(string $path, $value): Appends a value to an existing array at the specified path.
    • has(string $path): Returns true if the path exists, false otherwise.
    • remove(string $path): Removes the data at the specified path.
    • getData(string $path): Returns a new DataInterface instance representing the sub-structure at the given path.
    use Dflydev\DotAccessData\Data;
    
    $data = new Data;
    
    $data->set('a.b.c', 'C');
    $data->append('a.b.d', 'D1');
    $data->set('a.b.e', ['E0', 'E1', 'E2']);
    
    // Accessing values
    $val = $data->get('a.b.c'); // 'C'
    $exists = $data->has('a.b.c'); // true
    
    // Using defaults for missing paths
    $missing = $data->get('non.existent', 'default_value'); // 'default_value'
  5. Set nested values using set()

    main

    Use set(string $key, $value = null) to assign a value to a specific path. If the intermediate keys in the path do not exist, they will be created as arrays. If an intermediate key exists but is not an array, a DataException is thrown.

    $data->set('settings.theme.color', 'blue');
  6. Append values to an array using append()

    main

    Use append(string $key, $value = null) to add a value to a list at a specific path. If the key does not exist or is not an array, append will promote the value to an array to accommodate the new entry.

    $data->append('logs.errors', 'Connection timeout');
    // If 'logs.errors' didn't exist, it is now ['Connection timeout']
  7. Import data using import() or importData()

    main

    You can merge new data into the existing structure using:

    • import(array $data, int $mode = self::REPLACE): Merges a plain associative array.
    • importData(DataInterface $data, int $mode = self::REPLACE): Merges another DataInterface instance.
    $data->import(['new_key' => 'new_value']);