{"id":31863815,"url":"https://github.com/beeblebrox3/caster","last_synced_at":"2025-10-12T18:57:26.037Z","repository":{"id":56950626,"uuid":"111270816","full_name":"beeblebrox3/caster","owner":"beeblebrox3","description":"PHP Library to cast arrays","archived":false,"fork":false,"pushed_at":"2023-06-08T22:36:44.000Z","size":36,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-02T09:27:55.326Z","etag":null,"topics":["library","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beeblebrox3.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-19T06:35:21.000Z","updated_at":"2023-06-08T22:36:50.000Z","dependencies_parsed_at":"2022-08-21T03:40:17.490Z","dependency_job_id":null,"html_url":"https://github.com/beeblebrox3/caster","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/beeblebrox3/caster","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beeblebrox3%2Fcaster","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beeblebrox3%2Fcaster/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beeblebrox3%2Fcaster/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beeblebrox3%2Fcaster/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beeblebrox3","download_url":"https://codeload.github.com/beeblebrox3/caster/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beeblebrox3%2Fcaster/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279012518,"owners_count":26085135,"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","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["library","php"],"created_at":"2025-10-12T18:57:24.968Z","updated_at":"2025-10-12T18:57:26.030Z","avatar_url":"https://github.com/beeblebrox3.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=beeblebrox3_caster\u0026metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=beeblebrox3_caster)\n[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=beeblebrox3_caster\u0026metric=security_rating)](https://sonarcloud.io/summary/new_code?id=beeblebrox3_caster)\n[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=beeblebrox3_caster\u0026metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=beeblebrox3_caster)\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=beeblebrox3_caster\u0026metric=coverage)](https://sonarcloud.io/summary/new_code?id=beeblebrox3_caster)\n\n# Caster\n\nPHP Library to cast arrays of values\n\n## Requirements\n\n- PHP 7\n\n## Usage\n\nBasic example:\n\n```php\n$types = [\n    'must_be_integer' =\u003e 'integer',\n];\n$input = [\n    'must_be_integer' =\u003e '0.9',\n];\n\n$caster = new Beeblebrox3\\Caster\\Caster();\n$res = $caster-\u003ecast($types, $input);\n\n// $res will be ['must_be_integer' =\u003e 0]\n\n```\n\nThe `$types` parameter specifies how the `$input` values should be transformed.\nYou do this specifying an array of rules to be applyied. A rule is identified by a string.\n\n```php\n$types = [\n    'a' =\u003e 'integer',\n    'b' =\u003e 'string',\n    'c' =\u003e 'bool',\n]\n```\n\nYou can also apply multiple rules to the same value:\n\n```php\n$types = [\n    'a' =\u003e 'integer',\n    'b' =\u003e 'string|lpad:10,0', // will cast to string and after apply a left string pad\n]\n```\n\nRules can have arguments:\n\n```php\n$types = [\n    // string up to 60 characters\n    'a' =\u003e 'string:60',\n\n    // will cast to float and then round with precision of two specifying the mode in which rounding occurs\n    // (see https://php.net/round for details)\n    'b' =\u003e 'float:2,' . PHP_ROUND_HALF_DOWN,\n];\n```\n\nYou can use nested arrays too:\n\n```php\n$res = $caster-\u003ecast(\n    ['level1.level2.level3.key' =\u003e 'integer'],\n    ['level1' =\u003e ['level2' =\u003e ['level3' =\u003e ['key' =\u003e '999']]]]\n);\n\n// $res wil be ['level1' =\u003e ['level2' =\u003e ['level3' =\u003e ['key' =\u003e 999]]]]\n```\n\n### Available rules\n\n\u003e to pass options you don't use their names, but pass the values in the displayed order. Example: ` 'a' =\u003e 'bool|1'`.\n\u003e arguments with `*` are mandatory\n\n| Rule        | Arguments                              | Details                                                                                                      |\n|-------------|----------------------------------------|--------------------------------------------------------------------------------------------------------------|\n| bool        | `nullIfEmpty`*                         | cast to boolean. If `nullIfEmpty` is `1`, cast empty strings and null to null, else cast to `false`          |\n| date_format | `output format`* \u003cbr /\u003e `input format` | format the given date to an specific format. You can also specify the format of the input date               |\n| float       | `precision` \u003cbr /\u003e `round mode`        | cast to float and optionally round the value using `precision` and `round mode` (using the `round` function) |\n| integer     |                                        | cast to integer                                                                                              |\n| lpad        | `length` * \u003cbr /\u003e `str` *              | pad the string on the left site to `length` length using `str` to fill                                       |\n| rpad        | `length` * \u003cbr /\u003e `str` *              | pad the string on the right site to `length` length using `str` to fill                                      |\n| string      | `max length`                           | casto so string, optionally limiting the string size to `max length` (using `substr`)                        |\n\n\n### Custom Rules\n\nYou can create your own rules with a class implementing the `Beeblebrox3\\Caster\\Rules\\IRule` interface:\n\n```php\n\u003c?php\n\nuse Beeblebrox3\\Caster\\Rules\\IRule;\nuse Beeblebrox3\\Caster\\Caster;\n\nclass PipeRule implements IRule {\n  public function handle($value, ...$args) {\n    return $value;\n  }\n}\n\n$caster = new Caster();\n$caster-\u003eaddCustomRule('pipe', PipeRule::class);\n\n$res = $caster-\u003ecast(['a' =\u003e 'pipe'], ['a' =\u003e '199']);\n```\n\nThe `PipeRule` will only return the same input.\n\n#### Function as custom  rule\n\u003e new on version `0.1.0`\n\n```php\n$caster = new Caster();\n$caster-\u003eaddCustomrule('pipe', function ($value) { return $value; });\n```\n\nNow you created the same pipe rule as before, but without the class.\n\n## Changelog\n- `0.1.0` Add support functions as custom rules;\n- `0.0.2` Add custom rules support;\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeeblebrox3%2Fcaster","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeeblebrox3%2Fcaster","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeeblebrox3%2Fcaster/lists"}