Skip to content
This repository has been archived by the owner on Oct 30, 2020. It is now read-only.

Commit

Permalink
Extracts prepare method to preparable trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Paul committed Oct 26, 2016
1 parent eb01acf commit 66deadd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 50 deletions.
53 changes: 53 additions & 0 deletions src/Preparable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace CraigPaul\Moneris;

trait Preparable
{
/**
* Prepare the receipt data.
*
* @param $data
*
* @return array
*/
protected function prepare($data, array $params)
{
$array = [];

foreach ($params as $param) {
$key = $param['key'];
$property = $param['property'];

$array[$property] = isset($data->$key) && !is_null($data->$key) ? $data->$key : null;

if (isset($param['cast'])) {
switch ($param['cast']) {
case 'boolean':
$array[$property] = isset($array[$property]) ? (is_string($array[$property]) ? $array[$property] : $array[$property]->__toString()) : null;
$array[$property] = isset($array[$property]) && !is_null($array[$property]) ? ($array[$property] === 'true' ? true : false) : false;

break;
case 'float':
$array[$property] = isset($array[$property]) ? (is_string($array[$property]) ? floatval($array[$property]) : floatval($array[$property]->__toString())) : null;

break;
case 'string':
$array[$property] = isset($array[$property]) ? (is_string($array[$property]) ? $array[$property] : $array[$property]->__toString()) : null;

break;
case 'array':
$array[$property] = (array)$array[$property];
}
}

if (isset($param['callback'])) {
$callback = $param['callback'];

$array[$property] = $this->$callback($array[$property]);
}
}

return $array;
}
}
54 changes: 4 additions & 50 deletions src/Receipt.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class Receipt
{
use Preparable;

/**
* @var array
*/
Expand All @@ -16,20 +18,7 @@ class Receipt
*/
public function __construct($data)
{
$this->data = $this->prepare($data);
}

/**
* Prepare the receipt data.
*
* @param $data
*
* @return array
*/
protected function prepare($data)
{
$array = [];
$params = [
$this->data = $this->prepare($data, [
['property' => 'amount', 'key' => 'TransAmount', 'cast' => 'float'],
['property' => 'authorization', 'key' => 'AuthCode', 'cast' => 'string'],
['property' => 'avs_result', 'key' => 'AvsResultCode', 'cast' => 'string'],
Expand All @@ -47,42 +36,7 @@ protected function prepare($data)
['property' => 'time', 'key' => 'TransTime', 'cast' => 'string'],
['property' => 'transaction', 'key' => 'TransID', 'cast' => 'string'],
['property' => 'type', 'key' => 'TransType', 'cast' => 'string'],
];

foreach ($params as $param) {
$key = $param['key'];
$property = $param['property'];

$array[$property] = isset($data->$key) && !is_null($data->$key) ? $data->$key : null;

if (isset($param['cast'])) {
switch ($param['cast']) {
case 'boolean':
$array[$property] = isset($array[$property]) ? $array[$property]->__toString() : null;
$array[$property] = isset($array[$property]) && !is_null($array[$property]) ? ($array[$property] === 'true' ? true : false) : false;

break;
case 'float':
$array[$property] = isset($array[$property]) ? floatval($array[$property]->__toString()) : null;

break;
case 'string':
$array[$property] = isset($array[$property]) ? $array[$property]->__toString() : null;

break;
case 'array':
$array[$property] = (array)$array[$property];
}
}

if (isset($param['callback'])) {
$callback = $param['callback'];

$array[$property] = $this->$callback($array[$property]);
}
}

return $array;
]);
}

/**
Expand Down

0 comments on commit 66deadd

Please sign in to comment.