{"id":21874707,"url":"https://github.com/gustavonecore/sanitizer","last_synced_at":"2025-07-23T00:34:21.569Z","repository":{"id":62513532,"uuid":"78076042","full_name":"gustavonecore/sanitizer","owner":"gustavonecore","description":"PHP sanitizer and validator","archived":false,"fork":false,"pushed_at":"2022-10-06T21:35:27.000Z","size":33,"stargazers_count":5,"open_issues_count":5,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-15T01:40:00.134Z","etag":null,"topics":["library","php","php-sanitizer","sanitizer"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gustavonecore.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-05T03:35:32.000Z","updated_at":"2022-08-25T02:27:00.000Z","dependencies_parsed_at":"2022-11-02T13:15:40.213Z","dependency_job_id":null,"html_url":"https://github.com/gustavonecore/sanitizer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gustavonecore/sanitizer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gustavonecore%2Fsanitizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gustavonecore%2Fsanitizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gustavonecore%2Fsanitizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gustavonecore%2Fsanitizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gustavonecore","download_url":"https://codeload.github.com/gustavonecore/sanitizer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gustavonecore%2Fsanitizer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266597010,"owners_count":23953894,"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-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"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","php-sanitizer","sanitizer"],"created_at":"2024-11-28T07:13:07.715Z","updated_at":"2025-07-23T00:34:21.549Z","avatar_url":"https://github.com/gustavonecore.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"GCore Sanitizer\n===============\n\n## PHP sanitizer and validator\nThis is a library to sanitize your input from any source, using a predefined template of native types.\n\n**Why another sanitizer?**\nWell, this is because I really want to maintain my toolset pretty small, and not depending on big libraries/frameworks.\n\n### Requirements\n\n- PHP \u003e= v7.0\n\n### Install it with composer\n\n    composer require gustavonecore/php-sanitizer\n\n### Example of usage\n\n**Template definition**\nFirst, you must define your template\n\n```php\n    $filter = new Gcore\\Sanitizer\\Template\\TemplateSanitizer([\n    \t'first_name' =\u003e 'string',\n    \t'dob' =\u003e 'int',\n    \t'numbers' =\u003e 'int[]',\n    \t'test' =\u003e 'int',\n    \t'email' =\u003e 'email',\n    \t'email_wrong' =\u003e 'email',\n    \t'double' =\u003e 'double',\n    \t'boolean' =\u003e 'bool',\n    \t'datetime' =\u003e 'datetime',\n    \t\"persons[]\" =\u003e [\n    \t\t'name' =\u003e 'string',\n    \t\t'eyes' =\u003e 'int',\n    \t\t'address' =\u003e 'string[]',\n    \t\t'commits[]' =\u003e [\n    \t\t\t'hash' =\u003e 'string',\n    \t\t\t'n_comments' =\u003e 'int',\n    \t\t\t'users' =\u003e 'string[]'\n    \t\t],\n    \t],\n    ]);\n```\n\n**Required fields**\nYou can force the sanitization process to require fields, you just need to include a `!` at the end of the rule, e.g:\n\n```php\n    $filter = new Gcore\\Sanitizer\\Template\\TemplateSanitizer([\n    \t'email' =\u003e 'email!',\n    \t'phone' =\u003e 'string!',\n    \t'first_name' =\u003e 'string',\n    ]);\n```\n\n**If required fields is invalid**\nIf the input have required fileds with invalid data (null) the library will throw a `Gcore\\Sanitizer\\Template\\RequiredFieldsException`\n\n**Sanitize!**\nAfter that, you are good to sanitize any input\n\n```php\n    // This will be your inut body from an user\n    $input = [\n    \t'first_name' =\u003e 'Gustavo',\n    \t'dob' =\u003e '10',\n    \t'numbers' =\u003e [1,2,3,'4','5'],\n    \t'foo' =\u003e [1,2,4],\n    \t'email' =\u003e 'gustavo.uach@gmail.com',\n    \t'email_wrong' =\u003e 'gustavo.uachgmail.com',\n    \t'double' =\u003e '7876',\n    \t'boolean' =\u003e true,\n    \t'datetime' =\u003e '2017-11-11 13:50:10',\n    \t'persons' =\u003e [\n    \t\t[\n    \t\t\t'name' =\u003e 'jhon',\n    \t\t\t'eyes' =\u003e 2,\n    \t\t\t'address' =\u003e ['foo', 'bar', 'text'],\n    \t\t\t'commits' =\u003e [\n    \t\t\t\t'hash' =\u003e '2221321n3kj12n3kj12n32j1',\n    \t\t\t\t'n_comments' =\u003e 900,\n    \t\t\t\t'users' =\u003e ['jhon', 'doe'],\n    \t\t\t],\n    \t\t],\n    \t\t\t\t[\n    \t\t\t'name' =\u003e 'albert',\n    \t\t\t'eyes' =\u003e 'wrong int here',\n    \t\t\t'address' =\u003e ['a', 'b', 1],\n    \t\t\t'commits' =\u003e null,\n    \t\t],\n    \t]\n    ];\n\n    // All your data is clean now! awesome!\n    $cleanOutput = $filter-\u003esanitize($input);\n\n    print_r($cleanOutput);\n```\n\n\n\n**Clean Output**\n\nOutput of the previous call\n\n```php\n    php examples/index.php\n    Array\n    (\n        [first_name] =\u003e Gustavo\n        [dob] =\u003e 10\n        [numbers] =\u003e Array\n            (\n                [0] =\u003e 1\n                [1] =\u003e 2\n                [2] =\u003e 3\n                [3] =\u003e 4\n                [4] =\u003e 5\n            )\n\n        [test] =\u003e\n        [email] =\u003e gustavo.uach@gmail.com\n        [email_wrong] =\u003e\n        [double] =\u003e 7876\n        [boolean] =\u003e 1\n        [datetime] =\u003e DateTimeImmutable Object\n            (\n                [date] =\u003e 2017-11-11 13:50:10.000000\n                [timezone_type] =\u003e 3\n                [timezone] =\u003e America/Santiago\n            )\n\n        [persons] =\u003e Array\n            (\n                [0] =\u003e Array\n                    (\n                        [name] =\u003e jhon\n                        [eyes] =\u003e 2\n                        [address] =\u003e Array\n                            (\n                                [0] =\u003e foo\n                                [1] =\u003e bar\n                                [2] =\u003e text\n                            )\n\n                        [commits] =\u003e Array\n                            (\n                                [hash] =\u003e 2221321n3kj12n3kj12n32j1\n                                [n_comments] =\u003e 900\n                                [users] =\u003e Array\n                                    (\n                                        [0] =\u003e jhon\n                                        [1] =\u003e doe\n                                    )\n\n                            )\n\n                    )\n\n                [1] =\u003e Array\n                    (\n                        [name] =\u003e albert\n                        [eyes] =\u003e\n                        [address] =\u003e Array\n                            (\n                                [0] =\u003e a\n                                [1] =\u003e b\n                                [2] =\u003e 1\n                            )\n\n                        [commits] =\u003e\n                    )\n\n            )\n\n    )\n```\n\n**Notes**\n\n - Not defined keys in the template will be **ignored** by the sanitizer.\n - If a value doesn't match the desired type, will be returned as **null**\n\n\n**TODO**\n\n - [x] Add unit tests. #1 (In progress)\n - [x] Add required fields.\n - [x] Create a new template method to define **required** fields.\n   - [x] Improve this method to allow nested fields\n - [ ] Modularize a bit more the Strategy selector to allow extending the library with new types of data.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgustavonecore%2Fsanitizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgustavonecore%2Fsanitizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgustavonecore%2Fsanitizer/lists"}