Download the PHP package kuria/options without Composer

On this page you can find all versions of the php package kuria/options. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package options

Options #######

Resolve structured arrays (e.g. configuration) according to the specified set of options.

:depth: 4

Features

Requirements

Usage

Resolving options

Use Resolver to resolve arrays according to the specified options.

The resolve() method returns an instance of Node, which can be accessed as an array. See Working with Node instances.

If the passed value is invalid, ResolverException will be thrown. See Handling validation errors.

// create a resolver $resolver = new Resolver();

// define options $resolver->addOption( Option::string('path'), Option::int('interval')->default(null) );

// resolve an array $node = $resolver->resolve([ 'path' => 'file.txt', ]);

var_dump($node['path'], $node['interval']);

Output:

string(8) "file.txt"
NULL

Working with Node instances

By default, Resolver->resolve() returns a Node instance with the resolved options.

Resolver context

Resolver->resolve() accepts a second argument, which may be an array of additional arguments to pass to all validators, normalizers and lazy default closures. The values may be of any type.

$resolver = new Resolver();

$resolver->addOption( Option::string('option') ->normalize(function (string $value, $foo, $bar) { echo 'NORMALIZE: ', $foo, ', ', $bar, "\n";

return $value;

lidate(function (string $value, $foo, $bar) { echo 'VALIDATE: ', $foo, ', ', $bar, "\n";

string('optionWithLazyDefault') fault(function (Node $node, $foo, $bar) { echo 'DEFAULT: ', $foo, ', ', $bar, "\n";

return 'value';

);

$options = $resolver->resolve( ['option' => 'value'], ['context argument 1', 'context argument 2'] )->toArray();

Output:

NORMALIZE: context argument 1, context argument 2
VALIDATE: context argument 1, context argument 2
DEFAULT: context argument 1, context argument 2

Defining options

Terminology

leaf option An option in the option tree that does not contain children.

node option An option defined via Option::node() or Option::nodeList(). They are branches in the option tree.

child option Any option nested inside a node option. It can be either leaf or a node option.

Option factories

The Option class provides a number of static factories to create option instances.

FactoryDescription
Option::any($name) Mixed option that accepts all value types. NULL is accepted only if the option is nullable.

Option::bool($name) Boolean option.

Option::int($name) Integer option.

Option::float($name) Float option.

Option::number($name) Number option that accepts integers and floats.

Option::numeric($name) Numeric option that accepts integers, floats and numeric strings.

Option::string($name) String option.

Option::array($name) Array option. The individual values are not validated.

Option::list($name, $type) List option that accepts an array with values of the specified type. Each value is validated and must not be NULL. See Supported types.

Option::iterable($name) Iterable option that accepts both arrays and Traversable instances. The individual values are not validated.

Option::object($name) Object option.

Option::object($name, $className) Object option that only accepts instances of the given class or interface (or their descendants).

Option::resource($name) Resource option.

Option::scalar($name) Scalar option that accepts integers, floats, strings and booleans.

Option::choice($name, ...$choices) Choice option that accepts one of the listed values only (compared in strict mode).

Option::choiceList($name, ...$choices) Choice list option that accepts an array consisting of any of the listed values (compared in strict mode). Duplicates are allowed. NULL values are not allowed.

Option::node($name, ...$options) Node option that accepts an array of the specified options. See Node options.

Option::nodeList($name, ...$options) Node list option that accepts an array of arrays of the specified options. See Node options. ========================================== ===================================================

Option configuration

Option instances can be configured further by using the following methods.

All methods implement a fluent interface, for example:

Option::string('name') ->default('foo') ->nullable();

required()

Makes the option required (and removes any previously set default value).

default($default)

Makes the option optional and specifies a default value.

Lazy default values (leaf-only)

To specify a lazy default value, pass a closure with the following signature:

Option::string('foo')->default(function (Node $node) { // return value can also depend on other options return 'default'; });

Once the default value is needed, the closure will be called and its return value stored for later use (so it will not be called more than once).

nullable()

Make the option nullable, accepting NULL in addition to the specified type.

notNullable()

Make the option non-nullable, not accepting NULL.

allowEmpty()

Allow empty values to be passed to this option.

notEmpty()

Make the option reject empty values.

A value is considered empty if PHP's empty() returns TRUE.

normalize(callable $normalizer)

Append a normalizer to the option. The normalizer should accept a value and return the normalized value or throw Kuria\Options\Exception\NormalizerException on failure.

See Normalizer and validator value types.

Output:

object(Kuria\Options\Node)#7 (1) {
  ["name"]=>
  string(7) "foo bar"
}

validate(callable $validator)

Append a validator to the option. The validator should accept and validate a value.

The validator should return one of the following:

Output:

Failed to resolve options due to following errors:

1) email: must be a valid email address

Supported types

Any other type is considered to be a class name, accepting instances of the given class or interface (or their descendants).

An option defined as nullable will also accept a NULL value. See nullable().

Normalizer and validator value types

The type of the value passed to normalizers and validators depend on the type of the option.

Node options

Node options accept an array of the specified options. With them it is possible to resolve more complex structures.

Handling validation errors

The Resolver->resolve() method throws Kuria\Options\Exception\ResolverException on failure.

The specific errors can be retrieved by calling getErrors() on the exception object.

$resolver = new Resolver();

$resolver->addOption( Option::string('name'), Option::int('level'), Option::int('score') );

try { $resolver->resolve([ 'name' => null, 'level' => 'not_a_string', 'foo' => 'bar', ]); } catch (ResolverException $e) { foreach ($e->getErrors() as $error) { echo $error->getFormattedPath(), "\t", $error->getMessage(), "\n"; } }

Output:

name    string expected, but got NULL instead
level   int expected, but got "not_a_string" instead
score   this option is required
foo     unknown option

Ignoring unknown keys

The Resolver can be configured to ignore unknown keys by calling $resolver->setIgnoreUnknown(true).

Integrating the options resolver

The StaticOptionsTrait can be used to easily add static option support to a class.

It has the added benefit of caching and reusing the resolver in multiple instances of the class. If needed, the cache can be cleared by calling Foo::clearOptionsResolverCache().

class Foo { use StaticOptionsTrait;

/* @var Node / private $config;

function __construct(array $options) { $this->config = static::resolveOptions($options); }

protected static function defineOptions(Resolver $resolver): void { $resolver->addOption( Option::string('path'), Option::bool('enableCache')->default(false) ); }

function dumpConfig(): void { var_dump($this->config); }

}

Instantiation example:

$foo->dumpConfig();

Output:

object(Kuria\Options\Node)#8 (2) {
  ["path"]=>
  string(8) "file.txt"
  ["enableCache"]=>
  bool(false)
}

All versions of options with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1
kuria/debug Version ^4.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package kuria/options contains the following files

Loading the files please wait ....