1. Go to this page and download the library: Download chevere/parameter 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/ */
chevere / parameter example snippets
use function Chevere\Parameter\int;
$int = int(min: 10);
$int($var); // exception if $var < 10
use Chevere\Parameter\Attributes\FloatAttr;
use Chevere\Parameter\Attributes\IntAttr;
use Chevere\Parameter\Attributes\ReturnAttr;
use function Chevere\Parameter\returnAttr;
use function Chevere\Parameter\validated;
#[ReturnAttr(
new FloatAttr(min: 0, max: 2400)
)]
function wageWeekWA(
#[IntAttr(min: 1628)]
int $cents,
#[FloatAttr(min: 0, max: 40)]
float $hours
) {
return $cents*$hours/100;
}
validated('wageWeekWA', $cents, $hours);
use Chevere\Parameter\Attributes\IntAttr;
use Chevere\Parameter\Attributes\ReturnAttr;
use Chevere\Parameter\Attributes\StringAttr;
use function Chevere\Parameter\validated;
#[ReturnAttr(
new StringAttr('/ok$/')
)]
function myFunction(
#[IntAttr(min: 1, max: 10)]
int $var
): string
{
return 'done ok';
}
$result = validated('myFunction', $var);
use ReflectionFunction;
use function Chevere\Parameter\reflectionToParameters;
use function Chevere\Parameter\reflectionToReturn;
$reflection = new ReflectionFunction('myFunction');
$parameters = reflectionToParameters($reflection);
$return = reflectionToReturn($reflection);
$parameters(...$args); // valid $args
$result = myFunction(...$args); // myFunction call
$return($result); // valid $result
use Chevere\Parameter\Attributes\IntAttr;
use Chevere\Parameter\Attributes\ReturnAttr;
use Chevere\Parameter\Attributes\StringAttr;
use function Chevere\Parameter\valid;
use function Chevere\Parameter\returnAttr;
#[ReturnAttr(
new StringAttr('/ok$/')
)]
function myFunction(
#[IntAttr(min: 1, max: 10)]
int $var
): string
{
valid(); // valid $var
$return = 'ok';
return returnAttr()($return); // valid $return
}
use Chevere\Parameter\Interfaces\ParameterInterface;
use Chevere\Parameter\Attributes\CallableAttr;
function myCallable(): ParameterInterface
{
return arrayp(
email: string(),
)->withOptional(
name: string(),
);
}
#[CallableAttr('myCallable')]
use function Chevere\Parameter\string;
// Any string
$string = string();
// String matching bin-<digits>
$string = string('/^bin-[\d]+$/');
$string('bin-123');
use Chevere\Parameter\Attributes\StringAttr;
#[StringAttr('/^bin-[\d]+$/')]
use function Chevere\Parameter\enum;
$enum = enum('on', 'off');
$enum('on');
$enum('off');
use Chevere\Parameter\Attributes\EnumAttr;
#[EnumAttr('on', 'off')]
use function Chevere\Parameter\intString;
$int = intString();
$int('100');
use function Chevere\Parameter\boolString;
$bool = boolString();
$bool('0');
$bool('1');
use function Chevere\Parameter\date;
$date = date();
$date('2021-01-01');
use function Chevere\Parameter\time;
$time = time();
$time('12:00:00');
use function Chevere\Parameter\datetime;
$datetime = datetime();
$datetime('2024-01-09 10:53:00');
use function Chevere\Parameter\int;
// Any int
$int = int();
$int(1);
// Integer between 0 and 100
$int = int(min: 0, max: 100);
$int(50);
// Integer matching 1, 2 or 3
$int = int(accept: [1, 2, 3]);
$int(2);
// Integer not-matching 1, 2 or 3
$int = int(reject: [1, 2, 3]);
$int(4);
use Chevere\Parameter\Attributes\IntAttr;
#[IntAttr(min: 0, max: 100)]
use function Chevere\Parameter\boolInt;
$bool = boolInt();
$bool(0);
$bool(1);
use function Chevere\Parameter\float;
// Any float
$float = float();
$float(1.5);
// Float between 0 and 100
$float = float(min: 0, max: 100);
$float(50.5);
// Float matching 1.5, 2.5 or 3.5
$float = float(accept: [1.5, 2.5, 3.5]);
$float(2.5);
// Float not-matching 1.5, 2.5 or 3.5
$float = float(reject: [1.5, 2.5, 3.5]);
$float(4.5);
use Chevere\Parameter\Attributes\FloatAttr;
#[FloatAttr(min: 0, max: 100)]
use function Chevere\Parameter\bool;
$bool = bool();
$bool(true);
$bool(false);
use Chevere\Parameter\Attributes\BoolAttr;
#[BoolAttr]
use function Chevere\Parameter\null;
$null = null();
$null(null);
use Chevere\Parameter\Attributes\NullAttr;
#[NullAttr]
use function Chevere\Parameter\object;
$object = object(stdClass::class);
$object(new stdClass());
use Chevere\Parameter\Attributes\ObjectAttr;
#[ObjectAttr(stdClass::class)]
use function Chevere\Parameter\mixed;
$mixed = mixed();
$mixed(1);
$mixed('1');
$mixed(true);
$mixed(null);
use function Chevere\Parameter\union;
// Any string or null
$union = union(string(), null());
$union('abc');
$union(null);
// Any digit string or any integer
$union = union(
intString(),
integer()
);
$union('100');
$union(100);
use function Chevere\Parameter\arrayp;
use function Chevere\Parameter\float;
use function Chevere\Parameter\int;
$array = arrayp(
id: int(min: 0),
items: arrayp(
id: int(min: 0),
price: float(min: 0),
),
);
$array([
'id' => 1,
'items' => [
'id' => 25,
'price' => 16.5,
]
]);
use Chevere\Parameter\Attributes\ArrayAttr;
use Chevere\Parameter\Attributes\FloatAttr;
use Chevere\Parameter\Attributes\IntAttr;
#[ArrayAttr(
id: new IntAttr(),
items: new ArrayAttr(
id: new IntAttr(),
price: new FloatAttr(),
),
)]
use function Chevere\Parameter\int;
use function Chevere\Parameter\iterable;
$iterable = iterable(int(min: 0));
$iterable([0, 1, 2, 3]);
use function Chevere\Parameter\int;
use function Chevere\Parameter\iterable;
use function Chevere\Parameter\string;
$iterable = iterable(
V: arrayp(
id: int(min: 0),
name: string('^[\w]{1,255}'),
)
K: string(),
);
$iterable([
'based' => [
'id' => 1,
'name' => 'OscarGangas'
],
'fome' => [
'id' => 2,
'name' => 'BomboFica'
],
]);
use function Chevere\Parameters\parameters;
use function Chevere\Parameters\string;
$parameters = parameters(foo: string());
use function Chevere\Parameters\arguments;
use function Chevere\Parameters\string;
$arguments = arguments($parameters, ['foo' => 'bar']);
use function Chevere\Parameters\assertNamedArgument;
use function Chevere\Parameters\int;
use function Chevere\Parameters\parameters;
$parameter = int(min: 10);
assertNamedArgument(
name: 'foo',
parameter: $parameter,
argument: 20
);
use function Chevere\Parameters\toParameter;
$parameter = toParameter('int');
use function Chevere\Parameters\arrayFrom;
use function Chevere\Parameters\arrayp;
use function Chevere\Parameters\int;
use function Chevere\Parameters\string;
$source = arrayp(
id: int(),
name: string(),
email: string(),
age: int(),
);
$array = arrayFrom($source, 'name', 'id');
use function Chevere\Parameters\arrayp;
use function Chevere\Parameters\int;
use function Chevere\Parameters\takeKeys;
$array = arrayp(
id: int(),
size: int(),
);
$keys = takeKeys($array);
use function Chevere\Parameters\arrayp;
use function Chevere\Parameters\int;
use function Chevere\Parameters\string;
use function Chevere\Parameters\takeFrom;
$array = arrayp(
id: int(min: 0),
size: int(min: 100),
name: string(),
);
$iterator = takeFrom($array, 'size', 'name');
use function Chevere\Parameters\arrayp;
use function Chevere\Parameters\int;
use function Chevere\Parameters\string;
use function Chevere\Parameters\parametersFrom;
$array = arrayp(
id: int(min: 0),
size: int(min: 100),
name: string(),
);
$parameters = parametersFrom($array, 'size', 'name');
use function Chevere\Parameters\getParameters;
$parameters = getParameters($object);
use function Chevere\Parameters\getType;
$type = getType(1); // int
use function Chevere\Parameters\parameterAttr;
use Chevere\Parameter\Attributes\StringAttr;
function myFunction(
#[StringAttr('/^bin-[\d]+$/')]
string $foo
): void {
// ...
}
$stringAttr = parameterAttr('foo', 'myFunction');
$stringAttr('bin-123');
use function Chevere\Parameter\reflectionToParameters;
$parameters = reflectionToParameters($reflection);
use function Chevere\Parameter\reflectionToReturn;
$parameter = reflectionToReturn($reflection);
use function Chevere\Parameter\reflectedParameterAttribute;
$parameterAttribute = reflectedParameterAttribute($reflectionParameter);
use function Chevere\Parameter\validated;
$result = validated('myFunction', $arg1, $arg2,);
use function Chevere\Parameter\string;
$value = 'ahhh';
string('/^a.+/')($value);
use function Chevere\Parameter\int;
$value = 100;
int(min: 100)($value);
use function Chevere\Parameter\int;
$value = 1;
int(accept: [1, 2, 3])($value);
use function Chevere\Parameter\float;
$value = 3.1;
float(reject: [1.1, 2.1])($value);
use function Chevere\Parameter\arrayp;
use function Chevere\Parameter\int;
use function Chevere\Parameter\string;
$value = [
'id' => 1,
'name' => 'Pepe'
];
arrayp(
id: int(min: 1),
name: string('/^[A-Z]{1}\w+$/')
)($value);
use function Chevere\Parameter\int;
use function Chevere\Parameter\iterable;
$value = [1, 2, 3];
iterable(int())($value);
use function Chevere\Parameter\int;
use function Chevere\Parameter\iterable;
$value = [
'unila' => 1,
'dorila' => 2,
'tirifila' => 3,
];
iterable(
K: string('/ila$/'),
V: int(min: 1)
)($value);
use function Chevere\Parameter\int;
use function Chevere\Parameter\null;
$value = 1;
union(int(), null())($value);
use function Chevere\Parameter\validated;
$result = validated('myFunction', $var);
use ReflectionMethod;
use Chevere\Parameter\Attributes\IntAttr;
use function Chevere\Parameter\arguments;
use function Chevere\Parameter\reflectionToParameters;
$class = new class() {
public function wea(
#[IntAttr(accept: [1, 10, 100])]
int $base
): void {
}
};
$object = new $class();
$reflection = new ReflectionMethod($object, 'wea');
$parameters = reflectionToParameters($reflection);
$args = ['base' => 10];
$parameters(...$args); // valid $args
$result = $object->wea(...$args);
use ReflectionFunction;
use Chevere\Parameter\Attributes\IntAttr;
use Chevere\Parameter\Attributes\ReturnAttr;
use function Chevere\Parameter\reflectionToReturn;
$function =
#[ReturnAttr(
new IntAttr(min: 1000)
)]
function (int $base): int {
return 10 * $base;
};
$reflection = new ReflectionFunction($function);
$return = reflectionToReturn($reflection);
$base = 10;
$result = $function($base);
$result = $return($result); // Validates result
use Chevere\Parameter\Attributes\EnumAttr;
use function Chevere\Parameter\validate;
function myEnum(
#[EnumAttr('Hugo', 'Paco', 'Luis')]
string $name,
#[FloatAttr(min: 1000)]
float $money
): void
{
valid();
// Or single...
valid('name');
valid('money');
}
$arg1 = 'Paco';
$arg2 = 1000.50;
myEnum($arg1, $arg2);
use Chevere\Parameter\Attributes\IntAttr;
use function Chevere\Parameter\validate;
function myInt(
#[IntAttr(reject: [0, 100])]
int $id
): void
{
valid();
}
$value = 50;
myInt($value);
use Chevere\Parameter\Attributes\ArrayAttr;
use Chevere\Parameter\Attributes\IntAttr;
use Chevere\Parameter\Attributes\StringAttr;
use Chevere\Parameter\Attributes\IterableAttr;
use function Chevere\Parameter\validate;
function myArray(
#[ArrayAttr(
id: new IntAttr(min: 1),
role: new ArrayAttr(
mask: new IntAttr(accept: [64, 128, 256]),
name: new StringAttr('/[a-z]+/'),
tenants: new IterableAttr(
new IntAttr(min: 1)
)
),
)]
array $spooky
): void
{
valid();
}
$value = [
'id' => 10,
'role' => [
'mask' => 128,
'name' => 'admin',
'tenants' => [1, 2, 3, 4, 5]
],
];
myArray($value);
use Chevere\Parameter\Attributes\IntAttr;
use Chevere\Parameter\Attributes\IterableAttr;
use function Chevere\Parameter\validate;
function myIterable(
#[IterableAttr(
new IntAttr(),
)]
array $list = [0,1,2]
): void
{
valid();
}
use Chevere\Parameter\Attributes\IntAttr;
use Chevere\Parameter\Attributes\ReturnAttr;
use function Chevere\Parameter\returnAttr;
#[ReturnAttr(
new IntAttr(min: 0, max: 5)
)]
public function myReturnInt(): int
{
$result = 1;
return returnAttr()($result);
}
use Chevere\Parameter\Attributes\ArrayAttr;
use Chevere\Parameter\Attributes\IntAttr;
use Chevere\Parameter\Attributes\StringAttr;
use Chevere\Parameter\Attributes\ReturnAttr;
use function Chevere\Parameter\returnAttr;
#[ReturnAttr(
new ArrayAttr(
id: new IntAttr(min: 0),
name: new StringAttr()
)
)]
public function myReturnArray(): array
{
$result = [
'id' => 1,
'name' => 'Peoples Hernandez'
];
return returnAttr()($result);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.