xety/configurator

A simple configuration class without dependencies that use the Fluent pattern.

v1.0.1 2017-06-02 21:50 UTC

This package is auto-updated.

Last update: 2024-11-05 00:32:14 UTC


README

A simple configuration class without dependencies that use the Fluent pattern.

Requirement

PHP

Installation

composer require xety/configurator

Usage

The Configurator class is an abstract class, so you just need to extends it:

<?php
namespace App;

use Xety\Configurator\Configurator;

class MyClass extends Configurator
{
}

If you want to setup a default configuration for your class, just do the following :

<?php
class MyClass extends Configurator
{
    protected $defaultConfig = [
        'key' => 'value',
        //etc
    ];

    public function __construct()
    {
        $this->setConfig($defaultConfig);
    }
}

Docs

Methods

Configurator::setConfig

public setConfig (array $values)

Description

Set the values to the options array. This function will replace all the configuration options.

Parameters

  • (array) $values : The values to push into the config.

Return Values

\Xety\Configurator\Configurator::class

Configurator::getConfig

public getConfig (void)

Description

Get all the options with their values.

Parameters

This function has no parameters.

Return Values

array

Configurator::flushConfig

public flushConfig (string ...$filter)

Description

Flush a list of options from the options array.

Usage:

$this->flushConfig('key1', 'key2', 'key3');

Parameters

  • (string) ...$filter : All the options to remove from the config.

Return Values

\Xety\Configurator\Configurator::class

Configurator::mergeConfig

public mergeConfig (array $values, bool $invert = false)

Description

Merge the values to the options array.

Parameters

  • (array) $values : The values to merge in the config.
  • (bool) $invert : Invert the merge by merging the actual config into the values.

Return Values

\Xety\Configurator\Configurator::class

Configurator::clearConfig

public clearConfig (void)

Description

Clear all options stored.

Parameters

This function has no parameters.

Return Values

\Xety\Configurator\Configurator::class

Configurator::setOption

public setOption (string $name, mixed $value)

Description

Set a value to the given option.

Usage:

$this->setOption('key', 'value');
$this->setOption('key', ['key2' => ['value2']]);

Parameters

  • (string) $name : The option name.
  • (mixed) $value : The option value.

Return Values

\Xety\Configurator\Configurator::class

Configurator::getOption

public getOption (string $name)

Description

Get an option value.

Usage:

$this->getOption('key');

Parameters

  • (string) $name : The option name to to get.

Return Values

mixed

Configurator::hasOption

public hasOption (string $name)

Description

Check if the option exist.

Parameters

  • (string) $name : The option name to check.

Return Values

bool

Configurator::flushOption

public flushOption (string $name)

Description

Flush an option.

Parameters

  • (string) $name : The name of the option to flush.

Return Values

\Xety\Configurator\Configurator::class

Configurator::pushOption

public pushOption (string $name, array $args)

Description

Push the listed args to the named option.

Usage:

$this->pushOption('key', ['key1' => 'value1'], ['key2' => ['value2' => 'value3']]);

Result:

'key' => [
    'key1' => 'value1',
    'key2' => [
        'value2' => 'value3'
    ]
]

Parameters

  • (string) $name : The name of the option.
  • (array) $args : A list of values to push into the option key.

Return Values

\Xety\Configurator\Configurator::class

Configurator::consumeOption

public consumeOption (string $name)

Description

Read then flush an option. Exemple:

Config:

$config = [
    'key1' => 'value1'
];

Usage:

$result = $this->consumeOption('key1');

Result:

echo $result; // value1
var_dump($config); // []

Parameters

  • (string) $name : The name of the option to read then flush.

Return Values

mixed

Configurator::transientOption

public transientOption (string $name, mixed $value)

Description

Adds a transient configuration key/value.

Usage:

// Will update the value of the key `key` if it exist,
// or it will create it with the value `value`.
 $this->transientOption('key', 'value');

Parameters

  • (string) $name : The name of the option.
  • (mixed) $value : The value to set.

Return Values

\Xety\Configurator\Configurator::class

Contribute

If you want to contribute, please follow this guide.