Download the PHP package devcoder-xyz/php-options-resolver without Composer
On this page you can find all versions of the php package devcoder-xyz/php-options-resolver. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download devcoder-xyz/php-options-resolver
More information about devcoder-xyz/php-options-resolver
Files in devcoder-xyz/php-options-resolver
Package php-options-resolver
Short Description This library provides a simple solution for processing and validating option arrays in PHP.
License MIT
Informations about the package php-options-resolver
Option Array Processing and Validation Library
This library provides a simple solution for processing and validating option arrays in PHP.
Installation
To install this library, use Composer
Run the following Composer command:
Requirements
- PHP version 7.4 or higher
Defining Required Options
Define the required options for your class using OptionsResolver
with the expected options:
Defining Default Options
You can also set default values for your options using setDefaultValue
for each option:
Handling Non-existent Options
If a provided option does not exist in the defined list of options, an InvalidArgumentException
will be thrown:
Validating Option Values
You can add custom validators for each option to validate the provided values:
Certainly! Let's focus specifically on the use of Option::new()
to instantiate options in a fluent manner:
Instantiating Options with Option::new()
You can use Option::new()
to create and configure option instances in a fluent style before adding them to the OptionsResolver
. Here's an example that demonstrates this approach:
In this example:
- We use
Option::new('option1')
to create anOption
instance named'option1'
. - Similarly, we use
Option::new('option2')->setDefaultValue('default')
to create anOption
instance named'option2'
with a default value of'default'
. - Both options are then added to the
OptionsResolver
when it's instantiated. - Finally, we resolve the options by passing an array of values, and only
'option1'
is provided with a value ('value1'
).
Using Option::new()
provides a concise and clear way to create and configure option instances before resolving them with specific values.
This library is ideal for small projects where you need to manage and validate options in a simple and effective way.