Generator Changes at 9/27/2025 11:07:17 PM

This commit is contained in:
MeysamMoghaddam
2025-09-27 23:48:41 +03:30
parent 447e580a8a
commit a1b6e28d35
45 changed files with 1320 additions and 18 deletions
@@ -0,0 +1,38 @@
using System.Security.Cryptography;
namespace CMSMicroservice.Application.Common.Extensions;
public static class UtilExtensions
{
public static string NormalizeIranMobile(this string input)
{
if (string.IsNullOrWhiteSpace(input)) throw new ArgumentException("mobile is empty");
var m = new string(input.Where(char.IsDigit).ToArray());
if (m.StartsWith("0098")) m = m[4..];
else if (m.StartsWith("098")) m = m[3..];
else if (m.StartsWith("98")) m = m[2..];
if (!m.StartsWith("0")) m = "0" + m;
if (m.Length != 11 || !m.StartsWith("09"))
throw new ArgumentException("شماره موبایل نامعتبر است.");
return m;
}
public static string Generate(int digits = 10, bool firstDigitNonZero = false)
{
if (digits <= 0) throw new ArgumentOutOfRangeException(nameof(digits));
var chars = new char[digits];
// رقم اول
if (firstDigitNonZero)
chars[0] = (char)('0' + RandomNumberGenerator.GetInt32(1, 10)); // 1..9
else
chars[0] = (char)('0' + RandomNumberGenerator.GetInt32(10)); // 0..9
// بقیه ارقام
for (int i = 1; i < digits; i++)
chars[i] = (char)('0' + RandomNumberGenerator.GetInt32(10));
return new string(chars);
}
}