{"id":22092112,"url":"https://github.com/krakphp/adt","last_synced_at":"2025-08-23T10:37:39.753Z","repository":{"id":57009004,"uuid":"196140780","full_name":"krakphp/adt","owner":"krakphp","description":"ADT (Algebraic Data Types) in PHP","archived":false,"fork":false,"pushed_at":"2019-09-15T04:37:57.000Z","size":9,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-16T15:49:08.363Z","etag":null,"topics":["adt","algebraic-data-types","enum","enumeration","php","php7"],"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/krakphp.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":"2019-07-10T05:58:55.000Z","updated_at":"2020-09-11T23:46:18.000Z","dependencies_parsed_at":"2022-08-21T14:50:51.439Z","dependency_job_id":null,"html_url":"https://github.com/krakphp/adt","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/krakphp/adt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fadt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fadt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fadt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fadt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krakphp","download_url":"https://codeload.github.com/krakphp/adt/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fadt/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271746667,"owners_count":24813576,"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-08-23T02:00:09.327Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","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":["adt","algebraic-data-types","enum","enumeration","php","php7"],"created_at":"2024-12-01T03:08:22.136Z","updated_at":"2025-08-23T10:37:39.728Z","avatar_url":"https://github.com/krakphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ADT (Algebraic Data Types)\n\nPoor man's implementation of [Algebraic Data Types](https://en.wikipedia.org/wiki/Algebraic_data_type) in PHP.\n\nAlso known as an enum with an associated value in other languages like Swift or Rust.\n\n## Installation\n\nInstall with composer at `krak/adt`\n\n## Usage\n\n```php\n\u003c?php\n\nuse Krak\\ADT\\ADT;\n\n/**\n * @method static Upc upc(int $numberSystem, int $manufacturer, int $product, int $check)\n * @method static QrCode qrCode(string $productCode)\n */\nabstract class Barcode extends ADT {\n    public static function types(): array {\n        return [Upc::class, QrCode::class];\n    }\n}\n\nfinal class Upc extends Barcode {\n    public $numberSystem;\n    public $manufacturer;\n    public $product;\n    public $check;\n\n    public function __construct(int $numberSystem, int $manufacturer, int $product, int $check) {\n        $this-\u003enumberSystem = $numberSystem;\n        $this-\u003emanufacturer = $manufacturer;\n        $this-\u003eproduct = $product;\n        $this-\u003echeck = $check;\n    }\n}\n\nfinal class QrCode extends Barcode {\n    public $productCode;\n\n    public function __construct(string $productCode) {\n        $this-\u003eproductCode = $productCode;\n    }\n}\n\n$barcode = new QrCode('abc123');\n\n// requires that all cases are set or exception is thrown\n$oneOrTwo = $barcode-\u003ematch([\n    Upc::class =\u003e function(Upc $upc) { return 1;},\n    QrCode::class =\u003e function(QrCode $qrCode) { return 2; },\n]);\n\n// allow a default value\n$oneOrNull = $barcode-\u003ematchWithDefault([\n    Upc::class =\u003e function(Upc $upc) { return 1; }\n]);\n\n// return static values\n$threeOrFour = $barcode-\u003ematch([\n    Upc::class =\u003e 3,\n    QrCode::class =\u003e 4,\n]);\n\n// static constructors\n$qrCode = Barcode::qrCode('abc123');\n```\n\n### Autoloading Concerns\n\nWith the example above, if you try to create a QrCode or Upc before ever referencing the Barcode class, you'll likely get a file not found when using composer's psr-4 autoloader.\n\nYou can get around this a few ways:\n\n1. Utilize class mapping\n2. Include the enum class file in the composer autoload.files section (something like [php-inc](https://github.com/krakphp/php-inc) could make that easier)\n3. Utilize static constructors provided by the base ADT class:\n\n    ```php\n    \u003c?php\n\n    $upc = Barcode::upc(1, 2, 3, 4);\n    $qrCode = Barcode::qrCode('abc123');\n    ```\n\n## Tests\n\nTests are run via `make test` and are stored in the `test` directory. We use peridot for testing.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrakphp%2Fadt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrakphp%2Fadt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrakphp%2Fadt/lists"}