devly / repository
PHP data repository object with array access using dot notation
Requires
- php: >=8.1
- ext-json: *
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^0.7.2
- devly/coding-standard: ^1.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.4
This package is auto-updated.
Last update: 2024-12-13 13:25:01 UTC
README
Provides an object to store and access data arrays with dot notation support.
Usage
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()
set()
$repository->set('user.name', 'johndoe'); // Or as array $repository['user.name'] = 'johndoe'; // Equivalent vanilla PHP $array['user']['name'] = 'johndoe';
get()
Retrieves data by key name
$username = $repository->get('user.name'); // Or as array $username = $repository['user.name']; // Equivalent vanilla PHP $username = $array['user']['name'];
has()
Check whether a key name exists
$repository->has('user.name'); // Or as array isset($repository['user.name']); // Equivalent vanilla PHP isset($array['user']['name']);
all()
Returns all the data as array
$array = $repository->all();
remove()
Removes given key
$repository->remove('user.name'); // Or as array unset($repository['user.name']);
clear()
Removes all the data
$repository->clear();
count()
Counts the number of items in the data container
$repository->count(); // Or using the count function count($repository);
isEmpty()
Checks whether the container contains data
$repository->isEmpty();
toJson()
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);
createFrom()
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()
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']
mergeRecursive()
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']