Skip to content

Throw exceptions if key path does not exist #22

@colinodell

Description

@colinodell

In order to avoid constant null-checking I would like the ability for this library to throw exceptions when trying to access something that does not exist. Specifically, I would like to change these behaviors:

// Fetching a non-existent key with no default given
$data->get('foo.bar'); // returns null

// Fetching a non-existent key with `null` set as the default
$data->get('foo.bar', null); // returns null

// Fetching a non-existent key with a non-`null` default
$data->get('foo.bar', 42); // returns 42

To this:

// Fetching a non-existent key with no default given
$data->get('foo.bar'); // CHANGE: would throw an exception instead of returning null

// Fetching a non-existent key with `null` set as the default
$data->get('foo.bar', null); // returns null

// Fetching a non-existent key with a non-`null` default
$data->get('foo.bar', 42); // returns 42

Obviously this will be a BC break and therefore require a bump in the major version. Anyone wanting the old functionality could set null as the second argument.

ArrayAccess

One outstanding question is what the behavior of offsetGet() should be - calling this indirectly like $data['foo.bar'] does not provide the ability to define a default value. I have no preference on whether this should return null or throw an exception.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions