1. Go to this page and download the library: Download code-distortion/options library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// the 'sendTweet' option is allowed because restrictUnexpected() was not called
$options = Options::new()
->defaults('sendEmails sendSms !sendSlack')
->options('sendTweet');
// InvalidOptionException: "The option "sendTweet" was not expected"
$options = Options::new()
->defaults('sendEmails sendSms !sendSlack')
->restrictUnexpected()
->options('sendTweet');
$validatorCallback = function (string $name, $value, bool $wasExpected): bool {
return is_bool($value); // ensure the value is a boolean
};
// sendEmails is ignored because it's not a boolean
Options::new()
->validator($validatorCallback)
->defaults('sendEmails sendSms !sendSlack')
->options('sendEmails=yes');
// InvalidOptionException: "The option "sendEmails" and/or it's value "yes" are not allowed"
Options::new()
->validator($validatorCallback, true) // <<<
->defaults('sendEmails sendSms !sendSlack')
->options('sendEmails=yes');
// instantiate
$options = new Options();
$options = new Options($myOptions);
$options = Options::new();
$options = Options::new($myOptions);
// call and chain any of these, in any order
$options->options($myOptions)
->amendOptions($extraOptions)
->defaults($defaults)
->amendDefaults($extraDefaults)
->restrictUnexpected()
->validator($validatorCallback);
// then consult your Options instance like normal
$results = $options->all();
$has = $options->has('sendEmails');
$value = $options->get('sendEmails');