diff --git a/src/Controllers/ExportTool.cs b/src/Controllers/ExportTool.cs index 6d8c348..cb5aefb 100644 --- a/src/Controllers/ExportTool.cs +++ b/src/Controllers/ExportTool.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using RhSolutions.Models; +using System.Linq; namespace RhSolutions.Controllers { @@ -56,9 +57,9 @@ namespace RhSolutions.Controllers { object current = cells[row, column]; - if (Sku.TryParse(current.ToString(), out Sku rauSku)) + if (Sku.TryParse(current.ToString(), out var rauSku)) { - sku = rauSku.ToString(); + sku = rauSku.FirstOrDefault().ToString() ?? null; } else if (current.GetType() == typeof(string) diff --git a/src/Models/Sku.cs b/src/Models/Sku.cs index 1c5e477..99fcce6 100644 --- a/src/Models/Sku.cs +++ b/src/Models/Sku.cs @@ -1,11 +1,15 @@ -using System.Text.RegularExpressions; +using System.Collections.Generic; +using System.Linq; +using System; +using System.Text.RegularExpressions; namespace RhSolutions.Models { - internal class Sku + public class Sku { - public string Article { get; private set; } - public string Variant { get; private set; } + private const string matchPattern = @"([1\D]|\b)(?
\d{6})([1\s-]|)(?\d{3})\b"; + private string _article; + private string _variant; public Sku(string article, string variant) { @@ -13,49 +17,94 @@ namespace RhSolutions.Models Variant = variant; } - public static bool TryParse(string line, out Sku rehauSku) + public string Id { - Match match; - match = Regex.Match(line, @"\b[1]\d{6}[1]\d{3}\b"); - if (match.Success) + get { - string sku = match.Value.Substring(1, 6); - string variant = match.Value.Substring(8, 3); - rehauSku = new Sku(sku, variant); - return true; + return $"1{Article}1{Variant}"; } - - match = Regex.Match(line, @"\b\d{6}\D\d{3}\b"); - if (match.Success) + set { - string sku = match.Value.Substring(0, 6); - string variant = match.Value.Substring(7, 3); - rehauSku = new Sku(sku, variant); - return true; + if (TryParse(value, out IEnumerable skus)) + { + if (skus.Count() > 1) + { + throw new ArgumentException($"More than one valid sku detected: {value}"); + } + else + { + this.Article = skus.First().Article; + this.Variant = skus.First().Variant; + } + } + else + { + throw new ArgumentException($"Invalid sku input: {value}"); + } } - - match = Regex.Match(line, @"\b\d{9}\b"); - if (match.Success) + } + public string Article + { + get { - string sku = match.Value.Substring(0, 6); - string variant = match.Value.Substring(6, 3); - rehauSku = new Sku(sku, variant); - return true; + return _article; } - - match = Regex.Match(line, @"\b\d{6}\b"); - if (match.Success) + set { - string sku = match.Value.Substring(0, 6); - string variant = "001"; - rehauSku = new Sku(sku, variant); - return true; + if (value == null || value.Length != 6 || value.Where(c => char.IsDigit(c)).Count() != 6) + { + throw new ArgumentException($"Wrong Article: {Article}"); + } + else + { + _article = value; + } + } + } + public string Variant + { + get + { + return _variant; + } + set + { + if (value == null || value.Length != 3 || value.Where(c => char.IsDigit(c)).Count() != 3) + { + throw new ArgumentException($"Wrong Variant: {Variant}"); + } + else _variant = value; + } + } + public static IEnumerable GetValidSkus(string line) + { + MatchCollection matches = Regex.Matches(line, matchPattern); + if (matches.Count == 0) + { + yield break; + } + else + { + foreach (Match m in matches) + { + yield return new Sku(m.Groups["Article"].Value, m.Groups["Variant"].Value); + } + } + } + + public static bool TryParse(string line, out IEnumerable skus) + { + MatchCollection matches = Regex.Matches(line, matchPattern); + if (matches.Count == 0) + { + skus = Enumerable.Empty(); + return false; } else { - rehauSku = null; - return false; + skus = GetValidSkus(line); + return true; } }