Created
April 6, 2019 18:13
-
-
Save miholeus/13183e944fa43c2aa720047dc9f4a9e7 to your computer and use it in GitHub Desktop.
regex yandex money
This file contains 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 | |
// responses in different formats | |
$r1 = "Пароль: 0188 | |
Перевод на счет 41001312169904 | |
Спишется 10,06р. | |
"; | |
$r2 = "Пароль: 0188 | |
Спишется 10,06р. | |
Перевод на счет 41001312169904 | |
"; | |
$r3 = "Спишется 10,06р. | |
Перевод на счет 41001312169904 | |
Пароль: 0188 | |
"; | |
$r4 = "Спишется 10,06р. | |
Пароль: 0188 | |
Перевод на счет 41001312169904 | |
"; | |
$r5 = "Перевод на счет 41001312169904 | |
Спишется 10,06р. | |
Пароль: 0188 | |
"; | |
$r6 = "Перевод на счет 41001312169904 | |
Пароль: 0188 | |
Спишется 10,06р. | |
"; | |
$r7 = "Перевод на счет 41001312169904 | |
Пароль 0188 | |
Спишется 10,06руб. | |
"; | |
$r8 = "Перевод денег на счет 41001312169904 | |
Пароль 0188 | |
Будет списано 10,06руб. | |
"; | |
$r9 = "Недостаточно средств."; | |
$r10 = "Сумма указана неверно."; | |
$r11 = "Кошелек Яндекс.Денег указан неверно."; | |
class YandexMoneyResponse | |
{ | |
private $code; | |
private $sum; | |
private $wallet; | |
public function __construct(string $code, string $sum, string $wallet) | |
{ | |
$this->code = $code; | |
$this->sum = floatval(str_replace(",", ".", $sum)); | |
$this->wallet = $wallet; | |
} | |
public function getCode(): string | |
{ | |
return $this->code; | |
} | |
public function getSum(): float | |
{ | |
return $this->sum; | |
} | |
public function getWallet(): string | |
{ | |
return $this->wallet; | |
} | |
public function isValid(): bool | |
{ | |
if ("" === $this->code) { | |
throw new \RunTimeException("Code is invalid"); | |
} | |
if (0 === $this->sum) { | |
throw new \RunTimeException("Sum is invalid"); | |
} | |
if ("" === $this->wallet) { | |
throw new \RunTimeException("Wallet is invalid"); | |
} | |
return true; | |
} | |
} | |
// main function | |
function parseResponse(string $text): YandexMoneyResponse { | |
preg_match_all("~(?<wallet>41001\d{9,10})|(?<sum>(\d+([.,]\d{1,2})?))\D+\.|(?<code>\d{4})(?!\w)~u", $text, $m); | |
if (empty($m['sum']) || empty($m['code']) || empty($m['wallet'])) { | |
throw new \RunTimeException(sprintf("Error: %s", $text)); | |
} | |
$wallet = current(array_diff($m['wallet'], [""])); | |
$sum = current(array_diff($m['sum'], [""])); | |
$code = current(array_diff($m['code'], [""])); | |
$response = new YandexMoneyResponse($code, $sum, $wallet); | |
return $response; | |
} | |
// check | |
foreach (['r1' => $r1, 'r2' => $r2, 'r3' => $r3, 'r4' => $r4, 'r5' => $r5, 'r6' => $r6, 'r7' => $r7, 'r8' => $r8, 'r9' => $r9, 'r10' => $r10, 'r11' => $r11] as $key => $text) { | |
try { | |
$response = parseResponse($text); | |
if ($response->isValid()) { | |
printf("%s => OK!" . PHP_EOL, $key); | |
} | |
} catch (\RunTimeException $e) { | |
printf("%s => %s", $key, $e->getMessage() . PHP_EOL); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment