PHP code example of devcoder-xyz / php-options-resolver

1. Go to this page and download the library: Download devcoder-xyz/php-options-resolver 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/ */

    

devcoder-xyz / php-options-resolver example snippets




use DevCoder\Resolver\Option;
use DevCoder\Resolver\OptionsResolver;

class Database
{
    public function __construct(array $options = [])
    {
        $resolver = new OptionsResolver([
            new Option('host'),
            new Option('username'),
            new Option('password'),
            new Option('dbname'),
        ]);

        try {
            $this->options = $resolver->resolve($options);
        } catch (InvalidArgumentException $e) {
            throw new InvalidArgumentException("Error: " . $e->getMessage());
        }
    }
}

// Example usage:
try {
    $database = new Database([
        'host' => 'localhost',
        'dbname' => 'app',
    ]);
} catch (InvalidArgumentException $e) {
    echo "Error: " . $e->getMessage(); // Displays: "Error: The 



class Database
{
    public function __construct(array $options = [])
    {
        $resolver = new OptionsResolver([
            (new Option('host'))->setDefaultValue('localhost'),
            (new Option('username'))->setDefaultValue('root'),
            (new Option('password'))->setDefaultValue('root'),
            (new Option('dbname'))->setDefaultValue('app'),
        ]);

        $this->options = $resolver->resolve($options);
    }
}

// Example usage:
$database = new Database([]);
var_dump($database->getOptions());
// Expected output:
// array(4) {
//     ["host"]=> string(9) "localhost"
//     ["username"]=> string(4) "root"
//     ["password"]=> string(4) "root"
//     ["dbname"]=> string(3) "app"
// }



class Database
{
    public function __construct(array $options = [])
    {
        $resolver = new OptionsResolver([
            (new Option('host'))->setDefaultValue('localhost'),
            (new Option('username'))->setDefaultValue('root'),
            (new Option('password'))->setDefaultValue('root'),
            (new Option('dbname'))->setDefaultValue('app'),
        ]);

        try {
            $this->options = $resolver->resolve($options);
        } catch (InvalidArgumentException $e) {
            throw new InvalidArgumentException("Error: " . $e->getMessage());
        }
    }
}

// Example usage:
try {
    $database = new Database([
        'url' => 'mysql://root:root@localhost/app',
    ]);
} catch (InvalidArgumentException $e) {
    echo "Error: " . $e->getMessage(); // Displays: "Error: The option(s) 'url' do(es) not exist. Defined options are: 'host', 'username', 'password', 'dbname'."
}



class Database
{
    public function __construct(array $options = [])
    {
        $resolver = new OptionsResolver([
            (new Option('host'))->validator(static function($value) {
                return is_string($value);
            })->setDefaultValue('localhost'),

            (new Option('username'))->validator(static function($value) {
                return is_string($value);
            })->setDefaultValue('root'),

            (new Option('password'))->validator(static function($value) {
                return is_string($value);
            })->setDefaultValue('root'),

            (new Option('dbname'))->validator(static function($value) {
                return is_string($value);
            })->setDefaultValue('app'),

            (new Option('driver'))->validator(static function($value) {
                return in_array($value, ['pdo_mysql', 'pdo_pgsql']);
            })->setDefaultValue('pdo_mysql'),
        ]);

        try {
            $this->options = $resolver->resolve($options);
        } catch (InvalidArgumentException $e) {
            throw new InvalidArgumentException("Error: " . $e->getMessage());
        }
    }
}

// Example usage with an invalid driver value:
try {
    $database = new Database([
        'host' => '192.168.1.200',
        'username' => 'root',
        'password' => 'root',
        'dbname' => 'my-app',
        'driver' => 'pdo_sqlite',
    ]);
} catch (InvalidArgumentException $e) {
    echo "Error: " . $e->getMessage(); // Displays: "Error: The option 'driver' with value 'pdo_sqlite' is invalid."
}



use DevCoder\Resolver\Option;
use DevCoder\Resolver\OptionsResolver;

// Create an option instance using Option::new()
$option1 = Option::new('option1');

// Create another option instance with a default value using Option::new()
$option2 = Option::new('option2')->setDefaultValue('default');

// Create a resolver and add the configured options
$resolver = new OptionsResolver([$option1, $option2]);

// Resolve the options with provided values
$options = $resolver->resolve([
    'option1' => 'value1',
]);