diff options
author | Sergey Chebotar <s.chebotar@gmail.com> | 2022-12-22 14:52:54 +0300 |
---|---|---|
committer | Sergey Chebotar <s.chebotar@gmail.com> | 2022-12-22 14:52:54 +0300 |
commit | f267b27375a6d9577727b6df51f7550adc6480c0 (patch) | |
tree | 21be756a3b95d89641612eed8a79e809ad989dcd /src/Models/Sku.cs | |
parent | 8ac323447347e31875e1db53d899d13a4ac51c1d (diff) |
Change Sku to RhSolutions.Api implementation
Diffstat (limited to 'src/Models/Sku.cs')
-rw-r--r-- | src/Models/Sku.cs | 117 |
1 files changed, 83 insertions, 34 deletions
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)(?<Article>\d{6})([1\s-]|)(?<Variant>\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<Sku> 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; + } + set + { + 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<Sku> 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); + } } + } - match = Regex.Match(line, @"\b\d{6}\b"); - if (match.Success) + public static bool TryParse(string line, out IEnumerable<Sku> skus) + { + MatchCollection matches = Regex.Matches(line, matchPattern); + if (matches.Count == 0) { - string sku = match.Value.Substring(0, 6); - string variant = "001"; - rehauSku = new Sku(sku, variant); - return true; + skus = Enumerable.Empty<Sku>(); + return false; } else { - rehauSku = null; - return false; + skus = GetValidSkus(line); + return true; } } |