{"id":15728631,"url":"https://github.com/atayahmet/laravel-castable","last_synced_at":"2025-03-13T03:30:39.431Z","repository":{"id":62489070,"uuid":"83083069","full_name":"atayahmet/laravel-castable","owner":"atayahmet","description":"Clarify types of all inputs","archived":false,"fork":false,"pushed_at":"2017-03-02T20:52:09.000Z","size":32,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-26T18:23:43.737Z","etag":null,"topics":["casting","filter","input","laravel","presenter","querystrings","sanitization"],"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/atayahmet.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-02-24T21:12:12.000Z","updated_at":"2019-09-19T12:39:00.000Z","dependencies_parsed_at":"2022-11-02T09:31:13.642Z","dependency_job_id":null,"html_url":"https://github.com/atayahmet/laravel-castable","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atayahmet%2Flaravel-castable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atayahmet%2Flaravel-castable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atayahmet%2Flaravel-castable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atayahmet%2Flaravel-castable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/atayahmet","download_url":"https://codeload.github.com/atayahmet/laravel-castable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243335037,"owners_count":20274895,"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":["casting","filter","input","laravel","presenter","querystrings","sanitization"],"created_at":"2024-10-03T23:05:05.289Z","updated_at":"2025-03-13T03:30:39.127Z","avatar_url":"https://github.com/atayahmet.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/atayahmet/laravel-castable.svg?branch=master)](https://travis-ci.org/atayahmet/laravel-castable) [![Total Downloads](https://poser.pugx.org/atayahmet/laravel-castable/downloads)](https://packagist.org/packages/atayahmet/laravel-castable) [![License](https://poser.pugx.org/atayahmet/laravel-castable/license)](https://packagist.org/packages/atayahmet/laravel-castable) [![Latest Stable Version](https://poser.pugx.org/atayahmet/laravel-castable/v/stable)](https://packagist.org/packages/atayahmet/laravel-castable)\n\n# Laravel Castable\n\n**Laravel Castable** package is a type converter, input filter or sanitizer. It is possible to do all of these operations. Supported `POST`, `RAW DATA`, `GET` requests methods. We started by inspiring the Laravel Eloquent data cast.\n\n## Requirements\n\nPHP 5.6, 7.0+\nLaravel 5.3 (LTS) or Laravel 5.4 (Current)\n\n\n## Get Started\n\nFirstly, we install package:\n\n```sh\n$ composer require atayahmet/laravel-castable\n```\n\nand then we need add the service provider to the app.php\n\n```php\nCastable\\CastableServiceProvider::class\n```\n\nOK, we done.\n\nLet's see how to use the laravel-castable.\n\n### Castable types\n\n| Types  |\n|-------|\n| string |\n| integer |\n| boolean |\n| float |\n| double |\n| real |\n| unset |\n| array |\n| object (stdClass) |\n| collection |\n\n\n## Create Castable Form Request class\n\nWe created new artisan command that inspired `make:request` from laravel built in command.\n\n```sh\n$ php artisan make:cast ContactRequest\n```\n\n**New form of the form request class:**\n\n```php\n\n\u003c?php\n\nnamespace App\\Http\\Requests;\n\nuse Castable\\Castable;\n\nclass ContactRequest extends Castable\n{\n    protected $casts = [\n        'json' =\u003e [\n            //\n        ],\n        'post'  =\u003e [\n            'name' =\u003e 'string',\n            'age' =\u003e 'integer',\n            'student' =\u003e 'boolean',\n            'interests' =\u003e 'collection'\n        ],\n        'query' =\u003e [\n            'save' =\u003e 'boolean'\n        ]\n    ];\n\n    /**\n     * Determine if the user is authorized to make this request.\n     *\n     * @return bool\n     */\n    public function authorize()\n    {\n        return false;\n    }\n\n    /**\n     * Get the validation rules that apply to the request.\n     *\n     * @return array\n     */\n    public function rules()\n    {\n        return [\n            //\n        ];\n    }\n}\n```\n\nWe added four inputs to casts property, status attibute added to query string parameters and age, student and interests attributes added to post parameters.\n\n### Raw and converted type of the attributes\n\n**Post:**\n\n| Name  | Value | Type | Cast Type |\n|-------|-------|------| -----------|\n| name  | Ali | string  | string |\n| age   | 19  | string  | integer |\n| student | true   | string  | boolean |\n| interests | books, computers   | array  | collection |\n\n**Query String:**\n\n| Name  | Value | Type | Cast Type |\n|-------|-------|------| ----------|\n| save  | true  | string| boolean |\n\nTo get the above result for:\n\n```php\n\u003c?php\n\nContactController extends Controller {\n\n  public index(ContactRequest $contactRequest)\n  {\n      $contactRequest-\u003ecast()-\u003einput();\n  }\n}\n```\n\n**Get a input:**\n\n```php\n$contactRequest-\u003ecast()-\u003einput('interests'); // collection\n\n$contactRequest-\u003ecast()-\u003einput('student') // boolean (true)\n\n$contactRequest-\u003ecast()-\u003einput('save') // boolean (true)\n````\n\nif request is post raw data:\n\n```php\n$contactRequest-\u003ecast()-\u003ejson();\n\n$contactRequest-\u003ecast()-\u003ejson('age');\n````\n\n**Get original inputs:**\n\n```php\n$contactRequest-\u003einput();\n```\n\n**Get original an input:**\n\n```php\n$contactRequest-\u003einput('student'); // string (true)\n```\n\n**Original raw data:**\n\n```php\n$contactRequest-\u003ejson();\n```\n\n## Add presenter to the inputs\n\nYou can add presenter to the all post, query and json inputs. This feature gives you a chance to filter  inputs.\n\nAdd presenter for post parameters:\n\n```php\n\npublic function PostNameAttribute($value, $original)\n{\n    return ucfirst($value);\n}\n````\n\nAdd presenter for query string parameters:\n\n```php\n\npublic function QuerySaveAttribute($value, $original)\n{\n    return $value === true ? 1 : 0;\n}\n````\n\nAdd presenter for json raw data parameters:\n\n```php\n\npublic function JsonSaveAttribute($value, $original)\n{\n    return ucfirst($name);\n}\n````\n\n## License\n\nThis package is open-source software licensed under the [MIT license](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatayahmet%2Flaravel-castable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fatayahmet%2Flaravel-castable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatayahmet%2Flaravel-castable/lists"}