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

Commit

Permalink
Adds valid and invalid checks. Adds static validator methods for extr…
Browse files Browse the repository at this point in the history
…action
  • Loading branch information
Craig Paul committed Oct 19, 2016
1 parent 199e6e6 commit 0eb47a9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class Transaction
{
use Gettable;

/**
* The errors for the transaction.
*
* @var array
*/
protected $errors;

/**
* The Gateway instance.
*
Expand Down Expand Up @@ -39,6 +46,16 @@ public function __construct(Gateway $gateway, array $params = [], $prepare = tru
$this->params = $prepare ? $this->prepare($params) : $params;
}

/**
* Check that the required parameters have not been provided to the transaction.
*
* @return bool
*/
public function invalid()
{
return !$this->valid();
}

/**
* Prepare the transaction parameters.
*
Expand All @@ -64,4 +81,38 @@ protected function prepare(array $params)

return $params;
}

/**
* Check that the required parameters have been provided to the transaction.
*
* @return bool
*/
public function valid()
{
$params = $this->params;
$errors = [];

$errors[] = Validator::empty($params) ? 'No parameters provided.' : null;

if (isset($params['type'])) {
switch ($params['type']) {
case 'purchase':
$errors[] = Validator::set($params, 'order_id') ? null : 'Order Id not provided.';
$errors[] = Validator::set($params, 'pan') ? null : 'Credit card number not provided.';
$errors[] = Validator::set($params, 'amount') ? null : 'Amount not provided.';
$errors[] = Validator::set($params, 'expdate') ? null : 'Expiry date not provided.';

break;
default:
$errors[] = $params['type'].' is not a supported transaction type.';
}
} else {
$errors[] = 'Transaction type not provided.';
}

$errors = array_filter($errors);
$this->errors = $errors;

return empty($errors);
}
}
27 changes: 27 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace CraigPaul\Moneris;

class Validator
{
/**
* @param array $array
*
* @return bool
*/
public static function empty(array $array = [])
{
return empty($array);
}

/**
* @param array $array
* @param string $key
*
* @return bool
*/
public static function set(array $array, string $key)
{
return isset($array[$key]);
}
}

0 comments on commit 0eb47a9

Please sign in to comment.