Provides an object to store and access data arrays with dot notation support.
A new data container instance can be created empty or from data provided as array, object or another Devly\Repository
instance.
$repository = new \Devly\Repository();
// Or create with initial data
$repository = new \Devly\Repository(['user' => [...]]);
Can also be created using the static method Devly\Repository::new()
$repository->set('user.name', 'johndoe');
// Or as array
$repository['user.name'] = 'johndoe';
// Equivalent vanilla PHP
$array['user']['name'] = 'johndoe';
Retrieves data by key name
$username = $repository->get('user.name');
// Or as array
$username = $repository['user.name'];
// Equivalent vanilla PHP
$username = $array['user']['name'];
Check whether a key name exists
$repository->has('user.name');
// Or as array
isset($repository['user.name']);
// Equivalent vanilla PHP
isset($array['user']['name']);
Returns all the data as array
$array = $repository->all();
Removes given key
$repository->remove('user.name');
// Or as array
unset($repository['user.name']);
Removes all the data
$repository->clear();
Counts the number of items in the data container
$repository->count();
// Or using the count function
count($repository);
Checks whether the container contains data
$repository->isEmpty();
Returns a JSON representation of the data
Returns the value of a given key as JSON:
$repository->toJson('user');
Returns all the stored items as JSON:
$repository->toJson();
// Same as:
json_encode($repository);
Create a new Repository instance from an existing item
$repository = new Repository(['name' => ['first' => 'John', 'last' => 'Doe']]);
$repository2 = $repository->createFrom('name');
$repository2->all(); // ['first' => 'John', 'last' => 'Doe']
Merge array, object or an instance of IRepository into the current Repository
$repository = new Repository(['first' => 'John', 'last' => 'Doe']);
$repository->merge(new Repository(['first' => 'Johnny']));
// Can also be merged with an object
$repository->merge((object) ['first' => 'Johnny']);
// Can also be merged with an object
$repository->merge(['first' => 'Johnny']);
$repository->all(); // ['first' => 'Johnny', 'last' => 'Doe']
Merge array, object or an instance of IRepository into the current Repository recursively
$repository = new Repository(['name' => ['first' => 'John', 'last' => 'Doe']]);
$repository->mergeRecursive(['name' => ['first' => 'Johnny']]);
$repository->all(); // ['name' => ['first' => 'Johnny', 'last' => 'Doe']