IsoCodes\Ssn::generate PHP Method

generate() public static method

Generate an SSN based on state.
public static generate ( mixed $state = false, string $separator = '-' ) : false | string
$state mixed
$separator string
return false | string (false: bad state found)
    public static function generate($state = false, $separator = '-')
    {
        if (!static::$initialized) {
            static::initialize();
            static::$initialized = true;
        }
        $states = static::$states;
        $statePrefixes = static::$statePrefixes;
        $highgroup = static::$highgroup;
        $possibleGroups = static::$possibleGroups;
        if ($state === false) {
            $state = $states[mt_rand(0, count($states) - 1)];
        }
        $state = strtoupper($state);
        // Sanity check: is this a valid state?
        if (!isset($statePrefixes[$state])) {
            return false;
        }
        // Generate area number
        $area = $statePrefixes[$state][array_rand($statePrefixes[$state])];
        // Generate group number
        $group = $possibleGroups[mt_rand(0, array_search($highgroup[$area], $possibleGroups))];
        // Generate valid group number
        // Generate last four
        $lastfour = sprintf('%04s', trim(mt_rand(0, 9999)));
        return sprintf('%03s', $area) . $separator . sprintf('%02s', $group) . $separator . $lastfour;
    }