Last active
October 8, 2016 22:05
-
-
Save num8er/3c3fa38e0d287046b3990ada30cc28e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$string=" | |
Спишется 50,26р. Пароль 6675 | |
Перевод на счет 41001830685343"; | |
interface IConfirmationSMS { | |
public function setConfirmationCode($confirmation_code); | |
public function getConfirmationCode(); | |
public function setPurseId($purse_id); | |
public function getPurseId(); | |
public function setPaymentAmount($payment_amount); | |
public function getPaymentAmount(); | |
} | |
interface IConfirmationSMSParser { | |
public function parse($message, IConfirmationSMS $confirmationSMS); | |
} | |
class YMConfirmationSMS implements IConfirmationSMS { | |
private $data = []; | |
public function setConfirmationCode($confirmation_code) { | |
$this->data['confirmation_code'] = $confirmation_code; | |
} | |
public function getConfirmationCode() { | |
return $this->data['confirmation_code'] ?? null; | |
} | |
public function setPurseId($purse_id) { | |
$this->data['purse_id'] = $purse_id; | |
} | |
public function getPurseId() { | |
return $this->data['purse_id'] ?? null; | |
} | |
public function setPaymentAmount($payment_amount) { | |
$this->data['payment_amount'] = $payment_amount; | |
} | |
public function getPaymentAmount() { | |
return $this->data['payment_amount'] ?? null; | |
} | |
} | |
class YMConfirmationSMSParser implements IConfirmationSMSParser { | |
public function parse($message, IConfirmationSMS $confirmationSMS) { | |
$confirmationSMS->setConfirmationCode($this->extractConfirmationCode($message)); | |
$confirmationSMS->setPurseId($this->extractPurseId($message)); | |
$confirmationSMS->setPaymentAmount($this->extractPaymentAmount($message)); | |
return $confirmationSMS; | |
} | |
private function extractNumericValues($string) { | |
preg_match_all('/((?:[0-9]+,)*[0-9]+(?:\.[0-9]+)?)/', $string, $matches); | |
return $matches[0] ?? []; | |
} | |
private function findIntegerByLength($string, $length) { | |
$numericValues = $this->extractNumericValues($string); | |
foreach($numericValues AS $value) { | |
if((int)$value == (float)$value AND strlen($value) == $length) { | |
return (int)$value; | |
} | |
} | |
return null; | |
} | |
private function findFloat($string) { | |
$numericValues = $this->extractNumericValues($string); | |
foreach($numericValues AS $value) { | |
$value = str_replace(',', '.', $value); | |
if((int)$value != (float)$value) { | |
return (float)$value; | |
} | |
} | |
return null; | |
} | |
private function extractConfirmationCode($string) { | |
return $this->findIntegerByLength($string, 4); | |
} | |
private function extractPurseId($string) { | |
return $this->findIntegerByLength($string, 14); | |
} | |
private function extractPaymentAmount($string) { | |
return $this->findFloat($string); | |
} | |
} | |
$confirmationSMSParser = new YMConfirmationSMSParser(); | |
$confirmationSMS = $confirmationSMSParser->parse($string, new YMConfirmationSMS()); | |
echo 'code: '.$confirmationSMS->getConfirmationCode().'<br/>'; | |
echo 'purse: '.$confirmationSMS->getPurseId().'<br/>'; | |
echo 'amount: '.$confirmationSMS->getPaymentAmount().'<br/>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment