{"id":13545588,"url":"https://github.com/yohang/Finite","last_synced_at":"2025-04-02T15:31:23.661Z","repository":{"id":4834887,"uuid":"5988969","full_name":"yohang/Finite","owner":"yohang","description":"A Simple PHP Finite State Machine","archived":false,"fork":false,"pushed_at":"2025-02-25T17:25:04.000Z","size":685,"stargazers_count":1320,"open_issues_count":1,"forks_count":188,"subscribers_count":52,"default_branch":"main","last_synced_at":"2025-03-25T21:15:03.163Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://yohan.giarel.li/Finite","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"waynegraham/geoblacklight-converter","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yohang.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":"2012-09-27T21:26:13.000Z","updated_at":"2025-03-14T09:27:25.000Z","dependencies_parsed_at":"2023-07-06T01:53:23.737Z","dependency_job_id":"9d69d1e7-a4e1-4e74-b13d-a8089e2f4cc7","html_url":"https://github.com/yohang/Finite","commit_stats":{"total_commits":184,"total_committers":25,"mean_commits":7.36,"dds":0.3315217391304348,"last_synced_commit":"32cc53c1a10ee5344cf8e3ed2bfa26def54e14b9"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yohang%2FFinite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yohang%2FFinite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yohang%2FFinite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yohang%2FFinite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yohang","download_url":"https://codeload.github.com/yohang/Finite/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246718483,"owners_count":20822569,"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":"2024-08-01T11:01:06.283Z","updated_at":"2025-04-02T15:31:23.648Z","avatar_url":"https://github.com/yohang.png","language":"PHP","funding_links":[],"categories":["PHP","Recently Updated","Table of Contents"],"sub_categories":["[Apr 12, 2025](/content/2025/04/12/README.md)","Architectural","Globalization"],"readme":"Finite, A Simple PHP Finite State Machine\n=========================================\n\nFinite is a Simple State Machine, written in PHP. \nIt can manage any Stateful object by defining states and transitions between these states.\n\nAs of version 2, Finite is a low-deps and lightweight state machine library, thanks to the use of PHP Enums.\n\n\n![CI Status](https://github.com/yohang/finite/actions/workflows/ci.yml/badge.svg)\n\nDisclaimer\n----------\n\nI don't have the time anymore to maintain this lib. Here is the documentation for the brand new Finite V2, based\non PHP \u003e= 8.1 Enums, but don't expect it to be updated on a regular basis.\n\nFeatures\n--------\n\n* Manage State/Transition graph for an object\n* Attach business logic to states\n* Listen to transitions between state to trigger your domain code\n* Symfony integration\n* Twig Extension\n\nGetting started\n---------------\n\n### Installation (via composer)\n```bash\n $ composer req yohang/finite\n```\n\n### Define your state enum\n\n```php\nenum DocumentState: string implements State\n{\n    case DRAFT = 'draft';\n    case PUBLISHED = 'published';\n    case REPORTED = 'reported';\n    case DISABLED = 'disabled';\n\n    public static function getTransitions(): array\n    {\n        return [\n            new Transition('publish', [self::DRAFT], self::PUBLISHED),\n            new Transition('clear', [self::REPORTED, self::DISABLED], self::PUBLISHED),\n            new Transition('report', [self::PUBLISHED], self::REPORTED),\n            new Transition('disable', [self::REPORTED, self::PUBLISHED], self::DISABLED),\n        ];\n    }\n}\n\n```\n\n\n### Define your Stateful Object\n\nYour stateful object just need to have a state property\n\n\n```php\nclass Document\n{\n    private DocumentState $state = DocumentState::DRAFT;\n\n    public function getState(): DocumentState\n    {\n        return $this-\u003estate;\n    }\n\n    public function setState(DocumentState $state): void\n    {\n        $this-\u003estate = $state;\n    }\n}\n```\n\n\n### Initializing a simple StateMachine\n\n```php\nuse Finite\\StateMachine;\n\n$document = new Document;\n\n$sm = new StateMachine;\n\n// Can we process a transition ?\n$sm-\u003ecan($document, 'publish');\n\n// Apply a transition\n$sm-\u003eapply($document, 'publish'); \n\n```\n\n### Add business logic to states\n\nFinite \u003c 2.0 had properties on states. A metadata mechanism that allowed to add business properties on states to \ndefine the behavior of on object, depending on its state.\n\nThe idea behind this was to avoid to test the state in your domain code (A controller must not throw a 404 if the state \nis draft. But it can throw a 404 if the current state does not have a \"visible\" property. That was the idea).\n\nFinite 2 does not needs this. PHP Enums can have methods. So, replace your properties with simple methods on your state.\n\n```php\nenum DocumentState: string implements State\n{\n    // ...\n\n    public function isDeletable(): bool\n    {\n        return in_array($this, [self::DRAFT, self::DISABLED]);\n    }\n\n    public function isPrintable(): bool\n    {\n        return in_array($this, [self::PUBLISHED, self::REPORTED]);\n    }\n}\n```\n\nAfter that, you can use theses methods on your object, even without instantiating the state machine.\n\n```php\nvar_dump($document-\u003egetState()-\u003eisDeletable());\nvar_dump($document-\u003egetState()-\u003eisPrintable());\n```\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyohang%2FFinite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyohang%2FFinite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyohang%2FFinite/lists"}