[php] Get PHP functional

This commit is contained in:
Nemo 2023-07-02 14:21:35 +05:30
parent fc6bfdb113
commit 79b3302227
6 changed files with 64 additions and 38 deletions

View File

@ -13,3 +13,4 @@ src/pincode
*.toml *.toml
Pipfile Pipfile
.editorconfig .editorconfig
regexes.txt

9
regex.txt generated

File diff suppressed because one or more lines are too long

8
regexes.txt Normal file

File diff suppressed because one or more lines are too long

View File

@ -3,34 +3,53 @@
namespace PIN; namespace PIN;
class Validator { class Validator {
static $regex; static $regexes;
static $exactRegex;
public static function init(){ public static function init() {
if(!self::$regex) { if(!self::$regexes) {
self::$regex = "/" . trim(file_get_contents('regex.txt')) . "/"; self::$regexes = array_map(function($line) {
self::$exactRegex = "/^" . trim(file_get_contents('regex.txt')) . "$/"; return '/' . trim($line) . '/';
}, array_filter(file('regexes.txt')));
} }
} }
public static function validate(string $pin) { /**
self::init(); * Validate a PIN code
* @param string $pin
fwrite(STDERR, var_dump(self::$exactRegex, TRUE)); * @return bool
*/
if (strlen($pin) === 6 and preg_match(self::$exactRegex, $pin) === 1) { public static function validate(string $pin) : bool{
return true;
}
return false;
}
public static function search(string $address) {
self::init(); self::init();
preg_match_all(self::$regex, $address, $matches);
return array_map(function($match) { foreach (self::$regexes as $regex) {
return $match[0]; if (strlen($pin) === 6 and preg_match($regex, $pin) === 1) {
}, $matches); return true;
}
}
return false;
}
/**
* Search for PIN codes in a string
* @param string $address
* @return array
*/
public static function search(string $address){
self::init();
$results = [];
foreach (self::$regexes as $regex) {
preg_match_all($regex, $address, $matches);
$results = array_reduce($matches, function($res, $match) {
if ($match[0] and in_array($match[0], $res, true) === false){
$res[] = $match[0];
}
return $res;
}, $results);
}
return $results;
} }
} }

View File

@ -32,5 +32,10 @@ readInterface.on("line", function (line) {
}); });
readInterface.on("close", function () { readInterface.on("close", function () {
console.log("(" + regexes.join('|') + ")"); // We maintain two files, one with a single regex using (|) for each segment
fs.writeFileSync("regex.txt", "(" + regexes.join('|') + ")");
// And another with 8 regexes, one for each area code.
// The latter is required for some languages, which have a regex character limit
// (Currently PHP)
fs.writeFileSync("regexes.txt", regexes.join("\n"));
}); });

View File

@ -18,10 +18,10 @@ class SimpleTest extends TestCase {
} }
} }
// public function testSearch() { public function testSearch() {
// $this->assertSame(P::search("my pincode is 560029"), ["560029"]); $this->assertSame(P::search("560029"), ["560029"]);
// $this->assertSame(P::search("560029"), ["560029"]); $this->assertSame(P::search("my pincode is 560029"), ["560029"]);
// $this->assertSame(P::search("560029 244713"), ["560029", "244713"]); $this->assertSame(P::search("560029 244713"), ["244713", "560029"]);
// $this->assertSame(P::search("address 560038 bangalore"), ["560038"]); $this->assertSame(P::search("address 560038 bangalore"), ["560038"]);
// } }
} }