From dfd3aa5a25f9cf9db61122d4f4c5daa32c8aa06c Mon Sep 17 00:00:00 2001 From: Derek Law Date: Tue, 5 Apr 2016 11:34:44 -0500 Subject: [PATCH] Adding line ending format option --- src/Nacha/Batch.php | 11 +++++++---- src/Nacha/File.php | 15 +++++++++------ src/Nacha/LineEnding.php | 8 ++++++++ 3 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 src/Nacha/LineEnding.php diff --git a/src/Nacha/Batch.php b/src/Nacha/Batch.php index c94921a..a629739 100644 --- a/src/Nacha/Batch.php +++ b/src/Nacha/Batch.php @@ -7,6 +7,7 @@ use Nacha\Record\DebitEntry; use Nacha\Record\CcdEntry; use Nacha\Record\Entry; +use Nacha\LineEnding; /** * Class Batch @@ -20,6 +21,7 @@ class Batch { const DEBITS_ONLY = 225; private $header; + private $lineEnding; /** @var DebitEntry[] */ private $creditEntries = []; @@ -27,8 +29,9 @@ class Batch { /** @var CcdEntry[] */ private $debitEntries = []; - public function __construct() { + public function __construct($lineEnding=null) { $this->header = new BatchHeader(); + $this->lineEnding = $lineEnding ? $lineEnding : LineEnding::UNIX; } public function getHeader() { @@ -84,11 +87,11 @@ public function __toString() { ->setBatchNumber((string)$this->getHeader()->getBatchNumber()); foreach ($this->debitEntries as $entry) { - $entries .= (string)$entry."\n"; + $entries .= (string)$entry.$this->lineEnding; } foreach ($this->creditEntries as $entry) { - $entries .= (string)$entry."\n"; + $entries .= (string)$entry.$this->lineEnding; } // calculate service code @@ -107,7 +110,7 @@ public function __toString() { $footer->setTotalCreditAmount($this->getTotalCreditAmount()); $footer->setServiceClassCode((string)$this->header->getServiceClassCode()); - return (string)$this->header."\n".$entries.$footer; + return (string)$this->header.$this->lineEnding.$entries.$footer; } } \ No newline at end of file diff --git a/src/Nacha/File.php b/src/Nacha/File.php index f386419..ebf323e 100644 --- a/src/Nacha/File.php +++ b/src/Nacha/File.php @@ -9,11 +9,14 @@ class File { private $header; + private $lineEnding; + /** @var Batch[] */ private $batches = []; - public function __construct() { - $this->header = new FileHeader(); + public function __construct($lineEnding=null) { + $this->header = new FileHeader(); + $this->lineEnding = $lineEnding ? $lineEnding : LineEnding::UNIX; } public function getHeader() { @@ -51,7 +54,7 @@ public function __toString() { $totalDebits += $batch->getTotalDebitAmount(); // is this total amount of debits, or entries? $totalCredits += $batch->getTotalCreditAmount(); // is this total amount of credits, or entries? - $batches .= $batch."\n"; + $batches .= $batch.$this->lineEnding; } // block padding @@ -61,7 +64,7 @@ public function __toString() { $block = ''; for ($x=0; $x<$blocksNeeded % 10; $x++) { - $block .= (new Block)."\n"; + $block .= (new Block).$this->lineEnding; } $fileFooter->setBlockCount(ceil($totalRecords / 10)); @@ -69,9 +72,9 @@ public function __toString() { $fileFooter->setTotalDebits($totalDebits); $fileFooter->setTotalCredits($totalCredits); - $output = $this->header."\n".$batches.$fileFooter."\n".$block; + $output = $this->header.$this->lineEnding.$batches.$fileFooter.$this->lineEnding.$block; - return rtrim($output, "\n"); + return rtrim($output, $this->lineEnding); } } \ No newline at end of file diff --git a/src/Nacha/LineEnding.php b/src/Nacha/LineEnding.php new file mode 100644 index 0000000..e15d33f --- /dev/null +++ b/src/Nacha/LineEnding.php @@ -0,0 +1,8 @@ +