Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduces treverse method #17

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
269 changes: 88 additions & 181 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,181 +1,88 @@
# ItalyStrap Config API

[![Build status](https://github.com/ItalyStrap/config/actions/workflows/test.yml/badge.svg)](https://github.com/ItalyStrap/config/actions/workflows/test.yml?query=workflow%3Atest)
[![Latest Stable Version](https://img.shields.io/packagist/v/italystrap/config.svg)](https://packagist.org/packages/italystrap/config)
[![Total Downloads](https://img.shields.io/packagist/dt/italystrap/config.svg)](https://packagist.org/packages/italystrap/config)
[![Latest Unstable Version](https://img.shields.io/packagist/vpre/italystrap/config.svg)](https://packagist.org/packages/italystrap/config)
[![License](https://img.shields.io/packagist/l/italystrap/config.svg)](https://packagist.org/packages/italystrap/config)
![PHP from Packagist](https://img.shields.io/packagist/php-v/italystrap/config)

ItalyStrap Config Module - a simple and useful configuration package the OOP way

## Table Of Contents

* [Installation](#installation)
* [Basic Usage](#basic-usage)
* [Advanced Usage](#advanced-usage)
* [Contributing](#contributing)
* [License](#license)

## Installation

The best way to use this package is through Composer:

```CMD
composer require italystrap/config
```

## Basic Usage

This package is meant to be used in your application to manage configuration settings.
Values can be accessed using dot notation or array notation or a single key, and you can also set, update, delete, and check if a key exists.

Let's see a simple example:

```php
use ItalyStrap\Config\Config;

$config = new Config([
'key' => 'value',
'key2' => 'value2',
'key3' => [
'key4' => 'value4',
],
]);

$value = $config->get('key');

// Output: 'value'

// You can access using dot notation
$value = $config->get('key3.key4');

// Output: 'value4'

$value = $config->get('key3.key5', 'mixed default value');

// Output: 'mixed default value'

// You can also access using array notation
$value = $config->get(['key3', 'key4']);

// Output: 'value4'

// It is possible to set a value
$isSet = $config->set('key5', 'new value');

// Output: true

$value = $config->get('key5');

// Output: 'new value'

// You can also set a value using array notation or dot notation
$isSet = $config->set(['key6', 'key7'], 'new value');
// or
$isSet = $config->set('key6.key7', 'new value');

// Output: true

$value = $config->get('key6.key7');
// or
$value = $config->get(['key6', 'key7']);

// Output: 'new value'

// You can delete a value using dot notation or array notation or a single key

$isDeleted = $config->delete('key6.key7');
// or
$isDeleted = $config->delete(['key6', 'key7']);
// or
$isDeleted = $config->delete('key6');

// Output: true

// You can check if a key exists using dot notation or array notation or a single key

$exists = $config->has('key6.key7');
// or
$exists = $config->has(['key6', 'key7']);
// or
$exists = $config->has('key6');

// Output: true if exists, false otherwise

// You can update a value using dot notation or array notation or a single key
// It works in the same way as the set method

$isUpdated = $config->update('key', 'updated value');
// or
$isUpdated = $config->update(['key6', 'key7'], 'updated value');
// or
$isUpdated = $config->update('key6.key7', 'updated value');

// Output: true

// This package also has multiple methods to work with arrays like PSR-6

$config->getMultiple(['key', 'key2', 'key3.key4'], 'default');
$config->setMultiple(['key', 'key2', 'key3.key4'], 'value');
$config->deleteMultiple(['key', 'key2', 'key3.key4']);

// You can merge iterables after the Config object is created

$config->merge([
'key' => 'value',
'key2' => 'value2',
'key3' => [
'key4' => 'value4',
],
]);

// You can also get all the configuration settings as an array

$all = $config->toArray();

// Output: ['key' => 'value', 'key2' => 'value2', 'key3' => ['key4' => 'value4']]

// You can use the object and pass it to a \json_encode

$json = \json_encode($config);

// Output: '{"key":"value","key2":"value2","key3":{"key4":"value4"}}'

```

## Advanced Usage

You can see more advanced example in the tests' folder.

## Deprecation

List of all deprecated method that will be removed in the next major release.

* `Config::push()` => `Config::set()`
* `Config::add()` => `Config::set()`
* `Config::remove()` => `Config::delete()`
* `Config::all()` => `Config::toArray()`
* `ConfigThemeMods::class`

## Contributing

All feedback / bug reports / pull requests are welcome.

## License

Copyright (c) 2019 Enea Overclokk, ItalyStrap

This code is licensed under the [MIT](LICENSE).

## Credits

Ideas for the Config::class from:
- [Tonya Mork](https://github.com/wpfulcrum/config)
- [Alain Schlesser](https://github.com/brightnucleus/config)

For the Notation Array Search:
- https://github.com/balambasik/input/blob/master/src/Input.php

For some ideas:
- [ChatGPT](https://chat.openai.com)
# ItalyStrap Config API

[![Build status](https://github.com/ItalyStrap/config/actions/workflows/test.yml/badge.svg)](https://github.com/ItalyStrap/config/actions/workflows/test.yml?query=workflow%3Atest)
[![Latest Stable Version](https://img.shields.io/packagist/v/italystrap/config.svg)](https://packagist.org/packages/italystrap/config)
[![Total Downloads](https://img.shields.io/packagist/dt/italystrap/config.svg)](https://packagist.org/packages/italystrap/config)
[![Latest Unstable Version](https://img.shields.io/packagist/vpre/italystrap/config.svg)](https://packagist.org/packages/italystrap/config)
[![License](https://img.shields.io/packagist/l/italystrap/config.svg)](https://packagist.org/packages/italystrap/config)
![PHP from Packagist](https://img.shields.io/packagist/php-v/italystrap/config)

ItalyStrap Config Module - a simple and useful configuration package the OOP way

## Table Of Contents

* [Installation](#installation)
* [Basic Usage](#basic-usage)
* [Advanced Usage](#advanced-usage)
* [Contributing](#contributing)
* [License](#license)

## Installation

The best way to use this package is through Composer:

```CMD
composer require italystrap/config
```

## Documentation

* [Overview](docs/01_config.md)
* [Traversing Data](docs/02_traversing-data.md)

## Deprecation

List of all deprecated method that will be removed in the next major release (v3.x):

* `Config::push()` => `Config::set()`
* `Config::add()` => `Config::set()`
* `Config::remove()` => `Config::delete()`
* `Config::all()` => `Config::toArray()`
* `Config::toJson()` => (string)\json_encode(new Config(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
* `ConfigThemeMods::class` => No replacement class is provided
* `ConfigThemeModTest::class` => No replacement class is provided
* `Config_Factory::class` => `ConfigFactory::class`
* `Config_Interface::class` => `ConfigInterface::class`
* Move `\JsonSerializable` at the `ConfigInterface::class` level
* The second parameter of `Config::__construct()` is deprecated and will be removed in the next major release, the first parameter is now the default value used for the configuration, to add additional configuration you can use the `Config::merge()` method like in the example below:
```php
$defaultData = [
'key' => 'value',
];

$additionalData = [
'key' => 'new value',
];

$config = (new Config($defaultData))->merge($additionalData);
```
* The static method `ConfigFactory::make()` will be converted to a non-static, so you will need to instantiate the class to use it, like in the example below:
```php
$config = new ConfigFactory();
$config->make($data);
```

## Contributing

All feedback / bug reports / pull requests are welcome.

## License

Copyright (c) 2019 Enea Overclokk, ItalyStrap

This code is licensed under the [MIT](LICENSE).

## Credits

Ideas for the Config::class from:
- [Tonya Mork](https://github.com/wpfulcrum/config)
- [Alain Schlesser](https://github.com/brightnucleus/config)

For the Notation Array Search:
- https://github.com/balambasik/input/blob/master/src/Input.php

For the `traverse` method:
- [Nikita Popov](https://github.com/nikic/PHP-Parser)

For some ideas:
- [ChatGPT](https://chat.openai.com)
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.2",

"vimeo/psalm": "^4.26",
"humanmade/psalm-plugin-wordpress": "^2.0.3",

"phpbench/phpbench": "^1.2",
"phpmetrics/phpmetrics": "^2.8",
Expand All @@ -39,7 +38,8 @@
"infection/codeception-adapter": "^0.4.1",
"italystrap/debug": "^2.1",
"italystrap/event": "dev-master",
"italystrap/storage-tests": "dev-master"
"italystrap/storage-tests": "dev-master",
"cache/integration-tests": "^0.17.0"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -75,10 +75,10 @@
"@infection"
],
"cs": [
"@php vendor/bin/phpcs -p --ignore=./tests/_support/* ./src/ ./tests/"
"@php vendor/bin/phpcs"
],
"cs:fix": [
"@php vendor/bin/phpcbf -p --ignore=./tests/_support/* ./src/ ./tests/"
"@php vendor/bin/phpcbf"
],
"psalm": [
"@php vendor/bin/psalm --no-cache"
Expand Down Expand Up @@ -120,7 +120,6 @@
},
"config": {
"allow-plugins": {
"codeception/c3": true,
"composer/package-versions-deprecated": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"infection/extension-installer": true
Expand Down
Loading