Skip to content

Commit

Permalink
Merging from master
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwhitt committed Apr 6, 2016
2 parents 880439c + fa2dc1e commit fe28050
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/Nacha/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nacha\Record\DebitEntry;
use Nacha\Record\CcdEntry;
use Nacha\Record\Entry;
use Nacha\LineEnding;

/**
* Class Batch
Expand All @@ -20,15 +21,17 @@ class Batch {
const DEBITS_ONLY = 225;

private $header;
private $lineEnding;

/** @var DebitEntry[] */
private $creditEntries = [];

/** @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() {
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

}
15 changes: 9 additions & 6 deletions src/Nacha/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -61,17 +64,17 @@ 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));
$fileFooter->setEntryAddendaCount($totalEntryCount);
$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);
}

}
8 changes: 8 additions & 0 deletions src/Nacha/LineEnding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Nacha;

class LineEnding {
const WINDOWS = "\r\n";
const UNIX = "\n";
}

0 comments on commit fe28050

Please sign in to comment.