2020-02-24 18:02:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PIN;
|
|
|
|
|
|
|
|
class Validator {
|
2023-07-02 08:51:35 +00:00
|
|
|
static $regexes;
|
2023-07-01 14:30:02 +00:00
|
|
|
|
2023-07-02 08:51:35 +00:00
|
|
|
public static function init() {
|
|
|
|
if(!self::$regexes) {
|
|
|
|
self::$regexes = array_map(function($line) {
|
|
|
|
return '/' . trim($line) . '/';
|
|
|
|
}, array_filter(file('regexes.txt')));
|
2023-07-01 14:30:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-13 12:19:02 +00:00
|
|
|
|
2023-07-02 08:51:35 +00:00
|
|
|
/**
|
|
|
|
* Validate a PIN code
|
|
|
|
* @param string $pin
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function validate(string $pin) : bool{
|
|
|
|
self::init();
|
2020-02-24 18:02:18 +00:00
|
|
|
|
2023-07-02 08:51:35 +00:00
|
|
|
foreach (self::$regexes as $regex) {
|
|
|
|
if (strlen($pin) === 6 and preg_match($regex, $pin) === 1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 18:02:18 +00:00
|
|
|
|
2023-07-02 08:51:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-07-01 14:30:02 +00:00
|
|
|
|
2023-07-02 08:51:35 +00:00
|
|
|
/**
|
|
|
|
* Search for PIN codes in a string
|
|
|
|
* @param string $address
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function search(string $address){
|
2023-07-01 14:30:02 +00:00
|
|
|
self::init();
|
2023-07-02 08:51:35 +00:00
|
|
|
$results = [];
|
|
|
|
|
|
|
|
foreach (self::$regexes as $regex) {
|
|
|
|
preg_match_all($regex, $address, $matches);
|
|
|
|
|
|
|
|
$results = array_reduce($matches, function($res, $match) {
|
2023-07-02 09:20:58 +00:00
|
|
|
if (isset($match[0]) and in_array($match[0], $res, true) === false){
|
2023-07-02 08:51:35 +00:00
|
|
|
$res[] = $match[0];
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}, $results);
|
|
|
|
}
|
2023-07-01 14:30:02 +00:00
|
|
|
|
2023-07-02 09:20:58 +00:00
|
|
|
return array_values($results);
|
2023-07-01 14:30:02 +00:00
|
|
|
}
|
2020-03-13 12:19:02 +00:00
|
|
|
}
|