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. Go to chevere/action for our object-oriented convention around this package.
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.
Reference
Core types provided by Parameter.
Type | Helper | Attribute | Description |
---|---|---|---|
Array | arrayp |
ArrayAttr |
Array with named parameters |
Bool | bool |
BoolAttr |
Boolean |
Float | float |
FloatAttr |
Floating point number |
Int | int |
IntAttr |
Integer |
Iterable | iterable |
IterableAttr |
Iterable of key-value pairs |
Mixed | mixed |
-- | Mixed |
Null | null |
NullAttr |
Null |
Object | object |
-- | Object |
String | string |
StringAttr |
String matching a regex |
Union | union |
UnionAttr |
Union of parameters |
Array based-parameters provided.
Type | Helper | Description |
---|---|---|
ArrayString | arrayString |
Array with string values |
File | file |
File upload |
String based-parameters provided.
Type | Helper | Description |
---|---|---|
BoolString | boolString |
Bool string |
Date | date |
Date string |
Datetime | datetime |
Datetime string |
Enum | enum |
Enum string |
IntString | intString |
Int string |
Time | time |
Time string |
Int based-parameters provided.
Type | Helper | Description |
---|---|---|
BoolInt | boolInt |
Bool int |
Non-type attributes provided.
Attribute | Description |
---|---|
CallableAttr | Forward parameter resolution to a callable |
ReturnAttr | Return value validation |
Inline usage
Inline usage refers to the direct use of functions to create parameters and validate arguments.
Use inline validation to go from this:
To this:
When invoking a Parameter $param($arg)
or $param->__invoke($arg)
it will trigger validation against the passed argument and return the validated argument:
- Will fill-in any missing optional parameters with their default values.
- Will exclude any extra unexpected parameters.
Attribute usage
Attribute usage refers to the use of attributes to define parameters and return rules. You can use attribute notation for class properties, methods/functions 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:
Native attributes support
Parameter will understand/complement native attribute annotations.
#[SensitiveParameter]
ReturnAttr
Use ReturnAttr
attribute to define a return value validation rule.
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
, default
value, sensitive
flag, plus additional validation rules depending on the type.
A Parameter can be defined using functions or attributes, it takes same arguments for both.
Immutable methods
A Parameter provides immutable methods to re-define its rules. Every with
method returns a new instance preserving the original state. This enables to create Parameter variations from a base definition.
Immutable methods of a Parameter can be used to re-define its rules. For example, IntParameter
provides additional methods to define integer range:
Methods unique to each parameter:
- IntParameter:
withMin
,withMax
,withAccept
,withReject
- FloatParameter:
withMin
,withMax
,withAccept
,withReject
- StringParameter:
withRegex
- ArrayParameter:
withRequired
,withOptional
,withModify
,withMakeOptional
,withMakeRequired
,without
,withOptionalMinimum
- ObjectParameter:
withClassName
- IterableParameter:
withKey
,withValue
String
Use function string
to create a StringParameter
. Pass a regex
for string matching.
Use StringAttr
attribute to define a string parameter using attribute notation.
String based-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 using attribute notation.
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 using attribute notation.
Int based-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 using attribute notation.
Bool
Use function bool
to create a BoolParameter
.
Use BoolAttr
attribute to define a bool parameter using attribute notation.
Null
Use function null
to create a NullParameter
.
Use NullAttr
attribute to define a null parameter using attribute notation.
Object
Use function object
to create a ObjectParameter
. Pass a className for the object class name.
Use ObjectAttr
attribute to define an object parameter using attribute notation.
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.
Use UnionAttr
attribute to define an union parameter using attribute notation.
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 using attribute notation.
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 based-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.1.0
chevere/message Version ^1.0.0
chevere/regex Version ^1.0.1