Skip to content

Commit

Permalink
Introduce SwedishPersonalIdentityNumber (#6)
Browse files Browse the repository at this point in the history
- SwedishPersonalNumber is now discontinued
  • Loading branch information
Kralizek authored Feb 4, 2021
1 parent 4b06544 commit 19ce82d
Show file tree
Hide file tree
Showing 17 changed files with 566 additions and 421 deletions.
60 changes: 30 additions & 30 deletions DotNetTypes.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{FA9762F1-6DC4-4722-8A92-962C1EDA0557}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{38E7358A-E518-4E9D-AB95-3D6BA0F97CE0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwedishPersonalNumber", "src\SwedishPersonalNumber\SwedishPersonalNumber.csproj", "{973F55C0-3CF0-4810-873E-ED48E6F52042}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwedishPersonalIdentityNumber", "src\SwedishPersonalIdentityNumber\SwedishPersonalIdentityNumber.csproj", "{200F4052-CB53-4151-8E4C-D430C653255B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{8768CBBD-D786-4FBA-9F42-B49D093DC30D}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{F742414A-CF15-46C5-8EC8-1962F15FB76B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.SwedishPersonalNumber", "tests\Tests.SwedishPersonalNumber\Tests.SwedishPersonalNumber.csproj", "{FDC03A90-AC82-499A-A5F1-C467B080C46B}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests.SwedishPersonalIdentityNumber", "tests\Tests.SwedishPersonalIdentityNumber\Tests.SwedishPersonalIdentityNumber.csproj", "{B24DF638-F187-427C-863E-26A04272CF40}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -24,33 +24,33 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{973F55C0-3CF0-4810-873E-ED48E6F52042}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{973F55C0-3CF0-4810-873E-ED48E6F52042}.Debug|Any CPU.Build.0 = Debug|Any CPU
{973F55C0-3CF0-4810-873E-ED48E6F52042}.Debug|x64.ActiveCfg = Debug|Any CPU
{973F55C0-3CF0-4810-873E-ED48E6F52042}.Debug|x64.Build.0 = Debug|Any CPU
{973F55C0-3CF0-4810-873E-ED48E6F52042}.Debug|x86.ActiveCfg = Debug|Any CPU
{973F55C0-3CF0-4810-873E-ED48E6F52042}.Debug|x86.Build.0 = Debug|Any CPU
{973F55C0-3CF0-4810-873E-ED48E6F52042}.Release|Any CPU.ActiveCfg = Release|Any CPU
{973F55C0-3CF0-4810-873E-ED48E6F52042}.Release|Any CPU.Build.0 = Release|Any CPU
{973F55C0-3CF0-4810-873E-ED48E6F52042}.Release|x64.ActiveCfg = Release|Any CPU
{973F55C0-3CF0-4810-873E-ED48E6F52042}.Release|x64.Build.0 = Release|Any CPU
{973F55C0-3CF0-4810-873E-ED48E6F52042}.Release|x86.ActiveCfg = Release|Any CPU
{973F55C0-3CF0-4810-873E-ED48E6F52042}.Release|x86.Build.0 = Release|Any CPU
{FDC03A90-AC82-499A-A5F1-C467B080C46B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FDC03A90-AC82-499A-A5F1-C467B080C46B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FDC03A90-AC82-499A-A5F1-C467B080C46B}.Debug|x64.ActiveCfg = Debug|Any CPU
{FDC03A90-AC82-499A-A5F1-C467B080C46B}.Debug|x64.Build.0 = Debug|Any CPU
{FDC03A90-AC82-499A-A5F1-C467B080C46B}.Debug|x86.ActiveCfg = Debug|Any CPU
{FDC03A90-AC82-499A-A5F1-C467B080C46B}.Debug|x86.Build.0 = Debug|Any CPU
{FDC03A90-AC82-499A-A5F1-C467B080C46B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FDC03A90-AC82-499A-A5F1-C467B080C46B}.Release|Any CPU.Build.0 = Release|Any CPU
{FDC03A90-AC82-499A-A5F1-C467B080C46B}.Release|x64.ActiveCfg = Release|Any CPU
{FDC03A90-AC82-499A-A5F1-C467B080C46B}.Release|x64.Build.0 = Release|Any CPU
{FDC03A90-AC82-499A-A5F1-C467B080C46B}.Release|x86.ActiveCfg = Release|Any CPU
{FDC03A90-AC82-499A-A5F1-C467B080C46B}.Release|x86.Build.0 = Release|Any CPU
{200F4052-CB53-4151-8E4C-D430C653255B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{200F4052-CB53-4151-8E4C-D430C653255B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{200F4052-CB53-4151-8E4C-D430C653255B}.Debug|x64.ActiveCfg = Debug|Any CPU
{200F4052-CB53-4151-8E4C-D430C653255B}.Debug|x64.Build.0 = Debug|Any CPU
{200F4052-CB53-4151-8E4C-D430C653255B}.Debug|x86.ActiveCfg = Debug|Any CPU
{200F4052-CB53-4151-8E4C-D430C653255B}.Debug|x86.Build.0 = Debug|Any CPU
{200F4052-CB53-4151-8E4C-D430C653255B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{200F4052-CB53-4151-8E4C-D430C653255B}.Release|Any CPU.Build.0 = Release|Any CPU
{200F4052-CB53-4151-8E4C-D430C653255B}.Release|x64.ActiveCfg = Release|Any CPU
{200F4052-CB53-4151-8E4C-D430C653255B}.Release|x64.Build.0 = Release|Any CPU
{200F4052-CB53-4151-8E4C-D430C653255B}.Release|x86.ActiveCfg = Release|Any CPU
{200F4052-CB53-4151-8E4C-D430C653255B}.Release|x86.Build.0 = Release|Any CPU
{B24DF638-F187-427C-863E-26A04272CF40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B24DF638-F187-427C-863E-26A04272CF40}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B24DF638-F187-427C-863E-26A04272CF40}.Debug|x64.ActiveCfg = Debug|Any CPU
{B24DF638-F187-427C-863E-26A04272CF40}.Debug|x64.Build.0 = Debug|Any CPU
{B24DF638-F187-427C-863E-26A04272CF40}.Debug|x86.ActiveCfg = Debug|Any CPU
{B24DF638-F187-427C-863E-26A04272CF40}.Debug|x86.Build.0 = Debug|Any CPU
{B24DF638-F187-427C-863E-26A04272CF40}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B24DF638-F187-427C-863E-26A04272CF40}.Release|Any CPU.Build.0 = Release|Any CPU
{B24DF638-F187-427C-863E-26A04272CF40}.Release|x64.ActiveCfg = Release|Any CPU
{B24DF638-F187-427C-863E-26A04272CF40}.Release|x64.Build.0 = Release|Any CPU
{B24DF638-F187-427C-863E-26A04272CF40}.Release|x86.ActiveCfg = Release|Any CPU
{B24DF638-F187-427C-863E-26A04272CF40}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{973F55C0-3CF0-4810-873E-ED48E6F52042} = {FA9762F1-6DC4-4722-8A92-962C1EDA0557}
{FDC03A90-AC82-499A-A5F1-C467B080C46B} = {8768CBBD-D786-4FBA-9F42-B49D093DC30D}
{200F4052-CB53-4151-8E4C-D430C653255B} = {38E7358A-E518-4E9D-AB95-3D6BA0F97CE0}
{B24DF638-F187-427C-863E-26A04272CF40} = {F742414A-CF15-46C5-8EC8-1962F15FB76B}
EndGlobalSection
EndGlobal
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace InsightArchitectures.Types
{
public partial record SwedishPersonalNumber
public partial record SwedishPersonalIdentityNumber
{
private static readonly Regex _personalNumberRegex = new Regex(@"^(?<year>(\d{2})?(\d{2}))\-?(?<month>\d{2})\-?(?<day>\d{2})[\-\+]?(?<extra>\d{4})$", RegexOptions.Compiled);

Expand All @@ -15,7 +15,7 @@ public partial record SwedishPersonalNumber
/// <param name="input">The string to parse.</param>
/// <param name="result">The result of the operation.</param>
/// <returns><c>True</c> if the parsing was successful.</returns>
public static bool TryParse(string input, [NotNullWhen(true)] out SwedishPersonalNumber? result)
public static bool TryParse(string input, [NotNullWhen(true)] out SwedishPersonalIdentityNumber? result)
{
_ = input ?? throw new ArgumentNullException(nameof(input));

Expand All @@ -34,7 +34,7 @@ public static bool TryParse(string input, [NotNullWhen(true)] out SwedishPersona

if (DateTime.TryParse($"{year}-{month}-{day}", out var date) && int.TryParse(match.Groups["extra"].Value, NumberStyles.None, CultureInfo.InvariantCulture.NumberFormat, out var extra))
{
result = new SwedishPersonalNumber(date, extra);
result = new SwedishPersonalIdentityNumber(date, extra);
return true;
}

Expand All @@ -52,5 +52,21 @@ static string getYear(Group group)
return str;
}
}

/// <summary>
/// Parses a string into a Swedish personal number.
/// </summary>
/// <param name="input">The string to parse.</param>
/// <returns>The parsed Swedish Personal Identity Number.</returns>
/// <exception cref="FormatException">Thrown if <paramref name="input"/> does not contain a valid Swedish Personal Identity Number.</exception>
public static SwedishPersonalIdentityNumber Parse(string input)
{
if (TryParse(input, out var result))
{
return result;
}

throw new FormatException($"'{input}' is not a valid format for a Swedish Personal Identity Number");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace InsightArchitectures.Types
/// <summary>
/// A representation of the Swedish personal number (personnummer).
/// </summary>
public partial record SwedishPersonalNumber
public partial record SwedishPersonalIdentityNumber
{
/// <summary>
/// Creates a new instance of <see cref="SwedishPersonalNumber" /> from a date and an ordinal number.
/// Creates a new instance of <see cref="SwedishPersonalIdentityNumber" /> from a date and an ordinal number.
/// </summary>
/// <param name="dateOfBirth">The date part of the Swedish personal number.</param>
/// <param name="ordinalNumber">The ordinal number part of the Swedish personal number.</param>
public SwedishPersonalNumber(DateTime dateOfBirth, int ordinalNumber)
/// <param name="dateOfBirth">The date part of the Swedish personal identity number.</param>
/// <param name="ordinalNumber">The ordinal number part of the Swedish personal identity number.</param>
public SwedishPersonalIdentityNumber(DateTime dateOfBirth, int ordinalNumber)
{
DateOfBirth = dateOfBirth;

Expand All @@ -35,13 +35,13 @@ public SwedishPersonalNumber(DateTime dateOfBirth, int ordinalNumber)
public int OrdinalNumber { get; init; }

/// <summary>
/// Returns the value of this instance of <see cref="SwedishPersonalNumber" /> as a <see cref="string" /> according to the specified concrete implementation.
/// Returns the value of this instance of <see cref="SwedishPersonalIdentityNumber" /> as a <see cref="string" /> according to the specified concrete implementation.
/// </summary>
/// <param name="formatter">The formatter to use to formatter the Swedish personal number.</param>
/// <returns>A string containing this <see cref="SwedishPersonalNumber" /> formatted as a <see cref="string" />.</returns>
public string ToFormattedString(SwedishPersonalNumberFormatter formatter) => formatter?.Format(this) ?? throw new ArgumentNullException(nameof(formatter));
/// <param name="formatter">The formatter to use to formatter the Swedish personal identity number.</param>
/// <returns>A string containing this <see cref="SwedishPersonalIdentityNumber" /> formatted as a <see cref="string" />.</returns>
public string ToFormattedString(SwedishPersonalIdentityNumberFormatter formatter) => formatter?.Format(this) ?? throw new ArgumentNullException(nameof(formatter));

/// <inheritdoc/>
public override string ToString() => ToFormattedString(SwedishPersonalNumberFormatter.TwelveDigits);
public override string ToString() => ToFormattedString(SwedishPersonalIdentityNumberFormats.TwelveDigits);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0;netstandard2.1</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
<PackageDescription>This package contains a representation of the Swedish Personal Identity Number that can be reused across projects. It supports parsing a string and printing it as a string according to different formats.</PackageDescription>
</PropertyGroup>

<ItemGroup>
<Compile Update="SwedishPersonalIdentityNumber.*.cs">
<DependentUpon>SwedishPersonalIdentityNumber.cs</DependentUpon>
</Compile>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace InsightArchitectures.Types
{
/// <summary>
/// A set of formats that can be used to represent a Swedish Personal Identity Number.
/// </summary>
public static class SwedishPersonalIdentityNumberFormats
{
/// <summary>
/// A formatter that formats a <see cref="SwedishPersonalIdentityNumber" /> using the formatter <c>yyyyMMdd-xxxx</c>.
/// </summary>
public static readonly SwedishPersonalIdentityNumberFormatter TwelveDigits = new TwelveDigitsSwedishPersonalIdentityNumberFormatter();

/// <summary>
/// A formatter that formats a <see cref="SwedishPersonalIdentityNumber" /> using the formatter <c>yyMMdd-xxxx</c>.
/// </summary>
public static readonly SwedishPersonalIdentityNumberFormatter TenDigits = new TenDigitsSwedishPersonalIdentityNumberFormatter();

/// <summary>
/// A formatter that formats a <see cref="SwedishPersonalIdentityNumber" /> using the formatter <c>yyyyMMddxxxx</c>.
/// </summary>
public static readonly SwedishPersonalIdentityNumberFormatter NoSplitTwelveDigits = new NoSplitTwelveDigitsSwedishPersonalIdentityNumberFormatter();

/// <summary>
/// A formatter that formats a <see cref="SwedishPersonalIdentityNumber" /> using the formatter <c>yyMMddxxxx</c>.
/// </summary>
public static readonly SwedishPersonalIdentityNumberFormatter NoSplitTenDigits = new NoSplitTenDigitsSwedishPersonalIdentityNumberFormatter();

/// <summary>
/// A formatter that formats a <see cref="SwedishPersonalIdentityNumber" /> using the formatter <c>yyyy-MM-dd-xxxx</c>.
/// </summary>
public static readonly SwedishPersonalIdentityNumberFormatter SplitTwelveDigits = new SplitTwelveDigitsSwedishPersonalIdentityNumberFormatter();

/// <summary>
/// A formatter that formats a <see cref="SwedishPersonalIdentityNumber" /> using the formatter <c>yy-MM-dd-xxxx</c>.
/// </summary>
public static readonly SwedishPersonalIdentityNumberFormatter SplitTenDigits = new SplitTenDigitsSwedishPersonalIdentityNumberFormatter();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;

namespace InsightArchitectures.Types
{
/// <summary>
/// An abstract class that represents the different ways to formatter a Swedish personal identity number as a string.
/// </summary>
public abstract class SwedishPersonalIdentityNumberFormatter
{
/// <summary>
/// Formats the <paramref name="pin"/> according to the specified concrete implementation.
/// </summary>
/// <param name="pin">The Swedish personal identity number to formatter as string.</param>
/// <returns>The string formatted according to the concrete implementation.</returns>
public abstract string Format(SwedishPersonalIdentityNumber pin);
}

/// <summary>
/// Formats a Swedish personal identity number using the formatter yyyyMMddxxxx.
/// </summary>
public class NoSplitTwelveDigitsSwedishPersonalIdentityNumberFormatter : SwedishPersonalIdentityNumberFormatter
{
/// <inheritdoc/>
public override string Format(SwedishPersonalIdentityNumber pn)
{
_ = pn ?? throw new ArgumentNullException(nameof(pn));

return $"{pn.DateOfBirth:yyyyMMdd}{pn.OrdinalNumber:0000}";
}
}

/// <summary>
/// Formats a Swedish personal identity number using the formatter yyMMddxxxx.
/// </summary>
public class NoSplitTenDigitsSwedishPersonalIdentityNumberFormatter : SwedishPersonalIdentityNumberFormatter
{
/// <inheritdoc/>
public override string Format(SwedishPersonalIdentityNumber pn)
{
_ = pn ?? throw new ArgumentNullException(nameof(pn));

return $"{pn.DateOfBirth:yyMMdd}{pn.OrdinalNumber:0000}";
}
}

/// <summary>
/// Formats a Swedish personal identity number using the formatter yyyyMMdd-xxxx.
/// </summary>
public class TwelveDigitsSwedishPersonalIdentityNumberFormatter : SwedishPersonalIdentityNumberFormatter
{
/// <inheritdoc/>
public override string Format(SwedishPersonalIdentityNumber pn)
{
_ = pn ?? throw new ArgumentNullException(nameof(pn));

return $"{pn.DateOfBirth:yyyyMMdd}-{pn.OrdinalNumber:0000}";
}
}

/// <summary>
/// Formats a Swedish personal identity number using the formatter yyMMdd-xxxx.
/// </summary>
public class TenDigitsSwedishPersonalIdentityNumberFormatter : SwedishPersonalIdentityNumberFormatter
{
/// <inheritdoc/>
public override string Format(SwedishPersonalIdentityNumber pn)
{
_ = pn ?? throw new ArgumentNullException(nameof(pn));

return $"{pn.DateOfBirth:yyMMdd}-{pn.OrdinalNumber:0000}";
}
}

/// <summary>
/// Formats a Swedish personal identity number using the formatter yyyyMMdd-xxxx.
/// </summary>
public class SplitTwelveDigitsSwedishPersonalIdentityNumberFormatter : SwedishPersonalIdentityNumberFormatter
{
/// <inheritdoc/>
public override string Format(SwedishPersonalIdentityNumber pn)
{
_ = pn ?? throw new ArgumentNullException(nameof(pn));

return $"{pn.DateOfBirth:yyyy-MM-dd}-{pn.OrdinalNumber:0000}";
}
}

/// <summary>
/// Formats a Swedish personal identity number using the formatter yyMMdd-xxxx.
/// </summary>
public class SplitTenDigitsSwedishPersonalIdentityNumberFormatter : SwedishPersonalIdentityNumberFormatter
{
/// <inheritdoc/>
public override string Format(SwedishPersonalIdentityNumber pn)
{
_ = pn ?? throw new ArgumentNullException(nameof(pn));

return $"{pn.DateOfBirth:yy-MM-dd}-{pn.OrdinalNumber:0000}";
}
}
}
17 changes: 0 additions & 17 deletions src/SwedishPersonalNumber/SwedishPersonalNumber.csproj

This file was deleted.

Loading

0 comments on commit 19ce82d

Please sign in to comment.