enzyme/freckle

A collection of dot-based information retrieval helpers... wuh?

v0.3.0 2016-04-03 05:46 UTC

This package is not auto-updated.

Last update: 2024-12-07 20:22:43 UTC


README

Build Status Coverage Status Scrutinizer Code Quality

Freckle is a collection of information accessors. It allows you to traverse and get the values in arrays and other collection types using dot separated paths. For example, getting the value in a multi-dimensional-associative array for users.bob456.name, given the array:

$array = [
    'users' => [
        'jane123' => ['name' => 'Jane Foo'],
        'bob456'  => ['name' => 'Bob Foo'],
    ]
];

would return the value of Bob Foo. Pretty straight forward hey?

Installation

composer require enzyme/freckle

Usage

Getting a value from a simple collection.

use Enzyme\Freckle\Dot;

$array = [
    'users' => [
        'jane123' => ['name' => 'Jane Foo'],
        'bob456'  => ['name' => 'Bob Foo'],
    ]
];

$dot = new Dot;
$full_name = $dot->get($array, 'users.bob456.name'); // returns "Bob Foo".

Getting a value from a simple collection with numeric keys.

use Enzyme\Freckle\Dot;

$array = [
    'users' => [
        0 => [
            'jane123' => ['name' => 'Jane Foo'],
        ],
        1 => [
            'bob456'  => ['name' => 'Bob Foo'],
        ]
    ]
];

$dot = new Dot;
$full_name = $dot->get($array, 'users.bob456.name'); // returns "Bob Foo".

In the event that a collection has numeric keys or supports multiple entries with the same key name, only the first result found will ever be returned.

If no value can be found, null will be returned instead. Be careful though if you're checking against the null value for success as you may get false positives if an actual value was found, but that value happens to be null.

Contributing

Please see CONTRIBUTING.md

License

MIT - Copyright (c) 2015 Tristan Strathearn, see LICENSE