[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
Pipfile
.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;
class Validator {
static $regex;
static $exactRegex;
static $regexes;
public static function init(){
if(!self::$regex) {
self::$regex = "/" . trim(file_get_contents('regex.txt')) . "/";
self::$exactRegex = "/^" . trim(file_get_contents('regex.txt')) . "$/";
public static function init() {
if(!self::$regexes) {
self::$regexes = array_map(function($line) {
return '/' . trim($line) . '/';
}, array_filter(file('regexes.txt')));
}
}
public static function validate(string $pin) {
self::init();
fwrite(STDERR, var_dump(self::$exactRegex, TRUE));
if (strlen($pin) === 6 and preg_match(self::$exactRegex, $pin) === 1) {
return true;
}
return false;
}
public static function search(string $address) {
/**
* Validate a PIN code
* @param string $pin
* @return bool
*/
public static function validate(string $pin) : bool{
self::init();
preg_match_all(self::$regex, $address, $matches);
return array_map(function($match) {
return $match[0];
}, $matches);
foreach (self::$regexes as $regex) {
if (strlen($pin) === 6 and preg_match($regex, $pin) === 1) {
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 () {
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() {
// $this->assertSame(P::search("my pincode is 560029"), ["560029"]);
// $this->assertSame(P::search("560029"), ["560029"]);
// $this->assertSame(P::search("560029 244713"), ["560029", "244713"]);
// $this->assertSame(P::search("address 560038 bangalore"), ["560038"]);
// }
public function testSearch() {
$this->assertSame(P::search("560029"), ["560029"]);
$this->assertSame(P::search("my pincode is 560029"), ["560029"]);
$this->assertSame(P::search("560029 244713"), ["244713", "560029"]);
$this->assertSame(P::search("address 560038 bangalore"), ["560038"]);
}
}