aboutsummaryrefslogtreecommitdiff
path: root/src/Models/Sku.cs
blob: 99fcce6cfda832218329061dcfb54be5c8eb09a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System.Collections.Generic;
using System.Linq;
using System;
using System.Text.RegularExpressions;

namespace RhSolutions.Models
{
    public class Sku
    {
        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)
        {
            Article = article;
            Variant = variant;
        }

        public string Id
        {
            get
            {
                return $"1{Article}1{Variant}";
            }
            set
            {
                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}");
                }
            }
        }
        public string Article
        {
            get
            {
                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);
                }
            }
        }

        public static bool TryParse(string line, out IEnumerable<Sku> skus)
        {
            MatchCollection matches = Regex.Matches(line, matchPattern);
            if (matches.Count == 0)
            {
                skus = Enumerable.Empty<Sku>();
                return false;
            }

            else
            {
                skus = GetValidSkus(line);
                return true;
            }
        }

        public override string ToString()
        {
            return $"1{Article}1{Variant}";
        }
    }
}