File: /www/exchange0old/exchange/admin/validator/Transaction.php
<?php
/**
* validator - Transaction.php
*
* Initial version by: MisterX
* Initial version created on: 09.12.2015
*/
class Transaction implements ValidationData
{
const REGEXP = '|:(\w*):([^:]*)|im';
protected $raw = array();
protected $source = '';
protected $values = array();
protected $errors=array();
function __construct($string=null)
{
if($string!==null){
$this->parseFromString($string);
}
}
/**
* @return string
*/
public function getSource()
{
return $this->source;
}
public function parseFromString($string){
$result = array();
$this->source = $string;
preg_match_all(self::REGEXP,$string,$result);
foreach($result[1] as $index=>$field){
$value = trim($result[2][$index]);
$this->values['raw'][$field] = $value;
$parserMethod = 'parse'.strtoupper($field);
if(method_exists($this,$parserMethod)){
$this->values['data'][$field] = $this->{$parserMethod}($value);
}else{
$this->values['data'][$field] = $value;
}
}
}
public function isBatch(){
return (bool)$this->getBatchNumber();
}
public function getAmount(){
return $this->getData('32A.amount');
}
public function getCurrency(){
return $this->getData('32A.currency');
}
public function getBatchNumber(){
return isset($this->values['data']['20']['batch'])?$this->values['data']['20']['batch']:null;
}
public function getTransactionId(){
return $this->getData('70.number');
}
public function getAccountFrom()
{
return $this->getData('50A');
}
public function getAccountTo()
{
return $this->getData('59.account');
}
protected function parse32A($data){
$return = array('date'=>null,'currency'=>null,'amount'=>null);
$results = array();
preg_match_all('|([\d]{6})([A-Z]{3})(.*)|i',$data,$results);
if(count($results)!=4 or !count($results[0])){
return $return;
}
//Change comma
$amount = str_ireplace(',','.',$results[3][0]);
return array('date'=>$results[1][0],'currency'=>$results[2][0],'amount'=>(float)$amount);
}
protected function parse50A($data){
return ltrim($data,'/');
}
protected function parse57D($data){
return ltrim($data,'/');
}
protected function parse59($data){
$data = $this->parse50A($data);
$result = explode("\n",$data,2);
if(count($result)==2){
return array('account'=>trim($result[0]),'name'=>trim($result[1]));
}else{
return array('account'=>trim($result[0]),'name'=>null);
}
}
protected function parse70($data){
$results = array();
preg_match_all('|([\S\s]*)\/([\d]*)|',$data,$results);
if(count($results)!=3){
return null;
}
return array('number'=>$results[2][0],'reference'=>$results[1][0]);
}
protected function parse20($data){
$result = explode('/',$data);
if(count($result)==2){
list($reference,$batch) = $result;
}else{
$reference = $result[0];
$batch = '';
}
return array('batch'=>trim($batch),'reference'=>trim($reference));
}
public function getFieldValue($field)
{
return $this->getByPath($field,$this->values);
}
protected function getByPath($path,$values){
$exploded = explode('.',$path);
$temp = $values;
foreach($exploded as $key) {
$temp = &$temp[$key];
}
return $temp;
}
public function getData($field){
return $this->getByPath($field,$this->values['data']);
}
public function getRaw($field){
return $this->getByPath($field,$this->values['raw']);
}
public function setErrors(array $errors)
{
$this->errors = $errors;
}
public function getErrors()
{
return $this->errors;
}
public function hasErrors()
{
return (bool)$this->errors;
}
}