# Parameter
The Parameter component is in charge of providing extended type support with the ability to handle variadic typed parameters and its argument matching. It allows to provide variable parameter-argument type matching relying on the Type component.
# Creating a Parameter
In the code below, type for class FullQualifiedName
is created with a description and an added attribute.
use Chevere\Components\Parameter\Parameter;
use Chevere\Components\Type\Type;
$parameter = new Parameter(
type: new Type('FullQualifiedName')
);
# Description
The withDescription
method is used to provide the parameter description, which is a short summary explaining the purpose of the parameter.
use Chevere\Components\Parameter\Parameter;
/**
* @var Parameter $parameter
*/
$parameter = $parameter
->withDescription(description: 'Stuff for doing some');
# Attributes
The withAddedAttribute
method is used to provide parameter attributes, which are flags that can tag the parameter.
use Chevere\Components\Parameter\Parameter;
/**
* @var Parameter $parameter
*/
$parameter = $parameter
->withAddedAttribute('langEn', 'localeEn');
$parameter->hasAttribute('langEn'); // true
# Parameter types
The following parameter type classes are provided for convenience.
# Array Parameter
The ArrayParameter component in charge of defining a parameter of type array. It support default array
value.
use Chevere\Components\Parameter\ArrayParameter;
(new ArrayParameter)
->withDefault(value: ['do' => true]);
# Boolean Parameter
The BooleanParameter component in charge of defining a parameter of type boolean. It support default boolean
value.
use Chevere\Components\Parameter\BooleanParameter;
(new BooleanParameter)
->withDefault(value: true);
# Float Parameter
The FloatParameter component in charge of defining a parameter of type float. It support default float
value.
use Chevere\Components\Parameter\FloatParameter;
(new FloatParameter)
->withDefault(value: 12.3);
# Integer Parameter
The IntegerParameter component in charge of defining a parameter of type integer. It support integer
default value.
use Chevere\Components\Parameter\IntegerParameter;
(new IntegerParameter)
->withDefault(value: 123);
# String Parameter
The StringParameter component in charge of defining a parameter of type string. It support regex and string
default value.
use Chevere\Components\Parameter\StringParameter;
use Chevere\Components\Regex\Regex;
(new StringParameter)
->withDefault(value: 'id-000')
->withRegex(regex: new Regex('/^id-[\d]+$/'));
The above parameter will require an argument like id-123
to validate.
# Parameters
The Parameters component in charge of collecting objects implementing the ParameterInterface.
use Chevere\Components\Parameter\Parameters;
use Chevere\Components\Parameter\IntegerParameter;
(new Parameters)
->withAddedRequired(
id: (new IntegerParameter)
->withDefault(value: 123);
)
->withAddedOptional(
priority: (new IntegerParameter)
->withDefault(value: 1);
);
Parameters can be added either as required or optional.
# Arguments
The Arguments component in charge of providing arguments matching the declared Parameters.
use Chevere\Components\Parameter\Arguments;
use Chevere\Components\Parameter\Parameters;
new Arguments(
parameters: (new Parameters)
->withAddedRequired(id: new IntegerParameter),
id: 123
);
The example above shows how to construct an Arguments instance by passing the Parameters and the matching named arguments.
# Retrieve an argument
The get
method is used to retrieve an argument "as-is", without type checking.
In the example below $argument
must be either casted as boolean or use a doc block annotation to provide type-hint while $boolean
type gets hinted directly from the function return.
use Chevere\Components\Parameter\Arguments;
/**
* @var Arguments $arguments
* @var bool $argument
*/
$argument = $arguments->get(name: 'namedArgument');
$boolean = $arguments->getBoolean(name: 'namedArgument');
The following methods are available to provide typed argument retrieval:
Method | Return type |
---|---|
getBoolean | bool |
getString | string |
getInteger | integer |
getFloat | float |
getArray | array |
⚠ Note that the above methods will throw a TypeException on type mismatch.