{"id":16119971,"url":"https://github.com/uuf6429/php-castable","last_synced_at":"2026-02-14T07:32:22.347Z","repository":{"id":62549334,"uuid":"484853267","full_name":"uuf6429/php-castable","owner":"uuf6429","description":"🎭 Basic type-casting functionality for PHP.","archived":false,"fork":false,"pushed_at":"2024-06-04T05:40:49.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-15T21:38:34.270Z","etag":null,"topics":["casting","php","php-library","types","uuf6429"],"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/uuf6429.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-23T20:48:02.000Z","updated_at":"2024-06-04T05:40:52.000Z","dependencies_parsed_at":"2024-06-04T06:54:56.539Z","dependency_job_id":null,"html_url":"https://github.com/uuf6429/php-castable","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/uuf6429/php-castable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uuf6429%2Fphp-castable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uuf6429%2Fphp-castable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uuf6429%2Fphp-castable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uuf6429%2Fphp-castable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uuf6429","download_url":"https://codeload.github.com/uuf6429/php-castable/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uuf6429%2Fphp-castable/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29439504,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-14T07:24:13.446Z","status":"ssl_error","status_checked_at":"2026-02-14T07:23:58.969Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["casting","php","php-library","types","uuf6429"],"created_at":"2024-10-09T20:55:42.778Z","updated_at":"2026-02-14T07:32:22.328Z","avatar_url":"https://github.com/uuf6429.png","language":"PHP","readme":"# 🎭 PHP Castable\n\n[![CI](https://github.com/uuf6429/php-castable/actions/workflows/ci.yml/badge.svg)](https://github.com/uuf6429/php-castable/actions/workflows/ci.yml)\n[![codecov](https://codecov.io/gh/uuf6429/php-castable/branch/main/graph/badge.svg)](https://codecov.io/gh/uuf6429/php-castable)\n[![Minimum PHP Version](https://img.shields.io/badge/php-%5E8-8892BF.svg)](https://php.net/)\n[![License](https://poser.pugx.org/uuf6429/php-castable/license)](https://packagist.org/packages/uuf6429/php-castable)\n[![Latest Stable Version](https://poser.pugx.org/uuf6429/php-castable/v)](https://packagist.org/packages/uuf6429/php-castable)\n[![Latest Unstable Version](https://poser.pugx.org/uuf6429/php-castable/v/unstable)](https://packagist.org/packages/uuf6429/php-castable)\n\nBasic groundwork for type-casting in PHP.\n\n## 🔌 Installation\nThe recommended and easiest way to install this library is through Composer:\n\n```shell\ncomposer require uuf6429/php-castable\n```\n\n## ⭐️ Features / Functionality\n\n- Works with simple types and objects\n- `cast($value, $type)` function that converts a value to a target type.\n- `Castable` interface, exposes method that is called whenever `cast()` is called on objects implementing this interface.\n- Error handling - all errors routed to `NotCastableException`.\n- Fixes type-hinting for IDEs understanding PHPDoc Generics.\n\nWhile `cast()` is just a regular PHP function, it would be the equivalent to type-casting operators in other languages (e.g. `val as Type`, `(Type)val`, `val.to(Type)`, `CAST(val, TYPE)`...).\n\n## 🚀 Example\n\n```php\nclass Cat implements \\uuf6429\\Castable\\Castable\n{\n    public function castTo($type)\n    {\n        if ($type === Dog::class) {\n            return new Dog();\n        }\n\n        throw new RuntimeException(\"Unsupported type $type.\");\n    }\n}\n\nclass Dog {}\n\n$dog = \\uuf6429\\Castable\\cast(new Cat(), Dog::class); // ok, cat becomes a dog :)\n$cat = \\uuf6429\\Castable\\cast($dog, Cat::class);      // not allowed\n```\n\n## 🔍 Casting Behaviour\n\nThe casting process follows these steps:\n1. If the value is an object or value of the desired type, then it is returned unchanged.\n2. If the value is an *object* that *implements `Castable` interface*, `castTo()` is called and its value returned.\n3. Otherwise, PHP's `settype()` is attempted.\n\nAt any point in time, errors or unsupported type-casting could occur, in which case a `NotCastableException` is thrown.\n\n## 💰 Motivation\n\nIn many cases, having specific `castToX()` methods in your classes is enough, and it typically works adequately.\n\nHowever, this could get very repetitive and somewhat error-prone, until a more dynamic solution is needed. This package helps to safely avoid all that boilerplate code.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuuf6429%2Fphp-castable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuuf6429%2Fphp-castable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuuf6429%2Fphp-castable/lists"}