From 66deaddfd678173cde4e5a745a9c29991e507cd7 Mon Sep 17 00:00:00 2001 From: Craig Paul Date: Tue, 25 Oct 2016 19:33:48 -0600 Subject: [PATCH] Extracts prepare method to preparable trait --- src/Preparable.php | 53 +++++++++++++++++++++++++++++++++++++++++++++ src/Receipt.php | 54 ++++------------------------------------------ 2 files changed, 57 insertions(+), 50 deletions(-) create mode 100644 src/Preparable.php diff --git a/src/Preparable.php b/src/Preparable.php new file mode 100644 index 0000000..06a587a --- /dev/null +++ b/src/Preparable.php @@ -0,0 +1,53 @@ +$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; + } +} \ No newline at end of file diff --git a/src/Receipt.php b/src/Receipt.php index d2afe64..60143db 100644 --- a/src/Receipt.php +++ b/src/Receipt.php @@ -4,6 +4,8 @@ class Receipt { + use Preparable; + /** * @var array */ @@ -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'], @@ -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; + ]); } /**