Skip to content

kyosenergy/configparser

Repository files navigation

banner

configparser

Latest Version on Packagist Software License CI Coverage Status Total Downloads

A simple YAML config file loader.

Install

Via Composer

$ composer require kyos/configparser

Usage

Given the following config.yaml configuration file:

application:
  releaseStage: Production
  debugMode: true

you can parse any of its properties using:

$parser = Kyos\ConfigParser::getParserForFile('config.yaml');
echo $parser->get('application.releaseStage');

You can use the following notations:

  1. Dot notation:
echo $parser->get('application.releaseStage');
  1. Array notation:
echo $parser->get(['application', 'releaseStage']);
  1. String notation (for top level keys):
var_dump($parser->get('application'));

Assertions

Using ConfigParser, you can require specific keys to be defined and their types. Note: All commands should be proceeded by an evaluate function call.

Required Key

echo $parser->evaluate('application.releaseStage')->isRequired();

String

echo $parser->evaluate('application.releaseStage')->isString();

Numeric

echo $parser->evaluate('application.releaseStage')->isNumeric();

Boolean

echo $parser->evaluate('application.releaseStage')->isBoolean();

Array

echo $parser->evaluate('application')->isArray();

Allowed Values

echo $parser->evaluate('application.releaseStage')->isOneOf(['Production', 'Staging', 'Test']);

Chain

Assertion functions can be also chained.

echo $parser->evaluate('application.releaseStage')
            ->isRequired()->isString()
            ->isOneOf(['Production', 'Staging', 'Test']);

Change log

Please see changelog for more information on what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.