{"id":28324205,"url":"https://github.com/softboxlab/php-array-utils","last_synced_at":"2025-06-24T02:31:34.009Z","repository":{"id":57054865,"uuid":"94487314","full_name":"SoftboxLab/php-array-utils","owner":"SoftboxLab","description":null,"archived":false,"fork":false,"pushed_at":"2017-06-19T21:08:24.000Z","size":61,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-02T02:11:26.272Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/SoftboxLab.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}},"created_at":"2017-06-15T23:45:41.000Z","updated_at":"2017-06-16T12:20:45.000Z","dependencies_parsed_at":"2022-08-24T14:00:25.405Z","dependency_job_id":null,"html_url":"https://github.com/SoftboxLab/php-array-utils","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/SoftboxLab/php-array-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftboxLab%2Fphp-array-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftboxLab%2Fphp-array-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftboxLab%2Fphp-array-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftboxLab%2Fphp-array-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SoftboxLab","download_url":"https://codeload.github.com/SoftboxLab/php-array-utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftboxLab%2Fphp-array-utils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261591680,"owners_count":23181773,"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":[],"created_at":"2025-05-25T17:10:39.465Z","updated_at":"2025-06-24T02:31:34.002Z","avatar_url":"https://github.com/SoftboxLab.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP-ARRAY-UTILS\n[![Build Status](https://travis-ci.org/tarcisiojr/php-cast.svg?branch=master)](https://travis-ci.org/tarcisiojr/php-cast)\n[![codecov](https://codecov.io/gh/tarcisiojr/php-cast/branch/master/graph/badge.svg)](https://codecov.io/gh/tarcisiojr/php-cast)\n[![Latest Stable Version](https://poser.pugx.org/tarcisiojr/php-cast/v/stable)](https://packagist.org/packages/tarcisiojr/php-cast)\n[![Total Downloads](https://poser.pugx.org/tarcisiojr/php-cast/downloads)](https://packagist.org/packages/tarcisiojr/php-cast)\n[![Latest Unstable Version](https://poser.pugx.org/tarcisiojr/php-cast/v/unstable)](https://packagist.org/packages/tarcisiojr/php-cast)\n[![composer.lock](https://poser.pugx.org/tarcisiojr/php-cast/composerlock)](https://packagist.org/packages/tarcisiojr/php-cast)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/f4c39a14-b982-42d4-bd35-90bf660dc49a/mini.png)](https://insight.sensiolabs.com/projects/f4c39a14-b982-42d4-bd35-90bf660dc49a)\n\nHelps to cast values of array of array. Util when it is necessary garantee the type of output value, por exemple, \nencoding of arrays in JSON. Beyond cast values, it is possible format too.\n\n## Installation\n\n```\ncomposer require tarcisiojr/php-cast\n```\n\n## Usage\n\nExample:\n\n```php\nclass Example {\n    public function test() {\n        $value = [\n            \"a\" =\u003e [\n                \"b\" =\u003e [\n                    \"c1\" =\u003e \"a\",\n                    \"c2\" =\u003e \"2\",\n                    \"c3\" =\u003e \"3\",\n                    \"c4\" =\u003e [\"1\", \"2\", \"3\"],\n                    \"c5\" =\u003e [\n                        [\"d1\" =\u003e \"9\"],\n                        [\"d1\" =\u003e \"8\"],\n                        [\"d1\" =\u003e \"7\"],\n                    ]\n                ]\n            ]\n        ];\n    \n        // 1. Create a instance of CastHelper\n        // 2. Configure the rules to cast values\n        // 3. Execute method 'cast' to cast array values\n        \n        $ret = (new CastHelper())\n            -\u003eaddRule(\"a.b.c1\", \"string\")\n            -\u003eaddRule(\"a.b.c2\", \"int\")\n            -\u003eaddRule(\"a.b.c3\", \"float\")\n            -\u003eaddRule(\"a.b.c4.*\", \"int\")\n            -\u003eaddRule(\"a.b.c5.*.d1\", \"string|lpad:2,0\")\n            -\u003ecast($value);\n\n        echo \"\\n\\n\" . json_encode($ret, JSON_PRETTY_PRINT);\n    }\n}\n\n```\n\nOutput:\n\n```\n{\n    \"a\": {\n        \"b\": {\n            \"c1\": \"a\",\n            \"c2\": 2,\n            \"c3\": 3,\n            \"c4\": [\n                1,\n                2,\n                3\n            ],\n            \"c5\": [\n                {\n                    \"d1\": \"09\"\n                },\n                {\n                    \"d1\": \"08\"\n                },\n                {\n                    \"d1\": \"07\"\n                }\n            ]\n        }\n    }\n}\n```\n\n### Path Expression\n\n* a.b selects \"value\": \n```php\n    [ \n        \"a\" =\u003e [\n            \"b\" =\u003e \"value\"\n        ]\n    ]; \n```\n\n* a.2.b selects \"two\": \n```php\n    [ \n        \"a\" =\u003e [\n            [ \"b\" =\u003e \"zero\" ],\n            [ \"b\" =\u003e \"one\" ],\n            [ \"b\" =\u003e \"two\" ],\n            [ \"b\" =\u003e \"three\" ]\n        ]\n    ]; \n```\n\n* a.*.b selects all values \"zero, \"one\", \"two\" and \"three\": \n```php\n    [ \n        \"a\" =\u003e [\n            [ \"b\" =\u003e \"zero\" ],\n            [ \"b\" =\u003e \"one\" ],\n            [ \"b\" =\u003e \"two\" ],\n            [ \"b\" =\u003e \"three\" ]\n        ]\n    ]; \n```\n\n\n### Rule Expression\n\nExpression: cast_type|option_1:param_1,param_2,...param_n;option_2:param_1...;option_n... \n\nWhere cast_type is the identifier of cast rule, options are aditional configurations do be executed against the value, for example, trim, pad, etc.\n\nCast Types:\n\n* int: casts to a int value.\n* float: casts to a float point value.\n* bool: casts to a boolean value.\n* string: casts to a string value.\n    - max_length:size truncate the value at max length.\n    - rpad:size,char pad at right position with supplied char.\n    - lpad:size,char pad at left position with supplied char.\n\n## Extending\n\nIt is possible to add new cast rules, for this it will be necessary create a class the extends ```CastRule``` interface \nand register it with ```PHP\\Cast\\CastRule\\CastRuleFactory::registerCastRule``` method. You can use the \n```PHP\\Cast\\CastRule\\CastRuleBase``` to shorten the path. See example above:\n\n```php\nclass BooleanCastRule extends PHP\\Cast\\CastRule\\CastRuleBase {\n    public function getIdentifier() {\n        return \"bool\";\n    }\n\n    public function cast($value) {\n        return (boolean) $value;\n    }\n} \n\n...\n\nPHP\\Cast\\CastRule\\CastRuleFactory::registerCastRule(new BooleanCastRule());\n\n...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftboxlab%2Fphp-array-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoftboxlab%2Fphp-array-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftboxlab%2Fphp-array-utils/lists"}