{"id":27091693,"url":"https://github.com/dgame/php-cast","last_synced_at":"2025-04-06T07:53:28.340Z","repository":{"id":42364859,"uuid":"405186722","full_name":"Dgame/php-cast","owner":"Dgame","description":null,"archived":false,"fork":false,"pushed_at":"2022-08-07T20:29:29.000Z","size":55,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-20T10:47:53.013Z","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/Dgame.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":"2021-09-10T19:06:44.000Z","updated_at":"2023-03-05T03:25:21.000Z","dependencies_parsed_at":"2022-09-02T21:20:36.007Z","dependency_job_id":null,"html_url":"https://github.com/Dgame/php-cast","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dgame%2Fphp-cast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dgame%2Fphp-cast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dgame%2Fphp-cast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dgame%2Fphp-cast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Dgame","download_url":"https://codeload.github.com/Dgame/php-cast/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247451629,"owners_count":20940946,"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-04-06T07:53:27.659Z","updated_at":"2025-04-06T07:53:28.328Z","avatar_url":"https://github.com/Dgame.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Type Assumptions \u0026 Assertions simplified\n\nHave you ever had to validate user data? Probably you have used something like [webmozarts/assert](https://github.com/webmozarts/assert):\n\n```php\n$id = $data['id'] ?? null;\nAssert::integer($id);\n```\n\nThe problem is, even though we checked that `$id` must be an `int`, it is actually still seen as `mixed` (see [this example](https://phpstan.org/r/dca4ad02-603d-4fdc-814b-1cdfcfe508e7) for phpstan).\nTo change this, you need to write / use your own phpstan rule that makes phpstan believe that it will now always be an `int`.\nSo if you use your own verification methods, you must also write / use your own phpstan rules.\n\nThis package tries to simplify that. To verify that something is an `int`, you can _assume_ that it must be an `int`. If it is not, you get `null`:\n\n```php\nuse function Dgame\\Cast\\Assume\\int;\n\n$id = int($data['id'] ?? null);\n```\n\nWith this, `$id` is of type `int|null` for phpstan, psalm, phpstorm and so on.\n\nIf you want to _assert_ that it is an `int`, you can do that too by using:\n\n```php\nuse function Dgame\\Cast\\Assert\\int;\n\n$id = int($data['id'] ?? null);\n```\n\nNow `$id` is of type `int` or it fails with an `AssertionError`. A message for the `AssertionError` can also be optionally set:\n\n```php\nuse function Dgame\\Cast\\Assert\\int;\n\n$id = int($data['id'] ?? null, message: 'The id of the given user must be of type int');\n```\n\nYou can do that for [int](#int), [float](#float), [bool](#bool), [string](#string), [number](#number), [scalar](#scalar) and [array](#array) values.\n\n----\n\n# int\n\n### intify\n\nWith `intify` you get either the `int`-value or the `int`-casted `scalar` value, if any:\n\n```php\nuse function Dgame\\Cast\\Assume\\intify;\n\n$id = intify($data['id'] ?? null); // $id is of type int|null\n```\n\n### unsigned\n\n`unsigned` will return a non-null value, if the given value is a [number](#number) that is \u003e= 0.\n\n```php\nuse function Dgame\\Cast\\Assume\\unsigned;\n\n$id = unsigned($data['id'] ?? null); // $id is of type int|null and \u003e= 0 if it is an int\n```\n\n### positive\n\n`positive` will return a non-null value, if the given value is a [number](#number) that is \u003e 0.\n\n```php\nuse function Dgame\\Cast\\Assume\\positive;\n\n$id = positive($data['id'] ?? null); // $id is of type int|null and \u003e 0 if it is an int\n```\n\n### negative\n\n`negative` will return a non-null value, if the given value is a [number](#number) that is \u003c 0.\n\n```php\nuse function Dgame\\Cast\\Assume\\negative;\n\n$id = negative($data['id'] ?? null); // $id is of type int|null and \u003c 0 if it is an int\n```\n\n# float\n\n### floatify\n\nWith `floatify` you get either the `float`-value or the `float`-casted `scalar` value, if any:\n\n```php\nuse function Dgame\\Cast\\Assume\\float;\n\n$money = float($data['money'] ?? null); // $money is of type float|null\n```\n\n# bool\n\nWith `bool` you get the bool-value for `true`, `false`, `1`, `0`, `on`, `off`, `yes`, `no` or null.\n\n```php\nuse function Dgame\\Cast\\Assume\\bool;\n\n$checked = bool($data['checked'] ?? null); // $checked is of type bool|null\n```\n\n### boolify\n\nWith `boolify` you get either the `bool`-value or the `bool`-casted `scalar` value, if any:\n\n```php\nuse function Dgame\\Cast\\Assume\\boolify;\n\n$checked = boolify($data['checked'] ?? null); // $checked is of type bool|null\n```\n\n# string\n\n### stringify\n\nWith `stringify` you get either the `string`-value or the `string`-casted `scalar` value, if any:\n\n```php\nuse function Dgame\\Cast\\Assume\\stringify;\n\n$value = stringify($data['value'] ?? null); // $value is of type string|null\n```\n\n# number\n\nWith `number` you get either the `int` or `float`-value or null, if it is neither.\n\n```php\nuse function Dgame\\Cast\\Assume\\number;\n\n$range = number($data['range'] ?? null); // $range is of type int|float|null\n```\n\n# scalar\n\nWith `scalar` you get either the `int`, `float`, `bool` or `string`-value or null, if it is neither.\n\n```php\nuse function Dgame\\Cast\\Assume\\scalar;\n\n$value = scalar($data['value'] ?? null); // $value is of type int|float|bool|string|null\n```\n\n# array\n\nWith `collection` you can test whether your value is an `array`:\n\n```php\nuse function Dgame\\Cast\\Assume\\collection;\n\n$values = collection($data['values'] ?? null); // $values is of type array\u003cint|string, mixed\u003e|null\n```\n\nAnd with `collectionOf` you can test whether your value is an `array` of type `T`:\n\n```php\nuse function Dgame\\Cast\\Assume\\collectionOf;\n\n$values = collectionOf('Dgame\\Cast\\Assume\\int', $data['values'] ?? null); // $values is of type array\u003cint|string, int\u003e|null\n```\n\nIf **not** all values in the `array` are of type `int`, you get `null`. If you just want to filter the non-`int` values, you can do that by using `filter`:\n\n```php\nuse function Dgame\\Cast\\Collection\\filter;\n\n$values = filter('Dgame\\Cast\\Assume\\int', $data['values'] ?? []); // $values is of type array\u003cint|string, int\u003e\n```\n\nBut be aware that `filter` expects an `array\u003cint|string, mixed\u003e` as input and not `mixed`!\n\n### list\n\nIf you want to make sure, that you have a `list` of values (and not an assoc. array) you can use `listOf`:\n\n```php\nuse function Dgame\\Cast\\Assume\\listOf;\n\n$values = listOf('Dgame\\Cast\\Assume\\int', $data['values'] ?? null); // $values is of type int[]|null or, to be more accurate, of type array\u003cint, int\u003e|null\n```\n\n### map\n\nIf you want to make sure, that you have an assoc. array (and not a `list`) you can use `mapOf`:\n\n```php\nuse function Dgame\\Cast\\Assume\\mapOf;\n\n$values = mapOf('Dgame\\Cast\\Assume\\int', $data['values'] ?? null); // $values is of type array\u003cstring, int\u003e|null\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgame%2Fphp-cast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdgame%2Fphp-cast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgame%2Fphp-cast/lists"}