Migrating from PHP

laravel-rust-ogm Alpha Laravel for Rust

This page maps each ruudvdd/ogm call to its Rust equivalent and lists where the Rust behaviour differs on purpose.

Calls

PHPRust
(new Generator)->generate()let ogm = Ogm::generate(); ogm.digits()
(new Generator)->generate(true)Ogm::generate().to_string()
(new Generator)->generate(false, '2026')let ogm = Ogm::generate_with_prefix("2026")?; ogm.digits()
(new Generator)->generate(true, '0909337554')Ogm::from_base("0909337554")?.to_string()
TooMuchDigits exceptionErr(OgmError::PrefixTooLong { .. })
(new Validator)->isValid($ogm)is_valid_structured(ogm) or Ogm::from_structured(ogm)
(new Validator)->digitsAreValid($number)digits_are_valid(number) or Ogm::from_digits(number)
noneOgm::parse(input) / is_valid(input): any of the four forms, whitespace trimmed
bin/ogm generate [PREFIX]no CLI; Ogm::generate_with_prefix(prefix)?.to_string() (formatted by default)
bin/ogm generate -c [PREFIX]no CLI; let ogm = Ogm::generate_with_prefix(prefix)?; ogm.digits()
bin/ogm validate OGMno CLI; is_valid_structured(ogm)
bin/ogm validate -c OGMno CLI; digits_are_valid(digits)

Rust returns an Ogm value instead of a string. Keep it as the type in your own code and format it when you display or store it.

The random digits are not reproduced: the port does not use PHP's seeded mt_rand sequence. A 10-digit prefix gives the same reference as PHP. With a shorter prefix, PHP's output and Rust's output each have the right prefix and shape and pass the mod-97 check on their own; their random digits are unrelated and are the same only by chance.

Differences

Each one is a diverged row in the package's parity.toml.

  • ***XXX/XXXX/XXXXX*** is valid. PHP's isValid rejects it because of a str_replace bug, although its docs list both delimiters. Rust accepts both.
  • No trailing newline in strict mode. PHP's regex $ matches before a final \n, so PHP accepts "+++090/9337/55493+++\n". from_structured rejects it; Ogm::parse trims it.
  • Only ASCII digits. PHP's digitsAreValid casts with (int), so abcdefghij97 passes. Rust returns OgmError::InvalidCharacters.
  • Prefix type and coercion. PHP takes a string, int or null and throws InvalidArgumentException for other types, but silently coerces leading spaces (" 123456789" → " 12345678939") and negative integers (-123456789 → "-123456789-39") without warning, and throws TooMuchDigits before coercion on overlength non-digit prefixes ("1234567890x"). Rust takes &str, checks ASCII digits before length, and returns OgmError::InvalidPrefix for any non-digit character. Use Ogm::generate() for no prefix.
  • No CLI. The crate is a library only.