ghostwriter/config

Provides an object that maps configuration keys to values.

0.4.1 2023-07-11 10:18 UTC

This package is auto-updated.

Last update: 2024-12-06 16:17:26 UTC


README

Compliance Supported PHP Version Mutation Coverage Code Coverage Type Coverage Latest Version on Packagist Downloads

Provides an object that maps configuration keys to values.

Installation

You can install the package via composer:

composer require ghostwriter/config

Star ⭐️ this repo if you find it useful

You can also star (🌟) this repo to find it easier later.

Usage

$key = 'nested';
$path = 'path/to/config.php';
$options = [
    'settings' => [
        'enable' => true,
    ],
];

$config = $configFactory->create($options);
$config->toArray(); // ['settings' => ['enable'=>true]]

$config = Config::fromPath($path);
$config->toArray(); // ['settings' => ['enable'=>true]]

$config = Config::fromPath($path, $key);
$config->toArray(); // ['nested' => ['settings' => ['enable'=>true]]]
$config->has('nested.settings.disabled'); // true

//

$config = new Config($options);
$config->has('settings'); // true
$config->has('settings.enable'); // true
$config->get('settings.enable'); // true

$config = Config::new($options);
$config->has('settings.disabled'); // false
$config->get('settings.disabled'); // null
$config->get('settings.disabled', 'default'); // 'default'

$config->set('settings.disabled', false); // true
$config->has('settings.disabled'); // true

$config->get('settings.disabled'); // false

$config->toArray(); // ['settings' => ['enable'=>true,'disabled'=>false]]

$config->remove('settings.disabled');

$config->get('settings.disabled'); // null

$config->toArray(); // ['settings' => ['enable'=>true]]

API

interface ConfigInterface
{
    public function get(string $key, mixed $default = null): mixed;

    public function has(string $key): bool;

    public function remove(string $key): void;

    public function set(string $key, mixed $value): void;

    public function toArray(): array;
}

Testing

composer test

Changelog

Please see CHANGELOG.md for more information what has changed recently.

Security

If you discover any security related issues, please email nathanael.esayeas@protonmail.com instead of using the issue tracker.

Support

[Become a GitHub Sponsor]

Credits

License

The BSD-3-Clause. Please see License File for more information.