Last active
October 17, 2019 13:57
-
-
Save Chemaclass/1d05e5e7f4c9734554e9fe0e2de5dcb5 to your computer and use it in GitHub Desktop.
EdiFileGenerator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php declare(strict_types=1); | |
| final class EdiFileGenerator | |
| { | |
| private const MESSAGE_TEMPLATE = <<<STR | |
| UNH+%d+IFTMIN:S:93A:UN:PN001' | |
| BGM+340+0035800000000819%d+9' | |
| DTM+10:20191002:102' | |
| TSR+19+A2' | |
| CNT+7:0.1:KGM' | |
| CNT+11:1:PCE' | |
| CNT+15:0.068224:MTQ' | |
| RFF+CU:ORD336526' | |
| TDT+20' | |
| NAD+CZ+3669462:160:Z87++Shipper Name+Street Number 1 45+A Place++84191+SE' | |
| NAD+CN+++Consignee Name+Street Number 2 78+Receiver_City_Long_Name Ki++98131+SE' | |
| CTA+IC+:Name thingy' | |
| COM+0046759642314:AL' | |
| NAD+OS+++Person Name Surname+Street Number 3+City++2000+DK' | |
| GID+1+1' | |
| MEA+WT+G+KGM:0.1' | |
| MEA+VOL++MTQ:0.06822' | |
| PCI+18+0035800000000819%d' | |
| UNT+19+1' | |
| STR; | |
| private const TRANSACTION_HEADER = <<<STR | |
| UNA:+.? ' | |
| UNB+UNOC:3+87:789+NO000603:NEP:ASD12A+191002:2032+3+MPM 2.19+1424' | |
| STR; | |
| private const TRANSACTION_FOOTER = <<<STR | |
| "UNZ+%d+3'" | |
| STR; | |
| public static function generateMessages(int $total): string | |
| { | |
| $transaction = self::TRANSACTION_HEADER . PHP_EOL; | |
| foreach (range(1, $total) as $i) { | |
| $currentMessage = sprintf(self::MESSAGE_TEMPLATE, $i, $i, $i); | |
| $transaction .= $currentMessage . PHP_EOL; | |
| } | |
| return $transaction . sprintf(self::TRANSACTION_FOOTER, $total) . PHP_EOL; | |
| } | |
| } | |
| $total = 1000; | |
| $fileName = (new \DateTime())->format('Y-m-d.u') . "_{$total}.edi"; | |
| $handler = fopen($fileName, 'w'); | |
| fwrite($handler, EdiFileGenerator::generateMessages($total)); | |
| fclose($handler); | |
| echo "Done. Filename: {$fileName}"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment