Download the PHP package chevere/parameter without Composer
On this page you can find all versions of the php package chevere/parameter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download chevere/parameter
More information about chevere/parameter
Files in chevere/parameter
Package parameter
Short Description Library around parameter-argument
License Apache-2.0
Homepage https://chevere.org
Informations about the package parameter
Parameter
Summary
Parameter is a library around parameter-argument which provides additional functionality with validation rules and schema introspection.
Installing
Parameter is available through Packagist and the repository source is at chevere/parameter.
What it does?
Parameter enables to spawn dynamic parameters of any type with extra rules.
For example, an integer of minimum value 10.
In function or method parameters you can use attributes to define validation rules for parameters and return value.
Validation can be triggered using validated
(example above), delegated to a caller wrapper. Parameter provides helpers to access rules for both parameters and return value to ease wiring process.
Rules defined by each parameter provide a human-readable schema which allows to expose the validation criteria.
How to use
Parameter provides an API which can be used to create parameters using functions and/or attributes. Parameter objects can be used directly in the logic while attributes requires a read step.
Inline usage
Use inline validation to go from this:
To this:
Attribute-based usage
Use attributes to define rules for parameters and return value.
Use attribute delegated validation with the validated()
function to go from this:
To this:
Use reflectionToParameters
and reflectionToReturn
functions for manual validation for arguments and return value:
Use attribute inline validation for manual validation within the function body:
CallableAttr
Attributes in PHP only support expressions you can use on class constants. Is not possible to directly define dynamic parameters using attributes.
To avoid this limitation you can use CallableAttr
attribute which enables to forward parameter resolution to a callable returning a ParameterInterface
instance.
Types
A Parameter is an object implementing ParameterInterface
. Every Parameter can define a description
and a default
value, plus additional validation rules depending on the type.
A Parameter can be defined using functions and/or attributes, it takes same arguments for both.
When invoking a Parameter $param('value')
it will trigger validation against the passed argument.
String
Use function string
to create a StringParameter
. Pass a regex
for string matching.
Use StringAttr
attribute to define a string parameter.
String pseudo-parameters
The following parameters are based on String.
Enum string
Use function enum
to create a StringParameter
matching a list of strings.
Use EnumAttr
attribute to define an enum string parameter.
Int string
Use function intString
to create a StringParameter
matching a string integers.
Bool string
Use function boolString
to create a StringParameter
matching 0
and 1
strings.
Date string
Use function date
to create a StringParameter
matching YYYY-MM-DD
strings.
Time string
Use function time
to create a StringParameter
matching hh:mm:ss
strings.
Datetime string
Use function datetime
to create a StringParameter
matching YYYY-MM-DD hh:mm:ss
strings.
Int
Use function int
to create a IntParameter
. Pass min
and max
values for integer range, accept
for a list of accepted integers and reject
for a list of rejected integers.
Use IntAttr
attribute to define an integer parameter.
Int pseudo-parameters
The following parameters are based on Int.
Bool int
Use function boolInt
to create a IntParameter
matching 0
and 1
integers.
Float
Use function float
to create a FloatParameter
. Pass min
and max
values for float range, accept
for a list of accepted floats and reject
for a list of rejected floats.
Use FloatAttr
attribute to define a float parameter.
Bool
Use function bool
to create a BoolParameter
.
Use BoolAttr
attribute to define a bool parameter.
Null
Use function null
to create a NullParameter
.
Use NullAttr
attribute to define a null parameter.
Object
Use function object
to create a ObjectParameter
. Pass a className for the object class name.
Use ObjectAttr
attribute to define an object parameter.
Mixed
Use function mixed
to create a MixedParameter
.
Union
Use function union
to create a UnionParameter
. Pass a list of parameters to match, target value must match at least one.
Array
Parameter for type array
is handled as a composite Parameter holding parameter definition for each one of its members.
Use function arrayp
to create an ArrayParameter
for named arguments as required array keys.
Parameter supports nested arrays of any depth:
Use ArrayAttr
attribute to define an array parameter.
With required
use method withRequired
to define required parameters.
With optional
use method withOptional
to define optional parameters.
👉 Note: Optional parameters will be validated only if a matching key is provided.
With modify
use method withModify
to define modify parameters.
With make optional
use method withMakeOptional
to make required parameters optional.
With make required
use method withMakeRequired
to make optional parameters required.
Without
use method without
to remove parameters.
With optional minimum
use method withOptionalMinimum
to define a minimum number of optional parameters. Useful if all parameters are optional but 1.
Array pseudo-parameters
The following parameters are based on Array.
Array String
Use function arrayString
to create an ArrayStringParameterInterface
for string values. It only supports string parameters.
File
Use function file
to create an ArrayParameter
for file uploads.
By default it provides validation for $_FILES
shape, but you can define your own validation rules. For example, to validate name and contents:
Iterable
Iterable type Traversable|array
is considered as a composite Parameter holding a generic definition for key and value. Parameter enables to describe this collection of items sharing the same shape.
Use function iterable
to create an IterableParameter
. Pass a V
and K
parameters for generic key and value.
It also works with named keys:
Helpers
parameters
Use function parameters
to create a Parameters
instance.
arguments
Use function arguments
to create a Arguments
instance.
assertNamedArgument
Use function assertNamedArgument
to assert a named argument.
toParameter
Use function toParameter
to create a ParameterInterface
instance from a type string. In the example below the resulting $parameter
will be an IntParameter
.
arrayFrom
Use function arrayFrom
to create an Array parameter from another array parameter. In the example below the resulting $array
will contain only name
and id
keys as defined in $source
.
takeKeys
Use function takeKeys
to retrieve an array with the keys from a parameter. In the example below $keys
will contain id
and size
.
takeFrom
Use function takeFrom
to retrieve an iterator with the desired keys from a parameter. In the example below $iterator
will yield size
and name
keys.
parametersFrom
Use function parametersFrom
to create a Parameters
with desired keys from a parameter. In the example below $parameters
will contain size
and name
keys.
getParameters
Use function getParameters
to retrieve a Parameters
instance from an object implementing either ParameterAccessInterface
or ParametersInterface
.
getType
Use function getType
to retrieve the type as is known by this library.
parameterAttr
Use function parameterAttr
to retrieve an object implementing ParameterAttributeInterface
from a function or class method parameter.
reflectionToParameters
Use function reflectionToParameters
to retrieve a Parameters
instance from a ReflectionFunction
or ReflectionMethod
instance.
reflectionToReturn
Use function reflectionToReturn
to retrieve a ParameterInterface
instance from a ReflectionFunction
or ReflectionMethod
instance.
reflectedParameterAttribute
Use function reflectedParameterAttribute
to retrieve an object implementing ParameterAttributeInterface
from a ReflectionParameter
instance.
validated
Use function validated
to validate a function or method arguments.
Examples
Inline validation
-
Validate string starting with "a":
-
Validate an int of min value
100
: -
Validate an int accept list:
-
Validate a float reject list:
-
Validate an array:
-
Validate an iterable
int
list: -
Validate an iterable int list with string key type rules:
- Validate an union of type ?int:
Attribute delegated validation
-
Use function
validated()
to get a return validated against all rules. -
Use function
reflectionToParameters()
to get rules for validating arguments. - Use function
reflectionToReturn()
to get rules for validating function/method return value:
Attribute inline validation
Use valid()
on the function/method body to trigger validation for arguments.
- Validate an string enum for
Hugo
,Paco
,Luis
: -
Validate a min float value of
1000
: -
Validate an int of any value but
0
and100
: -
Validate a
nastynested array: - Validate iterable int list:
Use function returnAttr()
on the function/method body.
-
Validate int
min: 0, max: 5
return: - Validate array return:
💡 By convention when omitting ReturnAttr
the method public static function return(): ParameterInterface
(if any) will be used to determine return validation rules.
Documentation
Documentation is available at chevere.org.
License
Copyright Rodolfo Berrios A.
Chevere is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
All versions of parameter with dependencies
chevere/data-structure Version ^1.0.1
chevere/message Version ^1.0.0
chevere/regex Version ^1.0.1