Cake\Localized\Validation\PlValidation::regon PHP Method

regon() public static method

Checks REGON National Business Registry Number in Poland
public static regon ( string $check ) : boolean
$check string Value to check
return boolean Success.
    public static function regon($check)
    {
        $pattern = '/^[0-9]{9}$/';
        if (preg_match($pattern, $check)) {
            $sum = 0;
            $weights = [8, 9, 2, 3, 4, 5, 6, 7];
            for ($i = 0; $i < 8; $i++) {
                $sum += $check[$i] * $weights[$i];
            }
            $control = $sum % 11;
            if ($control == 10) {
                $control = 0;
            }
            if ($check[8] == $control) {
                return true;
            }
        }
        return false;
    }

Usage Example

Example #1
0
 /**
  * Test the regon method of PlValidation
  *
  * @return void
  */
 public function testRegon()
 {
     $this->assertTrue(PlValidation::regon('590096454'));
     $this->assertFalse(PlValidation::regon('590096453'));
     $this->assertFalse(PlValidation::regon('591096454'));
 }