{"id":19881841,"url":"https://github.com/kubinyete/edi-php","last_synced_at":"2025-09-12T03:37:11.806Z","repository":{"id":199300677,"uuid":"700891833","full_name":"Kubinyete/edi-php","owner":"Kubinyete","description":"A standard library for declaring EDI parsers","archived":false,"fork":false,"pushed_at":"2024-05-03T13:01:11.000Z","size":35,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-11T18:14:49.339Z","etag":null,"topics":["edi","library","parsing","php","php8"],"latest_commit_sha":null,"homepage":"","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/Kubinyete.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-05T13:53:30.000Z","updated_at":"2024-08-31T08:03:11.000Z","dependencies_parsed_at":"2024-10-23T11:24:27.572Z","dependency_job_id":null,"html_url":"https://github.com/Kubinyete/edi-php","commit_stats":null,"previous_names":["kubinyete/edi-php"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kubinyete%2Fedi-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kubinyete%2Fedi-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kubinyete%2Fedi-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kubinyete%2Fedi-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kubinyete","download_url":"https://codeload.github.com/Kubinyete/edi-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241310516,"owners_count":19941969,"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":["edi","library","parsing","php","php8"],"created_at":"2024-11-12T17:15:24.790Z","updated_at":"2025-03-01T03:20:59.449Z","avatar_url":"https://github.com/Kubinyete.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EDI PHP\n\nA standard library for declaring EDI parsers, because that's not a fun thing to do.\n\n### Why should I use it?\n\nThis is a very common use-case for many services that output data using a defined text-file layout, for example,\nmany services (Ex: Payment providers) output some sort of layout file that follows a simplified column separated values\nthat has a fixed size set.\n\nFor example, the following line (25 bytes):\n\n`120240503Hello World     `\n\nCan be interpreted as\n\n| Range  | Size | Column     | Value\n| ------ | ---- | ---------- | -----\n| 0 - 0  | 1    | Reg. type  | 1\n| 1 - 8  | 8    | Date       | 2024-05-03\n| 9 - 25 | 16   | Message    | Hello World\n\n### How it works\n\nTo provide a better developer experience while creating these parsers, this library provides some useful property \nattributes and a base class for declaring these types of data.\n\nFor example, to declare the previous line, we can declare it in code as the following:\n\n```php\nfinal class ExampleLayoutType extends Registry {\n    #[Number(1)]\n    public int $type;\n\n    #[Date(8, format: '!Ymd')]\n    public DateTimeInterface $type;\n\n    #[Text(16)]\n    public string $message;\n}\n\n// We can parse the data directly\n$example = ExampleLayoutType::from('120240503Hello World     ')\n\n// Or we can hydrate it\n$example = new ExampleLayoutType();\n$example-\u003ehydrate('120240503Hello World     ');\n\n```\n\n### Declaring dynamic parsers\n\nUsing our built in line parser, we can create dynamically parsed (by line) types, with the power of generators, these types are parsed on demand:\n\n```php\n// Using a standard line parser, iterates over each line\nclass EDIParser extends LineParser\n{\n    // Ran on each line iteration\n    protected function parse(LineContext $ctx): ?Registry\n    {\n        [$contents, $number] = $ctx-\u003eunwrap();\n        // For this EDI file, we can deduce which type it is based on\n        // the first 2 letters provided each line.\n        $code = substr($contents, 0, 2);\n\n        try {\n            return match ($code) {\n                EDIRegistry::TYPE_HEADER_START =\u003e EDIHeader::from($contents),\n                EDIRegistry::TYPE_TRANSACTION_BATCH_START =\u003e EDITransactionBatch::from($contents),\n                EDIRegistry::TYPE_SALE_RECEIPT =\u003e EDISaleReceipt::from($contents),\n                // Our LineContext can provide a more verbose message to inform\n                // where in our data the error ocurred.\n                default =\u003e $ctx-\u003eraise(\"Cannot parse EDI of type '$code'\", 0),\n            };\n        } catch (FieldException $e) {\n            $ctx-\u003eraise($e-\u003egetMessage(), $e-\u003egetCursor());\n        }\n    }\n}\n\n// Using our parser directly\n// Reading directly from stdin\n$buffer = Stream::file('php://stdin', 'rb');\n$parser = EDIParser::loadFromStream($buffer);\n\nforeach ($parser as $registry) {\n    /** @var Registry $registry */\n    // We have direct access to our parsed objects on demand\n    dump($registry);\n}\n```\n\n### Understanding parsing errors\n\n\u003e This feature is work-in-progress, there are a some things we can do to make it better\n\nWe enable out-of-the-box support for friendly contextual messages on any errors that ocurred.\n\n```js\nPHP Fatal error:  Uncaught Kubinyete\\Edi\\Parser\\Exception\\ParseException: Line 1: Failed to parse field '202d1001025840' as a date with format 'YmdHis'\nContents: \"A00003.1202d1001025840000442ADIQ SOLUCOES PAGAMENTOS S.A  0040002783189N000001\"\n           --------^\n in /home/vitorkubinyete/code/edi-php/src/Parser/LineContext.php:38","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkubinyete%2Fedi-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkubinyete%2Fedi-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkubinyete%2Fedi-php/lists"}