{"id":27994504,"url":"https://github.com/chevere/parameter","last_synced_at":"2025-06-15T23:09:11.418Z","repository":{"id":210119325,"uuid":"725796393","full_name":"chevere/parameter","owner":"chevere","description":"PHP library for creating, validating, and managing parameters with strict validation rules and schema introspection.","archived":false,"fork":false,"pushed_at":"2025-03-07T19:33:29.000Z","size":481,"stargazers_count":9,"open_issues_count":6,"forks_count":1,"subscribers_count":0,"default_branch":"1.1","last_synced_at":"2025-06-09T12:48:51.116Z","etag":null,"topics":["chevere","php","php-library"],"latest_commit_sha":null,"homepage":"https://chevere.org/packages/parameter","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chevere.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-11-30T22:24:36.000Z","updated_at":"2025-03-07T19:33:32.000Z","dependencies_parsed_at":"2023-12-31T15:23:06.191Z","dependency_job_id":"11b4859d-dfe2-42fa-b4e5-236fa3f888b7","html_url":"https://github.com/chevere/parameter","commit_stats":null,"previous_names":["chevere/parameter"],"tags_count":10,"template":false,"template_full_name":"chevere/package-template","purl":"pkg:github/chevere/parameter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chevere%2Fparameter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chevere%2Fparameter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chevere%2Fparameter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chevere%2Fparameter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chevere","download_url":"https://codeload.github.com/chevere/parameter/tar.gz/refs/heads/1.1","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chevere%2Fparameter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260063758,"owners_count":22953591,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chevere","php","php-library"],"created_at":"2025-05-08T19:13:08.224Z","updated_at":"2025-06-15T23:09:11.394Z","avatar_url":"https://github.com/chevere.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Parameter\n\n![Chevere](chevere.svg)\n\n[![Build](https://img.shields.io/github/actions/workflow/status/chevere/parameter/test.yml?branch=1.1\u0026style=flat-square)](https://github.com/chevere/parameter/actions)\n![Code size](https://img.shields.io/github/languages/code-size/chevere/parameter?style=flat-square)\n[![Apache-2.0](https://img.shields.io/github/license/chevere/parameter?style=flat-square)](LICENSE)\n[![PHPStan](https://img.shields.io/badge/PHPStan-level%209-blueviolet?style=flat-square)](https://phpstan.org/)\n[![Mutation testing badge](https://img.shields.io/endpoint?style=flat-square\u0026url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fchevere%2Fparameter%2F1.1)](https://dashboard.stryker-mutator.io/reports/github.com/chevere/parameter/1.1)\n\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=chevere_parameter\u0026metric=alert_status)](https://sonarcloud.io/dashboard?id=chevere_parameter)\n[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_parameter\u0026metric=sqale_rating)](https://sonarcloud.io/dashboard?id=chevere_parameter)\n[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_parameter\u0026metric=reliability_rating)](https://sonarcloud.io/dashboard?id=chevere_parameter)\n[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_parameter\u0026metric=security_rating)](https://sonarcloud.io/dashboard?id=chevere_parameter)\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=chevere_parameter\u0026metric=coverage)](https://sonarcloud.io/dashboard?id=chevere_parameter)\n[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=chevere_parameter\u0026metric=sqale_index)](https://sonarcloud.io/dashboard?id=chevere_parameter)\n[![CodeFactor](https://www.codefactor.io/repository/github/chevere/parameter/badge)](https://www.codefactor.io/repository/github/chevere/parameter)\n\n## Summary\n\nParameter is a library around parameter-argument which provides additional functionality with validation rules and schema introspection. Go to [chevere/action](https://github.com/chevere/action) for our object-oriented convention around this package.\n\n## Installing\n\nParameter is available through [Packagist](https://packagist.org/packages/chevere/parameter) and the repository source is at [chevere/parameter](https://github.com/chevere/parameter).\n\n```sh\ncomposer require chevere/parameter\n```\n\n## What it does?\n\nParameter enables to spawn dynamic parameters of any type with extra rules.\n\nFor example, an integer of minimum value 10.\n\n```php\nuse function Chevere\\Parameter\\int;\n\n$int = int(min: 10);\n$int($var); // exception if $var \u003c 10\n```\n\nIn function or method parameters you can use attributes to define validation rules for parameters and return value.\n\n```php\nuse Chevere\\Parameter\\Attributes\\FloatAttr;\nuse Chevere\\Parameter\\Attributes\\IntAttr;\nuse Chevere\\Parameter\\Attributes\\ReturnAttr;\nuse function Chevere\\Parameter\\returnAttr;\nuse function Chevere\\Parameter\\validated;\n\n#[ReturnAttr(\n    new FloatAttr(min: 0, max: 2400)\n)]\nfunction wageWeekWA(\n    #[IntAttr(min: 1628)]\n    int $cents,\n    #[FloatAttr(min: 0, max: 40)]\n    float $hours\n) {\n    return $cents*$hours/100;\n}\nvalidated('wageWeekWA', $cents, $hours);\n```\n\nValidation can be triggered using `validated` (example above), [inline](#inline-usage) and/or [delegated](#attribute-delegated-validation) to a caller wrapper. Parameter provides helpers to access rules for both parameters and return value to ease wiring process.\n\nRules defined by each parameter provide a human-readable schema which allows to expose the validation criteria.\n\n## Reference\n\nCore types provided by Parameter.\n\n| Type                  | Helper     | Attribute      | Description                 |\n| --------------------- | ---------- | -------------- | --------------------------- |\n| [Array](#array)       | `arrayp`   | `ArrayAttr`    | Array with named parameters |\n| [Bool](#bool)         | `bool`     | `BoolAttr`     | Boolean                     |\n| [Float](#float)       | `float`    | `FloatAttr`    | Floating point number       |\n| [Int](#int)           | `int`      | `IntAttr`      | Integer                     |\n| [Iterable](#iterable) | `iterable` | `IterableAttr` | Iterable of key-value pairs |\n| [Mixed](#mixed)       | `mixed`    | --             | Mixed                       |\n| [Null](#null)         | `null`     | `NullAttr`     | Null                        |\n| [Object](#object)     | `object`   | --             | Object                      |\n| [String](#string)     | `string`   | `StringAttr`   | String matching a regex     |\n| [Union](#union)       | `union`    | `UnionAttr`    | Union of parameters         |\n\n[Array based-parameters](#array-based-parameters) provided.\n\n| Type                         | Helper        | Description              |\n| ---------------------------- | ------------- | ------------------------ |\n| [ArrayString](#array-string) | `arrayString` | Array with string values |\n| [File](#file)                | `file`        | File upload              |\n\n[String based-parameters](#string-based-parameters) provided.\n\n| Type                         | Helper       | Description     |\n| ---------------------------- | ------------ | --------------- |\n| [BoolString](#bool-string)   | `boolString` | Bool string     |\n| [Date](#date-string)         | `date`       | Date string     |\n| [Datetime](#datetime-string) | `datetime`   | Datetime string |\n| [Enum](#enum-string)         | `enum`       | Enum string     |\n| [IntString](#int-string)     | `intString`  | Int string      |\n| [Time](#time-string)         | `time`       | Time string     |\n\n[Int based-parameters](#int-based-parameters) provided.\n\n| Type                 | Helper    | Description |\n| -------------------- | --------- | ----------- |\n| [BoolInt](#bool-int) | `boolInt` | Bool int    |\n\nNon-type attributes provided.\n\n| Attribute                     | Description                                |\n| ----------------------------- | ------------------------------------------ |\n| [CallableAttr](#callableattr) | Forward parameter resolution to a callable |\n| [ReturnAttr](#returnattr)     | Return value validation                    |\n\n## Inline usage\n\nInline usage refers to the direct use of functions to create parameters and validate arguments.\n\nUse [inline validation](#inline-validation) to go from this:\n\n```php\nif($var \u003e 10 || $var \u003c 1) {\n    throw new InvalidArgumentException();\n}\n```\n\nTo this:\n\n```php\nuse function Chevere\\Parameter\\int;\n\nint(min: 1, max: 10)($var);\n```\n\nWhen invoking a Parameter `$param($arg)` or `$param-\u003e__invoke($arg)` it will trigger validation against the passed argument and return the validated argument:\n\n* Will fill-in any missing optional parameters with their default values.\n* Will exclude any extra unexpected parameters.\n\n## Attribute usage\n\nAttribute 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.\n\nUse [attribute delegated validation](#attribute-delegated-validation) with the `validated()` function to go from this:\n\n```php\nfunction myFunction(int $var): string\n{\n    if($var \u003e 10 || $var \u003c 1) {\n        throw new InvalidArgumentException();\n    }\n    $return = 'done ok';\n    return preg_match('/ok$/', $return)\n        ? $return\n        : throw new InvalidArgumentException();\n}\n$result = myFunction($var);\n```\n\nTo this:\n\n```php\nuse Chevere\\Parameter\\Attributes\\IntAttr;\nuse Chevere\\Parameter\\Attributes\\ReturnAttr;\nuse Chevere\\Parameter\\Attributes\\StringAttr;\nuse function Chevere\\Parameter\\validated;\n\n#[ReturnAttr(\n    new StringAttr('/ok$/')\n)]\nfunction myFunction(\n    #[IntAttr(min: 1, max: 10,)]\n    int $var\n): string\n{\n    return 'done ok';\n}\n$result = validated('myFunction', $var);\n```\n\nUse `reflectionToParameters` and `reflectionToReturn` functions for manual validation for arguments and return value:\n\n```php\nuse ReflectionFunction;\nuse function Chevere\\Parameter\\reflectionToParameters;\nuse function Chevere\\Parameter\\reflectionToReturn;\n\n$reflection = new ReflectionFunction('myFunction');\n$parameters = reflectionToParameters($reflection);\n$return = reflectionToReturn($reflection);\n$parameters(...$args); // valid $args\n$result = myFunction(...$args); // myFunction call\n$return($result); // valid $result\n```\n\nUse [attribute inline validation](#attribute-inline-validation) for manual validation within the function body:\n\n```php\nuse Chevere\\Parameter\\Attributes\\IntAttr;\nuse Chevere\\Parameter\\Attributes\\ReturnAttr;\nuse Chevere\\Parameter\\Attributes\\StringAttr;\nuse function Chevere\\Parameter\\valid;\nuse function Chevere\\Parameter\\returnAttr;\n\n#[ReturnAttr(\n    new StringAttr('/ok$/')\n)]\nfunction myFunction(\n    #[IntAttr(min: 1, max: 10)]\n    int $var\n): string\n{\n    valid(); // valid $var\n    $return = 'ok';\n\n    return returnAttr()($return); // valid $return\n}\n```\n\n### Native attributes support\n\nParameter will understand/complement [native attribute](https://www.php.net/manual/reserved.attributes.php) annotations.\n\n`#[SensitiveParameter]`\n\n```php\nfunction myFunction(\n    #[SensitiveParameter]\n    #[IntAttr(min: 1)]\n    int $var\n): void;\n```\n\n### ReturnAttr\n\nUse `ReturnAttr` attribute to define a return value validation rule.\n\n```php\nuse Chevere\\Parameter\\Attributes\\ReturnAttr;\nuse Chevere\\Parameter\\Attributes\\StringAttr;\n\n#[ReturnAttr(\n    new StringAttr('/ok$/')\n)]\nfunction myFunction(): string\n{\n    return 'done ok';\n}\n```\n\n### CallableAttr\n\nAttributes in PHP only support expressions you can use on class constants. Is not possible to directly define dynamic parameters using attributes.\n\nTo avoid this limitation you can use `CallableAttr` attribute which enables to forward parameter resolution to a callable returning a `ParameterInterface` instance.\n\n```php\nuse Chevere\\Parameter\\Interfaces\\ParameterInterface;\nuse Chevere\\Parameter\\Attributes\\CallableAttr;\n\nfunction myCallable(): ParameterInterface\n{\n    return arrayp(\n        email: string(),\n    )-\u003ewithOptional(\n        name: string(),\n    );\n}\n\n#[CallableAttr('myCallable')]\n```\n\n## Types\n\nA Parameter is an object implementing `ParameterInterface`. Every Parameter can define a `description`, `default` value, `sensitive` flag, plus additional validation rules depending on the type.\n\nA Parameter can be defined using functions or attributes, it takes same arguments for both.\n\n### Immutable methods\n\nA 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.\n\n```php\n// Base generic methods\npublic function withDescription(string $description): self;\npublic function withIsSensitive(bool $isSensitive = true): self;\n```\n\nImmutable methods of a Parameter can be used to re-define its rules. For example, `IntParameter` provides additional methods to define integer range:\n\n```php\nuse function Chevere\\Parameter\\int;\n\n$int = int(min: 0, max: 100);\n// Same as:\n$int = int()-\u003ewithMin(0)-\u003ewithMax(100);\n```\n\nMethods unique to each parameter:\n\n* IntParameter: `withMin`, `withMax`, `withAccept`, `withReject`\n* FloatParameter: `withMin`, `withMax`, `withAccept`, `withReject`\n* StringParameter: `withRegex`\n* ArrayParameter: `withRequired`, `withOptional`, `withModify`, `withMakeOptional`, `withMakeRequired`, `without`, `withOptionalMinimum`\n* ObjectParameter: `withClassName`\n* IterableParameter: `withKey`, `withValue`\n\n## String\n\nUse function `string` to create a `StringParameter`. Pass a `regex` for string matching.\n\n```php\nuse function Chevere\\Parameter\\string;\n\n// Any string\n$string = string();\n// String matching bin-\u003cdigits\u003e\n$string = string('/^bin-[\\d]+$/');\n$string('bin-123');\n```\n\nUse `StringAttr` attribute to define a string parameter using attribute notation.\n\n```php\nuse Chevere\\Parameter\\Attributes\\StringAttr;\n\n#[StringAttr('/^bin-[\\d]+$/')]\n```\n\n### String based-parameters\n\nThe following parameters are based on String.\n\n#### Enum string\n\nUse function `enum` to create a `StringParameter` matching a list of strings.\n\n```php\nuse function Chevere\\Parameter\\enum;\n\n$enum = enum('on', 'off');\n$enum('on');\n$enum('off');\n```\n\nUse `EnumAttr` attribute to define an enum string parameter using attribute notation.\n\n```php\nuse Chevere\\Parameter\\Attributes\\EnumAttr;\n\n#[EnumAttr('on', 'off')]\n```\n\n#### Int string\n\nUse function `intString` to create a `StringParameter` matching a string integers.\n\n```php\nuse function Chevere\\Parameter\\intString;\n\n$int = intString();\n$int('100');\n```\n\n#### Bool string\n\nUse function `boolString` to create a `StringParameter` matching `0` and `1` strings.\n\n```php\nuse function Chevere\\Parameter\\boolString;\n\n$bool = boolString();\n$bool('0');\n$bool('1');\n```\n\n#### Date string\n\nUse function `date` to create a `StringParameter` matching `YYYY-MM-DD` strings.\n\n```php\nuse function Chevere\\Parameter\\date;\n\n$date = date();\n$date('2021-01-01');\n```\n\n#### Time string\n\nUse function `time` to create a `StringParameter` matching `hh:mm:ss` strings.\n\n```php\nuse function Chevere\\Parameter\\time;\n\n$time = time();\n$time('12:00:00');\n```\n\n#### Datetime string\n\nUse function `datetime` to create a `StringParameter` matching `YYYY-MM-DD hh:mm:ss` strings.\n\n```php\nuse function Chevere\\Parameter\\datetime;\n\n$datetime = datetime();\n$datetime('2024-01-09 10:53:00');\n```\n\n## Int\n\nUse 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.\n\n```php\nuse function Chevere\\Parameter\\int;\n\n// Any int\n$int = int();\n$int(1);\n// Integer between 0 and 100\n$int = int(min: 0, max: 100);\n$int(50);\n// Integer matching 1, 2 or 3\n$int = int(accept: [1, 2, 3]);\n$int(2);\n// Integer not-matching 1, 2 or 3\n$int = int(reject: [1, 2, 3]);\n$int(4);\n```\n\nUse `IntAttr` attribute to define an integer parameter using attribute notation.\n\n```php\nuse Chevere\\Parameter\\Attributes\\IntAttr;\n\n#[IntAttr(min: 0, max: 100)]\n```\n\n### Int based-parameters\n\nThe following parameters are based on Int.\n\n#### Bool int\n\nUse function `boolInt` to create a `IntParameter` matching `0` and `1` integers.\n\n```php\nuse function Chevere\\Parameter\\boolInt;\n\n$bool = boolInt();\n$bool(0);\n$bool(1);\n```\n\n## Float\n\nUse 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.\n\n```php\nuse function Chevere\\Parameter\\float;\n\n// Any float\n$float = float();\n$float(1.5);\n// Float between 0 and 100\n$float = float(min: 0, max: 100);\n$float(50.5);\n// Float matching 1.5, 2.5 or 3.5\n$float = float(accept: [1.5, 2.5, 3.5]);\n$float(2.5);\n// Float not-matching 1.5, 2.5 or 3.5\n$float = float(reject: [1.5, 2.5, 3.5]);\n$float(4.5);\n```\n\nUse `FloatAttr` attribute to define a float parameter using attribute notation.\n\n```php\nuse Chevere\\Parameter\\Attributes\\FloatAttr;\n\n#[FloatAttr(min: 0, max: 100)]\n```\n\n## Bool\n\nUse function `bool` to create a `BoolParameter`.\n\n```php\nuse function Chevere\\Parameter\\bool;\n\n$bool = bool();\n$bool(true);\n$bool(false);\n```\n\nUse `BoolAttr` attribute to define a bool parameter using attribute notation.\n\n```php\nuse Chevere\\Parameter\\Attributes\\BoolAttr;\n\n#[BoolAttr]\n```\n\n## Null\n\nUse function `null` to create a `NullParameter`.\n\n```php\nuse function Chevere\\Parameter\\null;\n\n$null = null();\n$null(null);\n```\n\nUse `NullAttr` attribute to define a null parameter using attribute notation.\n\n```php\nuse Chevere\\Parameter\\Attributes\\NullAttr;\n\n#[NullAttr]\n```\n\n## Object\n\nUse function `object` to create a `ObjectParameter`. Pass a className for the object class name.\n\n```php\nuse function Chevere\\Parameter\\object;\n\n$object = object(stdClass::class);\n$object(new stdClass());\n```\n\nUse `ObjectAttr` attribute to define an object parameter using attribute notation.\n\n```php\nuse Chevere\\Parameter\\Attributes\\ObjectAttr;\n\n#[ObjectAttr(stdClass::class)]\n```\n\n## Mixed\n\nUse function `mixed` to create a `MixedParameter`.\n\n```php\nuse function Chevere\\Parameter\\mixed;\n\n$mixed = mixed();\n$mixed(1);\n$mixed('1');\n$mixed(true);\n$mixed(null);\n```\n\n## Union\n\nUse function `union` to create a `UnionParameter`. Pass a list of parameters to match, target value must match at least one.\n\n```php\nuse function Chevere\\Parameter\\union;\n\n// Any string or null\n$union = union(string(), null());\n$union('abc');\n$union(null);\n// Any digit string or any integer\n$union = union(\n    intString(),\n    integer()\n);\n$union('100');\n$union(100);\n```\n\nUse `UnionAttr` attribute to define an union parameter using attribute notation.\n\n```php\nuse Chevere\\Parameter\\Attributes\\UnionAttr;\nuse Chevere\\Parameter\\Attributes\\FloatAttr;\nuse Chevere\\Parameter\\Attributes\\IntAttr;\n\n#[UnionAttr(\n    new IntAttr(),\n    new FloatAttr()\n)]\n```\n\n## Array\n\nParameter for type `array` is handled as a composite Parameter holding parameter definition for **each one** of its members.\n\nUse function `arrayp` to create an `ArrayParameter` for named arguments as required array keys.\n\n```php\nuse function Chevere\\Parameter\\arrayp;\n\n// Empty array\n$array = arrayp();\n$array([]);\n// Required 'a' =\u003e \u003cstring\u003e\n$array = arrayp(a: string());\n$array(['a' =\u003e 'Hello world']);\n```\n\nParameter supports nested arrays of any depth:\n\n```php\nuse function Chevere\\Parameter\\arrayp;\nuse function Chevere\\Parameter\\float;\nuse function Chevere\\Parameter\\int;\n\n$array = arrayp(\n    id: int(min: 0),\n    items: arrayp(\n        id: int(min: 0),\n        price: float(min: 0),\n    ),\n);\n$array([\n    'id' =\u003e 1,\n    'items' =\u003e [\n        'id' =\u003e 25,\n        'price' =\u003e 16.5,\n    ]\n]);\n```\n\nUse `ArrayAttr` attribute to define an array parameter using attribute notation.\n\n```php\nuse Chevere\\Parameter\\Attributes\\ArrayAttr;\nuse Chevere\\Parameter\\Attributes\\FloatAttr;\nuse Chevere\\Parameter\\Attributes\\IntAttr;\n\n#[ArrayAttr(\n    id: new IntAttr(),\n    items: new ArrayAttr(\n        id: new IntAttr(),\n        price: new FloatAttr(),\n    ),\n)]\n```\n\n### With required\n\nuse method `withRequired` to define required parameters.\n\n```php\n$array = $array\n    -\u003ewithRequired(\n        username: string(),\n        email: string()\n    );\n```\n\n### With optional\n\nuse method `withOptional` to define optional parameters.\n\n```php\n$array = $array\n    -\u003ewithOptional(address: string());\n```\n\n👉 **Note:** Optional parameters will be validated only if a matching key is provided.\n\n### With modify\n\nuse method `withModify` to define modify parameters.\n\n```php\n$array = $array\n    -\u003ewithModify(\n        username: string('/\\w+/'),\n    );\n```\n\n### With make optional\n\nuse method `withMakeOptional` to make required parameters optional.\n\n```php\n$array = $array\n    -\u003ewithMakeOptional('username');\n```\n\n### With make required\n\nuse method `withMakeRequired` to make optional parameters required.\n\n```php\n$array = $array\n    -\u003ewithMakeRequired('email');\n```\n\n### Without\n\nuse method `without` to remove parameters.\n\n```php\n$array = $array\n    -\u003ewithout('a');\n```\n\n### With optional minimum\n\nuse method `withOptionalMinimum` to define a minimum number of optional parameters. Useful if all parameters are optional but 1.\n\n```php\n$array = $array\n    -\u003ewithOptionalMinimum(1);\n```\n\n### Array based-parameters\n\nThe following parameters are based on Array.\n\n#### Array String\n\nUse function `arrayString` to create an `ArrayStringParameterInterface` for string values. It only supports string parameters.\n\n```php\nuse function Chevere\\Parameter\\arrayString;\nuse function Chevere\\Parameter\\string;\n\n$array = arrayString(\n    test: string(),\n);\n$array(['test' =\u003e 'foo']);\n```\n\n#### File\n\nUse function `file` to create an `ArrayParameter` for file uploads.\n\n```php\nuse function Chevere\\Parameter\\file;\n\n$array = file();\n$file = [\n    'name' =\u003e 'foo.txt',\n    'type' =\u003e 'text/plain',\n    'tmp_name' =\u003e '/tmp/phpYzdqkD',\n    'error' =\u003e 0,\n    'size' =\u003e 123,\n];\n$array($file);\n```\n\nBy default it provides validation for `$_FILES` shape, but you can define your own validation rules. For example, to validate name and contents:\n\n```php\nuse function Chevere\\Parameter\\file;\n\n$array = file(\n    name: string('/^\\.txt$/'),\n    contents: string('/wage-/'),\n);\n$array(\n    'name' =\u003e 'wage-2024.txt',\n    'type' =\u003e 'text/plain',\n    'tmp_name' =\u003e '/tmp/phpYzdqkD',\n    'error' =\u003e 0,\n    'size' =\u003e 27,\n    'contents' =\u003e 'yada yada wage-2024 bla bla',\n);\n```\n\n## Iterable\n\nIterable 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.\n\nUse function `iterable` to create an `IterableParameter`. Pass a `V` and `K` parameters for generic key and value.\n\n```php\nuse function Chevere\\Parameter\\int;\nuse function Chevere\\Parameter\\iterable;\n\n$iterable = iterable(int(min: 0));\n$iterable([0, 1, 2, 3]);\n```\n\nIt also works with named keys:\n\n```php\nuse function Chevere\\Parameter\\int;\nuse function Chevere\\Parameter\\iterable;\nuse function Chevere\\Parameter\\string;\n\n$iterable = iterable(\n    V: arrayp(\n        id: int(min: 0),\n        name: string('^[\\w]{1,255}'),\n    )\n    K: string(),\n);\n$iterable([\n    'based' =\u003e [\n        'id' =\u003e 1,\n        'name' =\u003e 'OscarGangas'\n    ],\n    'fome' =\u003e [\n        'id' =\u003e 2,\n        'name' =\u003e 'BomboFica'\n    ],\n]);\n```\n\n## Helpers\n\n### parameters\n\nUse function `parameters` to create a `Parameters` instance.\n\n```php\nuse function Chevere\\Parameters\\parameters;\nuse function Chevere\\Parameters\\string;\n\n$parameters = parameters(foo: string());\n```\n\n### arguments\n\nUse function `arguments` to create a `Arguments` instance.\n\n```php\nuse function Chevere\\Parameters\\arguments;\nuse function Chevere\\Parameters\\string;\n\n$arguments = arguments($parameters, ['foo' =\u003e 'bar']);\n```\n\n### assertNamedArgument\n\nUse function `assertNamedArgument` to assert a named argument.\n\n```php\nuse function Chevere\\Parameters\\assertNamedArgument;\nuse function Chevere\\Parameters\\int;\nuse function Chevere\\Parameters\\parameters;\n\n$parameter = int(min: 10);\nassertNamedArgument(\n    name: 'foo',\n    parameter: $parameter,\n    argument: 20\n);\n```\n\n### toParameter\n\nUse function `toParameter` to create a `ParameterInterface` instance from a type string. In the example below the resulting `$parameter` will be an `IntParameter`.\n\n```php\nuse function Chevere\\Parameters\\toParameter;\n\n$parameter = toParameter('int');\n```\n\n### arrayFrom\n\nUse function `arrayFrom` to create an [Array parameter](#array) from another array parameter. In the example below the resulting `$array` will contain only `name` and `id` keys as defined in `$source`.\n\n```php\nuse function Chevere\\Parameters\\arrayFrom;\nuse function Chevere\\Parameters\\arrayp;\nuse function Chevere\\Parameters\\int;\nuse function Chevere\\Parameters\\string;\n\n$source = arrayp(\n    id: int(),\n    name: string(),\n    email: string(),\n    age: int(),\n);\n$array = arrayFrom($source, 'name', 'id');\n```\n\n### takeKeys\n\nUse function `takeKeys` to retrieve an array with the keys from a parameter. In the example below `$keys` will contain `id` and `size`.\n\n```php\nuse function Chevere\\Parameters\\arrayp;\nuse function Chevere\\Parameters\\int;\nuse function Chevere\\Parameters\\takeKeys;\n\n$array = arrayp(\n    id: int(),\n    size: int(),\n);\n$keys = takeKeys($array);\n```\n\n### takeFrom\n\nUse function `takeFrom` to retrieve an iterator with the desired keys from a parameter. In the example below `$iterator` will yield `size` and `name` keys.\n\n```php\nuse function Chevere\\Parameters\\arrayp;\nuse function Chevere\\Parameters\\int;\nuse function Chevere\\Parameters\\string;\nuse function Chevere\\Parameters\\takeFrom;\n\n$array = arrayp(\n    id: int(min: 0),\n    size: int(min: 100),\n    name: string(),\n);\n$iterator = takeFrom($array, 'size', 'name');\n```\n\n### parametersFrom\n\nUse function `parametersFrom` to create a `Parameters` with desired keys from a parameter. In the example below `$parameters` will contain `size` and `name` keys.\n\n```php\nuse function Chevere\\Parameters\\arrayp;\nuse function Chevere\\Parameters\\int;\nuse function Chevere\\Parameters\\string;\nuse function Chevere\\Parameters\\parametersFrom;\n\n$array = arrayp(\n    id: int(min: 0),\n    size: int(min: 100),\n    name: string(),\n);\n$parameters = parametersFrom($array, 'size', 'name');\n```\n\n### getParameters\n\nUse function `getParameters` to retrieve a `Parameters` instance from an object implementing either `ParameterAccessInterface` or `ParametersInterface`.\n\n```php\nuse function Chevere\\Parameters\\getParameters;\n\n$parameters = getParameters($object);\n```\n\n### getType\n\nUse function `getType` to retrieve the type as is known by this library.\n\n```php\nuse function Chevere\\Parameters\\getType;\n\n$type = getType(1); // int\n```\n\n### parameterAttr\n\nUse function `parameterAttr` to retrieve an object implementing `ParameterAttributeInterface` from a function or class method parameter.\n\n```php\nuse function Chevere\\Parameters\\parameterAttr;\nuse Chevere\\Parameter\\Attributes\\StringAttr;\n\n\nfunction myFunction(\n    #[StringAttr('/^bin-[\\d]+$/')]\n    string $foo\n): void {\n    // ...\n}\n\n$stringAttr = parameterAttr('foo', 'myFunction');\n$stringAttr('bin-123');\n```\n\n### reflectionToParameters\n\nUse function `reflectionToParameters` to retrieve a `Parameters` instance from a `ReflectionFunction` or `ReflectionMethod` instance.\n\n```php\nuse function Chevere\\Parameter\\reflectionToParameters;\n\n$parameters = reflectionToParameters($reflection);\n```\n\n### reflectionToReturn\n\nUse function `reflectionToReturn` to retrieve a `ParameterInterface` instance from a `ReflectionFunction` or `ReflectionMethod` instance.\n\n```php\nuse function Chevere\\Parameter\\reflectionToReturn;\n\n$parameter = reflectionToReturn($reflection);\n```\n\n### reflectedParameterAttribute\n\nUse function `reflectedParameterAttribute` to retrieve an object implementing `ParameterAttributeInterface` from a `ReflectionParameter` instance.\n\n```php\nuse function Chevere\\Parameter\\reflectedParameterAttribute;\n\n$parameterAttribute = reflectedParameterAttribute($reflectionParameter);\n```\n\n### validated\n\nUse function `validated` to validate a function or method arguments.\n\n```php\nuse function Chevere\\Parameter\\validated;\n\n$result = validated('myFunction', $arg1, $arg2,);\n```\n\n## Examples\n\n### Inline validation\n\n* Validate string starting with \"a\":\n\n```php\nuse function Chevere\\Parameter\\string;\n\n$value = 'ahhh';\nstring('/^a.+/')($value);\n```\n\n* Validate an int of min value `100`:\n\n```php\nuse function Chevere\\Parameter\\int;\n\n$value = 100;\nint(min: 100)($value);\n```\n\n* Validate an int accept list:\n\n```php\nuse function Chevere\\Parameter\\int;\n\n$value = 1;\nint(accept: [1, 2, 3])($value);\n```\n\n* Validate a float reject list:\n\n```php\nuse function Chevere\\Parameter\\float;\n\n$value = 3.1;\nfloat(reject: [1.1, 2.1])($value);\n```\n\n* Validate an array:\n\n```php\nuse function Chevere\\Parameter\\arrayp;\nuse function Chevere\\Parameter\\int;\nuse function Chevere\\Parameter\\string;\n\n$value = [\n    'id' =\u003e 1,\n    'name' =\u003e 'Pepe'\n];\narrayp(\n    id: int(min: 1),\n    name: string('/^[A-Z]{1}\\w+$/')\n)($value);\n```\n\n* Validate an iterable `int` list:\n\n```php\nuse function Chevere\\Parameter\\int;\nuse function Chevere\\Parameter\\iterable;\n\n$value = [1, 2, 3];\niterable(int())($value);\n```\n\n* Validate an iterable int list with string key type rules:\n\n```php\nuse function Chevere\\Parameter\\int;\nuse function Chevere\\Parameter\\iterable;\n\n$value = [\n    'unila' =\u003e 1,\n    'dorila' =\u003e 2,\n    'tirifila' =\u003e 3,\n];\niterable(\n    K: string('/ila$/'),\n    V: int(min: 1)\n)($value);\n```\n\n* Validate an union of type ?int:\n\n```php\nuse function Chevere\\Parameter\\int;\nuse function Chevere\\Parameter\\null;\n\n$value = 1;\nunion(int(), null())($value);\n```\n\n### Attribute delegated validation\n\n* Use function `validated()` to get a return validated against all rules.\n\n```php\nuse function Chevere\\Parameter\\validated;\n\n$result = validated('myFunction', $var);\n```\n\n* Use function `reflectionToParameters()` to get rules for validating arguments.\n\n```php\nuse ReflectionMethod;\nuse Chevere\\Parameter\\Attributes\\IntAttr;\nuse function Chevere\\Parameter\\arguments;\nuse function Chevere\\Parameter\\reflectionToParameters;\n\n$class = new class() {\n    public function wea(\n        #[IntAttr(accept: [1, 10, 100])]\n        int $base\n    ): void {\n    }\n};\n$object = new $class();\n$reflection = new ReflectionMethod($object, 'wea');\n$parameters = reflectionToParameters($reflection);\n$args = ['base' =\u003e 10];\n$parameters(...$args); // valid $args\n$result = $object-\u003ewea(...$args);\n```\n\n* Use function `reflectionToReturn()` to get rules for validating function/method return value:\n\n```php\nuse ReflectionFunction;\nuse Chevere\\Parameter\\Attributes\\IntAttr;\nuse Chevere\\Parameter\\Attributes\\ReturnAttr;\nuse function Chevere\\Parameter\\reflectionToReturn;\n\n$function =\n    #[ReturnAttr(\n        new IntAttr(min: 1000)\n    )]\n    function (int $base): int {\n        return 10 * $base;\n    };\n$reflection = new ReflectionFunction($function);\n$return = reflectionToReturn($reflection);\n$base = 10;\n$result = $function($base);\n$result = $return($result); // Validates result\n```\n\n### Attribute inline validation\n\nUse `valid()` on the function/method body to trigger validation for arguments.\n\n* Validate an string enum for `Hugo`, `Paco`, `Luis`:\n* Validate a min float value of `1000`:\n\n```php\nuse Chevere\\Parameter\\Attributes\\EnumAttr;\nuse function Chevere\\Parameter\\validate;\n\nfunction myEnum(\n    #[EnumAttr('Hugo', 'Paco', 'Luis')]\n    string $name,\n    #[FloatAttr(min: 1000)]\n    float $money\n): void\n{\n    valid();\n    // Or single...\n    valid('name');\n    valid('money');\n}\n$arg1 = 'Paco';\n$arg2 = 1000.50;\nmyEnum($arg1, $arg2);\n```\n\n* Validate an int of any value but `0` and `100`:\n\n```php\nuse Chevere\\Parameter\\Attributes\\IntAttr;\nuse function Chevere\\Parameter\\validate;\n\nfunction myInt(\n    #[IntAttr(reject: [0, 100])]\n    int $id\n): void\n{\n    valid();\n}\n$value = 50;\nmyInt($value);\n```\n\n* Validate a ~~nasty~~ nested array:\n\n```php\nuse Chevere\\Parameter\\Attributes\\ArrayAttr;\nuse Chevere\\Parameter\\Attributes\\IntAttr;\nuse Chevere\\Parameter\\Attributes\\StringAttr;\nuse Chevere\\Parameter\\Attributes\\IterableAttr;\nuse function Chevere\\Parameter\\validate;\n\nfunction myArray(\n    #[ArrayAttr(\n        id: new IntAttr(min: 1),\n        role: new ArrayAttr(\n            mask: new IntAttr(accept: [64, 128, 256]),\n            name: new StringAttr('/[a-z]+/'),\n            tenants: new IterableAttr(\n                new IntAttr(min: 1)\n            )\n        ),\n    )]\n    array $spooky\n): void\n{\n    valid();\n}\n$value = [\n    'id' =\u003e 10,\n    'role' =\u003e [\n        'mask' =\u003e 128,\n        'name' =\u003e 'admin',\n        'tenants' =\u003e [1, 2, 3, 4, 5]\n    ],\n];\nmyArray($value);\n```\n\n* Validate iterable int list:\n\n```php\nuse Chevere\\Parameter\\Attributes\\IntAttr;\nuse Chevere\\Parameter\\Attributes\\IterableAttr;\nuse function Chevere\\Parameter\\validate;\n\nfunction myIterable(\n    #[IterableAttr(\n        new IntAttr(),\n    )]\n    array $list = [0,1,2]\n): void\n{\n    valid();\n}\n```\n\nUse function `returnAttr()` on the function/method body.\n\n* Validate int `min: 0, max: 5` return:\n\n```php\nuse Chevere\\Parameter\\Attributes\\IntAttr;\nuse Chevere\\Parameter\\Attributes\\ReturnAttr;\nuse function Chevere\\Parameter\\returnAttr;\n\n#[ReturnAttr(\n    new IntAttr(min: 0, max: 5)\n)]\npublic function myReturnInt(): int\n{\n    $result = 1;\n\n    return returnAttr()($result);\n}\n```\n\n* Validate array return:\n\n```php\nuse Chevere\\Parameter\\Attributes\\ArrayAttr;\nuse Chevere\\Parameter\\Attributes\\IntAttr;\nuse Chevere\\Parameter\\Attributes\\StringAttr;\nuse Chevere\\Parameter\\Attributes\\ReturnAttr;\nuse function Chevere\\Parameter\\returnAttr;\n\n#[ReturnAttr(\n    new ArrayAttr(\n        id: new IntAttr(min: 0),\n        name: new StringAttr()\n    )\n)]\npublic function myReturnArray(): array\n{\n    $result = [\n        'id' =\u003e 1,\n        'name' =\u003e 'Peoples Hernandez'\n    ];\n\n    return returnAttr()($result);\n}\n```\n\n💡 By convention when omitting `ReturnAttr` the method `public static function return(): ParameterInterface` (if any) will be used to determine return validation rules.\n\n## Documentation\n\nDocumentation is available at [chevere.org](https://chevere.org/packages/parameter).\n\n## License\n\nCopyright [Rodolfo Berrios A.](https://rodolfoberrios.com/)\n\nChevere is licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for the full license text.\n\nUnless 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchevere%2Fparameter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchevere%2Fparameter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchevere%2Fparameter/lists"}