Completed
Pull Request — master (#95)
by
unknown
05:12
created
lib/Fhp/Model/StatementOfAccount/Statement.php 2 patches
Indentation   +131 added lines, -131 removed lines patch added patch discarded remove patch
@@ -8,136 +8,136 @@
 block discarded – undo
8 8
  */
9 9
 class Statement
10 10
 {
11
-	const CD_CREDIT = 'credit';
12
-	const CD_DEBIT = 'debit';
13
-
14
-	/**
15
-	 * @var array of Transaction
16
-	 */
17
-	protected $transactions = array();
18
-
19
-	/**
20
-	 * @var float
21
-	 */
22
-	protected $startBalance = 0.0;
23
-
24
-	/**
25
-	 * @var string
26
-	 */
27
-	protected $creditDebit;
28
-
29
-	/**
30
-	 * @var \DateTime|null
31
-	 */
32
-	protected $date;
33
-
34
-	/**
35
-	 * Get transactions
36
-	 *
37
-	 * @return Transaction[]
38
-	 */
39
-	public function getTransactions()
40
-	{
41
-		return $this->transactions;
42
-	}
43
-
44
-	/**
45
-	 * Set transactions
46
-	 *
47
-	 * @param array $transactions
48
-	 *
49
-	 * @return $this
50
-	 */
51
-	public function setTransactions(array $transactions = null)
52
-	{
53
-		$this->transactions = $transactions;
54
-
55
-		return $this;
56
-	}
57
-
58
-	public function addTransaction(Transaction $transaction)
59
-	{
60
-		$this->transactions[] = $transaction;
61
-	}
62
-
63
-	/**
64
-	 * Get startBalance
65
-	 *
66
-	 * @return float
67
-	 */
68
-	public function getStartBalance()
69
-	{
70
-		return $this->startBalance;
71
-	}
72
-
73
-	public function getSignedStartBalance()
74
-	{
75
-		$s = 1;
76
-		if ($this->getCreditDebit() == self::CD_DEBIT) $s = -1;
77
-		return $s * $this->getStartBalance();
78
-
79
-	}
80
-
81
-	/**
82
-	 * Set startBalance
83
-	 *
84
-	 * @param float $startBalance
85
-	 *
86
-	 * @return $this
87
-	 */
88
-	public function setStartBalance($startBalance)
89
-	{
90
-		$this->startBalance = (float) $startBalance;
91
-
92
-		return $this;
93
-	}
94
-
95
-	/**
96
-	 * Get creditDebit
97
-	 *
98
-	 * @return string
99
-	 */
100
-	public function getCreditDebit()
101
-	{
102
-		return $this->creditDebit;
103
-	}
104
-
105
-	/**
106
-	 * Set creditDebit
107
-	 *
108
-	 * @param string|null $creditDebit
109
-	 *
110
-	 * @return $this
111
-	 */
112
-	public function setCreditDebit($creditDebit)
113
-	{
114
-		$this->creditDebit = $creditDebit;
115
-
116
-		return $this;
117
-	}
118
-
119
-	/**
120
-	 * Get date
121
-	 *
122
-	 * @return \DateTime
123
-	 */
124
-	public function getDate()
125
-	{
126
-		return $this->date;
127
-	}
128
-
129
-	/**
130
-	 * Set date
131
-	 *
132
-	 * @param \DateTime $date
133
-	 *
134
-	 * @return $this
135
-	 */
136
-	public function setDate(\DateTime $date)
137
-	{
138
-		$this->date = $date;
139
-
140
-		return $this;
141
-	}
11
+    const CD_CREDIT = 'credit';
12
+    const CD_DEBIT = 'debit';
13
+
14
+    /**
15
+     * @var array of Transaction
16
+     */
17
+    protected $transactions = array();
18
+
19
+    /**
20
+     * @var float
21
+     */
22
+    protected $startBalance = 0.0;
23
+
24
+    /**
25
+     * @var string
26
+     */
27
+    protected $creditDebit;
28
+
29
+    /**
30
+     * @var \DateTime|null
31
+     */
32
+    protected $date;
33
+
34
+    /**
35
+     * Get transactions
36
+     *
37
+     * @return Transaction[]
38
+     */
39
+    public function getTransactions()
40
+    {
41
+        return $this->transactions;
42
+    }
43
+
44
+    /**
45
+     * Set transactions
46
+     *
47
+     * @param array $transactions
48
+     *
49
+     * @return $this
50
+     */
51
+    public function setTransactions(array $transactions = null)
52
+    {
53
+        $this->transactions = $transactions;
54
+
55
+        return $this;
56
+    }
57
+
58
+    public function addTransaction(Transaction $transaction)
59
+    {
60
+        $this->transactions[] = $transaction;
61
+    }
62
+
63
+    /**
64
+     * Get startBalance
65
+     *
66
+     * @return float
67
+     */
68
+    public function getStartBalance()
69
+    {
70
+        return $this->startBalance;
71
+    }
72
+
73
+    public function getSignedStartBalance()
74
+    {
75
+        $s = 1;
76
+        if ($this->getCreditDebit() == self::CD_DEBIT) $s = -1;
77
+        return $s * $this->getStartBalance();
78
+
79
+    }
80
+
81
+    /**
82
+     * Set startBalance
83
+     *
84
+     * @param float $startBalance
85
+     *
86
+     * @return $this
87
+     */
88
+    public function setStartBalance($startBalance)
89
+    {
90
+        $this->startBalance = (float) $startBalance;
91
+
92
+        return $this;
93
+    }
94
+
95
+    /**
96
+     * Get creditDebit
97
+     *
98
+     * @return string
99
+     */
100
+    public function getCreditDebit()
101
+    {
102
+        return $this->creditDebit;
103
+    }
104
+
105
+    /**
106
+     * Set creditDebit
107
+     *
108
+     * @param string|null $creditDebit
109
+     *
110
+     * @return $this
111
+     */
112
+    public function setCreditDebit($creditDebit)
113
+    {
114
+        $this->creditDebit = $creditDebit;
115
+
116
+        return $this;
117
+    }
118
+
119
+    /**
120
+     * Get date
121
+     *
122
+     * @return \DateTime
123
+     */
124
+    public function getDate()
125
+    {
126
+        return $this->date;
127
+    }
128
+
129
+    /**
130
+     * Set date
131
+     *
132
+     * @param \DateTime $date
133
+     *
134
+     * @return $this
135
+     */
136
+    public function setDate(\DateTime $date)
137
+    {
138
+        $this->date = $date;
139
+
140
+        return $this;
141
+    }
142 142
 }
143 143
 
Please login to merge, or discard this patch.
Braces   +3 added lines, -1 removed lines patch added patch discarded remove patch
@@ -73,7 +73,9 @@
 block discarded – undo
73 73
 	public function getSignedStartBalance()
74 74
 	{
75 75
 		$s = 1;
76
-		if ($this->getCreditDebit() == self::CD_DEBIT) $s = -1;
76
+		if ($this->getCreditDebit() == self::CD_DEBIT) {
77
+		    $s = -1;
78
+		}
77 79
 		return $s * $this->getStartBalance();
78 80
 
79 81
 	}
Please login to merge, or discard this patch.
lib/Fhp/Response/GetStatementOfAccount.php 1 patch
Indentation   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -55,29 +55,29 @@
 block discarded – undo
55 55
     protected static function addFromArray(array $array, StatementOfAccount $statementOfAccount)
56 56
     {
57 57
         foreach ($array as $statement) {
58
-					$statementModel = new Statement();
59
-					$statementModel->setDate(new \DateTime($statement['date']));
60
-					$statementModel->setStartBalance((float) $statement['start_balance']['amount']);
61
-					$statementModel->setCreditDebit($statement['start_balance']['credit_debit']);
62
-					$statementOfAccount->addStatement($statementModel);
58
+                    $statementModel = new Statement();
59
+                    $statementModel->setDate(new \DateTime($statement['date']));
60
+                    $statementModel->setStartBalance((float) $statement['start_balance']['amount']);
61
+                    $statementModel->setCreditDebit($statement['start_balance']['credit_debit']);
62
+                    $statementOfAccount->addStatement($statementModel);
63 63
 
64
-					foreach ($statement['transactions'] as $trx) {
65
-						$transaction = new Transaction();
66
-						$transaction->setBookingDate(new \DateTime($trx['booking_date']));
67
-						$transaction->setValutaDate(new \DateTime($trx['valuta_date']));
68
-						$transaction->setCreditDebit($trx['credit_debit']);
69
-						$transaction->setAmount($trx['amount']);
70
-						$transaction->setTransactionCode($trx['transaction_code']);
71
-						$transaction->setBookingText($trx['description']['booking_text']);
72
-						$transaction->setDescription1($trx['description']['description_1']);
73
-						$transaction->setDescription2($trx['description']['description_2']);
74
-						$transaction->setStructuredDescription($trx['description']['description']);
75
-						$transaction->setBankCode($trx['description']['bank_code']);
76
-						$transaction->setAccountNumber($trx['description']['account_number']);
77
-						$transaction->setName($trx['description']['name']);
78
-						$statementModel->addTransaction($transaction);
79
-					}
80
-				}
64
+                    foreach ($statement['transactions'] as $trx) {
65
+                        $transaction = new Transaction();
66
+                        $transaction->setBookingDate(new \DateTime($trx['booking_date']));
67
+                        $transaction->setValutaDate(new \DateTime($trx['valuta_date']));
68
+                        $transaction->setCreditDebit($trx['credit_debit']);
69
+                        $transaction->setAmount($trx['amount']);
70
+                        $transaction->setTransactionCode($trx['transaction_code']);
71
+                        $transaction->setBookingText($trx['description']['booking_text']);
72
+                        $transaction->setDescription1($trx['description']['description_1']);
73
+                        $transaction->setDescription2($trx['description']['description_2']);
74
+                        $transaction->setStructuredDescription($trx['description']['description']);
75
+                        $transaction->setBankCode($trx['description']['bank_code']);
76
+                        $transaction->setAccountNumber($trx['description']['account_number']);
77
+                        $transaction->setName($trx['description']['name']);
78
+                        $statementModel->addTransaction($transaction);
79
+                    }
80
+                }
81 81
 
82 82
         return $statementOfAccount;
83 83
     }
Please login to merge, or discard this patch.
lib/Fhp/Parser/MT940.php 4 patches
Indentation   +229 added lines, -229 removed lines patch added patch discarded remove patch
@@ -10,233 +10,233 @@
 block discarded – undo
10 10
  */
11 11
 class MT940
12 12
 {
13
-	const TARGET_ARRAY = 0;
14
-
15
-	const CD_CREDIT = 'credit';
16
-	const CD_DEBIT = 'debit';
17
-	const CD_CREDIT_CANCELLATION = 'credit_cancellation';
18
-	const CD_DEBIT_CANCELLATION = 'debit_cancellation';
19
-
20
-	/** @var string */
21
-	protected $rawData;
22
-	/** @var string */
23
-	protected $soaDate;
24
-
25
-	/**
26
-	 * MT940 constructor.
27
-	 *
28
-	 * @param string $rawData
29
-	 */
30
-	public function __construct($rawData)
31
-	{
32
-		$this->rawData = (string) $rawData;
33
-	}
34
-
35
-	/**
36
-	 * @param string $target
37
-	 * @return array
38
-	 * @throws MT940Exception
39
-	 */
40
-	public function parse($target)
41
-	{
42
-		switch ($target) {
43
-		case static::TARGET_ARRAY:
44
-			return $this->parseToArray();
45
-			break;
46
-		default:
47
-			throw new MT940Exception('Invalid parse type provided');
48
-		}
49
-	}
50
-
51
-	/**
52
-	 * @return array
53
-	 * @throws MT940Exception
54
-	 */
55
-	protected function parseToArray()
56
-	{
57
-		// The divider can be either \r\n or @@
58
-
59
-		$divider = "(@@|\r\n)";
60
-		$result = array();
61
-		$days = preg_split('/' . $divider . '-' . $divider . '/', $this->rawData);
62
-
63
-		foreach ($days as $day) {
64
-			$day = preg_split('/' . $divider . ':/', $day);
65
-			$statement = array();
66
-			$transactions = array();
67
-			$cnt = 0;
68
-			for ($i = 0, $cnt = count($day); $i < $cnt; $i++) {
69
-				// handle start balance
70
-				// 60F:C160401EUR1234,56
71
-				if (preg_match('/^60(F|M):/', $day[$i])) {
72
-					// remove 60(F|M): for better parsing
73
-					$day[$i] = substr($day[$i], 4);
74
-					$this->soaDate = $this->getDate(substr($day[$i], 1, 6));
75
-
76
-					$amount = str_replace(',', '.', substr($day[$i], 10));
77
-					$statement['start_balance'] = array(
78
-						'amount' => $amount,
79
-						'credit_debit' => (substr($day[$i], 0, 1) == 'C') ? static::CD_CREDIT : static::CD_DEBIT
80
-					);
81
-					$statement['date'] = $this->soaDate;
82
-				} elseif (
83
-					// found transaction
84
-					// trx:61:1603310331DR637,39N033NONREF
85
-					0 === strpos($day[$i], '61:')
86
-					&& isset($day[$i + 1])
87
-					&& 0 === strpos($day[$i + 1], '86:')
88
-				) {
89
-					$transaction = substr($day[$i], 3);
90
-					$description = substr($day[$i + 1], 3);
91
-
92
-					$currentTrx = array();
93
-
94
-					preg_match('/^\d{6}(\d{4})?(C|D|RC|RD)[A-Z]?([^N]+)N/', $transaction, $matches);
95
-
96
-					switch($matches[2]) {
97
-					case 'C':
98
-						$currentTrx['credit_debit'] = static::CD_CREDIT;
99
-						break;
100
-					case 'D':
101
-						$currentTrx['credit_debit'] = static::CD_DEBIT;
102
-						break;
103
-					case 'RC':
104
-						$currentTrx['credit_debit'] = static::CD_CREDIT_CANCELLATION;
105
-						break;
106
-					case 'RD':
107
-						$currentTrx['credit_debit'] = static::CD_DEBIT_CANCELLATION;
108
-						break;
109
-					default:
110
-						throw new MT940Exception('c/d/rc/rd mark not found in: ' . $transaction);
111
-					}
112
-
113
-					$amount = $matches[3];
114
-					$amount = str_replace(',', '.', $amount);
115
-					$currentTrx['amount'] = floatval($amount);
116
-
117
-					$currentTrx['transaction_code'] = substr($description, 0, 3);
118
-
119
-					$description = $this->parseDescription($description);
120
-					$currentTrx['description'] = $description;
121
-
122
-					// :61:1605110509D198,02NMSCNONREF
123
-					// 16 = year
124
-					// 0511 = valuta date
125
-					// 0509 = booking date
126
-					$year = substr($transaction, 0, 2);
127
-					$valutaDate = $this->getDate($year . substr($transaction, 2, 4));
128
-
129
-					$bookingDate = substr($transaction, 6, 4);
130
-					if (preg_match('/^\d{4}$/', $bookingDate)) {
131
-						// if valuta date is earlier than booking date, then it must be in the new year.
132
-						if (substr($transaction, 2, 2) == '12' && substr($transaction, 6, 2) == '01') {
133
-							$year++;
134
-						} elseif (substr($transaction, 2, 2) == '01' && substr($transaction, 6, 2) == '12') {
135
-							$year--;
136
-						}
137
-						$bookingDate = $this->getDate($year . $bookingDate);
138
-					} else {
139
-						// if booking date not set in :61, then we have to take it from :60F
140
-						$bookingDate = $this->soaDate;
141
-					}
142
-
143
-					$currentTrx['booking_date'] = $bookingDate;
144
-					$currentTrx['valuta_date'] = $valutaDate;
145
-
146
-					$transactions[] = $currentTrx;
147
-				}
148
-			}
149
-			$statement['transactions'] = $transactions;
150
-			if (count($transactions) > 0) $result[] = $statement;
151
-		}
152
-
153
-		return $result;
154
-	}
155
-
156
-	/**
157
-	 * @param string $descr
158
-	 * @return array
159
-	 */
160
-	protected function parseDescription($descr)
161
-	{
162
-		$prepared = array();
163
-		$result = array();
164
-
165
-		// prefill with empty values
166
-		for ($i = 0; $i <= 63; $i++) {
167
-			$prepared[$i] = null;
168
-		}
169
-
170
-		$descr = str_replace("\r\n", '', $descr);
171
-		$descr = str_replace('? ', '?', $descr);
172
-		preg_match_all('/\?[\r\n]*(\d{2})([^\?]+)/', $descr, $matches, PREG_SET_ORDER);
173
-
174
-		$descriptionLines = array();
175
-		$description1 = ''; // Legacy, could be removed.
176
-		$description2 = ''; // Legacy, could be removed.
177
-		foreach ($matches as $m) {
178
-			$index = (int) $m[1];
179
-			if ((20 <= $index && $index <= 29) || (60 <= $index && $index <= 63)) {
180
-				if (20 <= $index && $index <= 29) {
181
-					$description1 .= $m[2];
182
-				} else {
183
-					$description2 .= $m[2];
184
-				}
185
-				$m[2] = trim($m[2]);
186
-				if (!empty($m[2])) {
187
-					$descriptionLines[] = $m[2];
188
-				}
189
-			} else {
190
-				$prepared[$index] = $m[2];
191
-			}
192
-		}
193
-
194
-		$description = array();
195
-		if (empty($descriptionLines) || strlen($descriptionLines[0]) < 5 || $descriptionLines[0][4] !== '+') {
196
-			$description['SVWZ'] = implode('', $descriptionLines);
197
-		} else {
198
-			$lastType = null;
199
-			foreach ($descriptionLines as $line) {
200
-				if (strlen($line) > 5 && $line[4] === '+') {
201
-					if ($lastType != null) {
202
-						$description[$lastType] = trim($description[$lastType]);
203
-					}
204
-					$lastType = substr($line, 0, 4);
205
-					$description[$lastType] = substr($line, 5);
206
-				} else {
207
-					$description[$lastType] .= $line;
208
-				}
209
-				if (strlen($line) < 27) {
210
-					// Usually, lines are 27 characters long. In case characters are missing, then it's either the end
211
-					// of the current type or spaces have been trimmed from the end. We want to collapse multiple spaces
212
-					// into one and we don't want to leave trailing spaces behind. So add a single space here to make up
213
-					// for possibly missing spaces, and if it's the end of the type, it will be trimmed off later.
214
-					$description[$lastType] .= ' ';
215
-				}
216
-			}
217
-			$description[$lastType] = trim($description[$lastType]);
218
-		}
219
-
220
-		$result['description']       = $description;
221
-		$result['booking_text']      = trim($prepared[0]);
222
-		$result['primanoten_nr']     = trim($prepared[10]);
223
-		$result['description_1']     = trim($description1);
224
-		$result['bank_code']         = trim($prepared[30]);
225
-		$result['account_number']    = trim($prepared[31]);
226
-		$result['name']              = trim($prepared[32] . $prepared[33]);
227
-		$result['text_key_addition'] = trim($prepared[34]);
228
-		$result['description_2']     = trim($description2);
229
-		return $result;
230
-	}
231
-
232
-	/**
233
-	 * @param string $val
234
-	 * @return string
235
-	 */
236
-	protected function getDate($val)
237
-	{
238
-		$val = '20' . $val;
239
-		preg_match('/(\d{4})(\d{2})(\d{2})/', $val, $m);
240
-		return $m[1] . '-' . $m[2] . '-' . $m[3];
241
-	}
13
+    const TARGET_ARRAY = 0;
14
+
15
+    const CD_CREDIT = 'credit';
16
+    const CD_DEBIT = 'debit';
17
+    const CD_CREDIT_CANCELLATION = 'credit_cancellation';
18
+    const CD_DEBIT_CANCELLATION = 'debit_cancellation';
19
+
20
+    /** @var string */
21
+    protected $rawData;
22
+    /** @var string */
23
+    protected $soaDate;
24
+
25
+    /**
26
+     * MT940 constructor.
27
+     *
28
+     * @param string $rawData
29
+     */
30
+    public function __construct($rawData)
31
+    {
32
+        $this->rawData = (string) $rawData;
33
+    }
34
+
35
+    /**
36
+     * @param string $target
37
+     * @return array
38
+     * @throws MT940Exception
39
+     */
40
+    public function parse($target)
41
+    {
42
+        switch ($target) {
43
+        case static::TARGET_ARRAY:
44
+            return $this->parseToArray();
45
+            break;
46
+        default:
47
+            throw new MT940Exception('Invalid parse type provided');
48
+        }
49
+    }
50
+
51
+    /**
52
+     * @return array
53
+     * @throws MT940Exception
54
+     */
55
+    protected function parseToArray()
56
+    {
57
+        // The divider can be either \r\n or @@
58
+
59
+        $divider = "(@@|\r\n)";
60
+        $result = array();
61
+        $days = preg_split('/' . $divider . '-' . $divider . '/', $this->rawData);
62
+
63
+        foreach ($days as $day) {
64
+            $day = preg_split('/' . $divider . ':/', $day);
65
+            $statement = array();
66
+            $transactions = array();
67
+            $cnt = 0;
68
+            for ($i = 0, $cnt = count($day); $i < $cnt; $i++) {
69
+                // handle start balance
70
+                // 60F:C160401EUR1234,56
71
+                if (preg_match('/^60(F|M):/', $day[$i])) {
72
+                    // remove 60(F|M): for better parsing
73
+                    $day[$i] = substr($day[$i], 4);
74
+                    $this->soaDate = $this->getDate(substr($day[$i], 1, 6));
75
+
76
+                    $amount = str_replace(',', '.', substr($day[$i], 10));
77
+                    $statement['start_balance'] = array(
78
+                        'amount' => $amount,
79
+                        'credit_debit' => (substr($day[$i], 0, 1) == 'C') ? static::CD_CREDIT : static::CD_DEBIT
80
+                    );
81
+                    $statement['date'] = $this->soaDate;
82
+                } elseif (
83
+                    // found transaction
84
+                    // trx:61:1603310331DR637,39N033NONREF
85
+                    0 === strpos($day[$i], '61:')
86
+                    && isset($day[$i + 1])
87
+                    && 0 === strpos($day[$i + 1], '86:')
88
+                ) {
89
+                    $transaction = substr($day[$i], 3);
90
+                    $description = substr($day[$i + 1], 3);
91
+
92
+                    $currentTrx = array();
93
+
94
+                    preg_match('/^\d{6}(\d{4})?(C|D|RC|RD)[A-Z]?([^N]+)N/', $transaction, $matches);
95
+
96
+                    switch($matches[2]) {
97
+                    case 'C':
98
+                        $currentTrx['credit_debit'] = static::CD_CREDIT;
99
+                        break;
100
+                    case 'D':
101
+                        $currentTrx['credit_debit'] = static::CD_DEBIT;
102
+                        break;
103
+                    case 'RC':
104
+                        $currentTrx['credit_debit'] = static::CD_CREDIT_CANCELLATION;
105
+                        break;
106
+                    case 'RD':
107
+                        $currentTrx['credit_debit'] = static::CD_DEBIT_CANCELLATION;
108
+                        break;
109
+                    default:
110
+                        throw new MT940Exception('c/d/rc/rd mark not found in: ' . $transaction);
111
+                    }
112
+
113
+                    $amount = $matches[3];
114
+                    $amount = str_replace(',', '.', $amount);
115
+                    $currentTrx['amount'] = floatval($amount);
116
+
117
+                    $currentTrx['transaction_code'] = substr($description, 0, 3);
118
+
119
+                    $description = $this->parseDescription($description);
120
+                    $currentTrx['description'] = $description;
121
+
122
+                    // :61:1605110509D198,02NMSCNONREF
123
+                    // 16 = year
124
+                    // 0511 = valuta date
125
+                    // 0509 = booking date
126
+                    $year = substr($transaction, 0, 2);
127
+                    $valutaDate = $this->getDate($year . substr($transaction, 2, 4));
128
+
129
+                    $bookingDate = substr($transaction, 6, 4);
130
+                    if (preg_match('/^\d{4}$/', $bookingDate)) {
131
+                        // if valuta date is earlier than booking date, then it must be in the new year.
132
+                        if (substr($transaction, 2, 2) == '12' && substr($transaction, 6, 2) == '01') {
133
+                            $year++;
134
+                        } elseif (substr($transaction, 2, 2) == '01' && substr($transaction, 6, 2) == '12') {
135
+                            $year--;
136
+                        }
137
+                        $bookingDate = $this->getDate($year . $bookingDate);
138
+                    } else {
139
+                        // if booking date not set in :61, then we have to take it from :60F
140
+                        $bookingDate = $this->soaDate;
141
+                    }
142
+
143
+                    $currentTrx['booking_date'] = $bookingDate;
144
+                    $currentTrx['valuta_date'] = $valutaDate;
145
+
146
+                    $transactions[] = $currentTrx;
147
+                }
148
+            }
149
+            $statement['transactions'] = $transactions;
150
+            if (count($transactions) > 0) $result[] = $statement;
151
+        }
152
+
153
+        return $result;
154
+    }
155
+
156
+    /**
157
+     * @param string $descr
158
+     * @return array
159
+     */
160
+    protected function parseDescription($descr)
161
+    {
162
+        $prepared = array();
163
+        $result = array();
164
+
165
+        // prefill with empty values
166
+        for ($i = 0; $i <= 63; $i++) {
167
+            $prepared[$i] = null;
168
+        }
169
+
170
+        $descr = str_replace("\r\n", '', $descr);
171
+        $descr = str_replace('? ', '?', $descr);
172
+        preg_match_all('/\?[\r\n]*(\d{2})([^\?]+)/', $descr, $matches, PREG_SET_ORDER);
173
+
174
+        $descriptionLines = array();
175
+        $description1 = ''; // Legacy, could be removed.
176
+        $description2 = ''; // Legacy, could be removed.
177
+        foreach ($matches as $m) {
178
+            $index = (int) $m[1];
179
+            if ((20 <= $index && $index <= 29) || (60 <= $index && $index <= 63)) {
180
+                if (20 <= $index && $index <= 29) {
181
+                    $description1 .= $m[2];
182
+                } else {
183
+                    $description2 .= $m[2];
184
+                }
185
+                $m[2] = trim($m[2]);
186
+                if (!empty($m[2])) {
187
+                    $descriptionLines[] = $m[2];
188
+                }
189
+            } else {
190
+                $prepared[$index] = $m[2];
191
+            }
192
+        }
193
+
194
+        $description = array();
195
+        if (empty($descriptionLines) || strlen($descriptionLines[0]) < 5 || $descriptionLines[0][4] !== '+') {
196
+            $description['SVWZ'] = implode('', $descriptionLines);
197
+        } else {
198
+            $lastType = null;
199
+            foreach ($descriptionLines as $line) {
200
+                if (strlen($line) > 5 && $line[4] === '+') {
201
+                    if ($lastType != null) {
202
+                        $description[$lastType] = trim($description[$lastType]);
203
+                    }
204
+                    $lastType = substr($line, 0, 4);
205
+                    $description[$lastType] = substr($line, 5);
206
+                } else {
207
+                    $description[$lastType] .= $line;
208
+                }
209
+                if (strlen($line) < 27) {
210
+                    // Usually, lines are 27 characters long. In case characters are missing, then it's either the end
211
+                    // of the current type or spaces have been trimmed from the end. We want to collapse multiple spaces
212
+                    // into one and we don't want to leave trailing spaces behind. So add a single space here to make up
213
+                    // for possibly missing spaces, and if it's the end of the type, it will be trimmed off later.
214
+                    $description[$lastType] .= ' ';
215
+                }
216
+            }
217
+            $description[$lastType] = trim($description[$lastType]);
218
+        }
219
+
220
+        $result['description']       = $description;
221
+        $result['booking_text']      = trim($prepared[0]);
222
+        $result['primanoten_nr']     = trim($prepared[10]);
223
+        $result['description_1']     = trim($description1);
224
+        $result['bank_code']         = trim($prepared[30]);
225
+        $result['account_number']    = trim($prepared[31]);
226
+        $result['name']              = trim($prepared[32] . $prepared[33]);
227
+        $result['text_key_addition'] = trim($prepared[34]);
228
+        $result['description_2']     = trim($description2);
229
+        return $result;
230
+    }
231
+
232
+    /**
233
+     * @param string $val
234
+     * @return string
235
+     */
236
+    protected function getDate($val)
237
+    {
238
+        $val = '20' . $val;
239
+        preg_match('/(\d{4})(\d{2})(\d{2})/', $val, $m);
240
+        return $m[1] . '-' . $m[2] . '-' . $m[3];
241
+    }
242 242
 }
Please login to merge, or discard this patch.
Switch Indentation   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -40,11 +40,11 @@  discard block
 block discarded – undo
40 40
 	public function parse($target)
41 41
 	{
42 42
 		switch ($target) {
43
-		case static::TARGET_ARRAY:
44
-			return $this->parseToArray();
45
-			break;
46
-		default:
47
-			throw new MT940Exception('Invalid parse type provided');
43
+		    case static::TARGET_ARRAY:
44
+			    return $this->parseToArray();
45
+			    break;
46
+		    default:
47
+			    throw new MT940Exception('Invalid parse type provided');
48 48
 		}
49 49
 	}
50 50
 
@@ -94,20 +94,20 @@  discard block
 block discarded – undo
94 94
 					preg_match('/^\d{6}(\d{4})?(C|D|RC|RD)[A-Z]?([^N]+)N/', $transaction, $matches);
95 95
 
96 96
 					switch($matches[2]) {
97
-					case 'C':
98
-						$currentTrx['credit_debit'] = static::CD_CREDIT;
99
-						break;
100
-					case 'D':
101
-						$currentTrx['credit_debit'] = static::CD_DEBIT;
102
-						break;
103
-					case 'RC':
104
-						$currentTrx['credit_debit'] = static::CD_CREDIT_CANCELLATION;
105
-						break;
106
-					case 'RD':
107
-						$currentTrx['credit_debit'] = static::CD_DEBIT_CANCELLATION;
108
-						break;
109
-					default:
110
-						throw new MT940Exception('c/d/rc/rd mark not found in: ' . $transaction);
97
+					    case 'C':
98
+						    $currentTrx['credit_debit'] = static::CD_CREDIT;
99
+						    break;
100
+					    case 'D':
101
+						    $currentTrx['credit_debit'] = static::CD_DEBIT;
102
+						    break;
103
+					    case 'RC':
104
+						    $currentTrx['credit_debit'] = static::CD_CREDIT_CANCELLATION;
105
+						    break;
106
+					    case 'RD':
107
+						    $currentTrx['credit_debit'] = static::CD_DEBIT_CANCELLATION;
108
+						    break;
109
+					    default:
110
+						    throw new MT940Exception('c/d/rc/rd mark not found in: ' . $transaction);
111 111
 					}
112 112
 
113 113
 					$amount = $matches[3];
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -93,7 +93,7 @@
 block discarded – undo
93 93
 
94 94
 					preg_match('/^\d{6}(\d{4})?(C|D|RC|RD)[A-Z]?([^N]+)N/', $transaction, $matches);
95 95
 
96
-					switch($matches[2]) {
96
+					switch ($matches[2]) {
97 97
 					case 'C':
98 98
 						$currentTrx['credit_debit'] = static::CD_CREDIT;
99 99
 						break;
Please login to merge, or discard this patch.
Braces   +3 added lines, -1 removed lines patch added patch discarded remove patch
@@ -147,7 +147,9 @@
 block discarded – undo
147 147
 				}
148 148
 			}
149 149
 			$statement['transactions'] = $transactions;
150
-			if (count($transactions) > 0) $result[] = $statement;
150
+			if (count($transactions) > 0) {
151
+			    $result[] = $statement;
152
+			}
151 153
 		}
152 154
 
153 155
 		return $result;
Please login to merge, or discard this patch.
lib/Tests/Fhp/Parser/MT940Test.php 1 patch
Indentation   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -6,37 +6,37 @@
 block discarded – undo
6 6
 
7 7
 class MT940Test extends \PHPUnit_Framework_TestCase
8 8
 {
9
-	/**
10
-	 * Parser must be able to parse description lines starting with "-".
11
-	 */
12
-	public function testParseDescriptionLines()
13
-	{
14
-		$rawData = [
15
-			":20:STARTUMSE",
16
-			":25:00000000/0221122370",
17
-			":28C:00000/001",
18
-			":60F:C170428EUR6670,54",
19
-			":61:1705030503CR223,72N062NONREF",
20
-			":86:166?00GUTSCHRIFT?109251?20EREF+CCB.122.UE.266455?21SVWZ+Re 17",
21
-			"-H-0005 vom 24.04?22.2017?30DRESDEFF850?31DE00000000000000000000?",
22
-			"32TEST TEST GBR",
23
-			":62F:C170503EUR6894,26",
24
-			"-"
25
-		];
9
+    /**
10
+     * Parser must be able to parse description lines starting with "-".
11
+     */
12
+    public function testParseDescriptionLines()
13
+    {
14
+        $rawData = [
15
+            ":20:STARTUMSE",
16
+            ":25:00000000/0221122370",
17
+            ":28C:00000/001",
18
+            ":60F:C170428EUR6670,54",
19
+            ":61:1705030503CR223,72N062NONREF",
20
+            ":86:166?00GUTSCHRIFT?109251?20EREF+CCB.122.UE.266455?21SVWZ+Re 17",
21
+            "-H-0005 vom 24.04?22.2017?30DRESDEFF850?31DE00000000000000000000?",
22
+            "32TEST TEST GBR",
23
+            ":62F:C170503EUR6894,26",
24
+            "-"
25
+        ];
26 26
 
27
-		$parser = new MT940(implode("\r\n", $rawData));
28
-		$result = $parser->parse(MT940::TARGET_ARRAY);
29
-		$this->assertEquals(
30
-			'EREF+CCB.122.UE.266455SVWZ+Re 17-H-0005 vom 24.04.2017',
31
-			$result[0]['transactions'][0]['description']['description_1']
32
-		);
33
-		$this->assertEquals(
34
-			'166',
35
-			$result[0]['transactions'][0]['transaction_code']
36
-		);
37
-		$this->assertEquals(
38
-			'223.72',
39
-			$result[0]['transactions'][0]['amount']
40
-		);
41
-	}
27
+        $parser = new MT940(implode("\r\n", $rawData));
28
+        $result = $parser->parse(MT940::TARGET_ARRAY);
29
+        $this->assertEquals(
30
+            'EREF+CCB.122.UE.266455SVWZ+Re 17-H-0005 vom 24.04.2017',
31
+            $result[0]['transactions'][0]['description']['description_1']
32
+        );
33
+        $this->assertEquals(
34
+            '166',
35
+            $result[0]['transactions'][0]['transaction_code']
36
+        );
37
+        $this->assertEquals(
38
+            '223.72',
39
+            $result[0]['transactions'][0]['amount']
40
+        );
41
+    }
42 42
 }
Please login to merge, or discard this patch.