1 module iyzipay.pkibuilder; 2 3 import std.algorithm.mutation: strip; 4 import std.algorithm.searching: endsWith; 5 import std.string: indexOf, stripRight; 6 import std.format: format; 7 import std.conv: to; 8 9 class PkiBuilder 10 { 11 private string _pkiString; 12 13 public void append(string key, string value) 14 { 15 _pkiString = _pkiString ~ key ~ "=" ~ value ~ ","; 16 } 17 unittest 18 { 19 PkiBuilder pki = new PkiBuilder(); 20 pki.append("key", "value"); 21 22 assert(pki.getPkiString == "[key=value]", pki.getPkiString); 23 } 24 25 public void appendPrice(string key, string value) 26 { 27 string formatPrice = string.init; 28 29 if (value.indexOf('.') < 0) formatPrice = value ~ ".0"; 30 else formatPrice = value.stripRight("0"); 31 32 if (formatPrice.endsWith(".")) formatPrice = formatPrice ~ "0"; 33 34 _pkiString = _pkiString ~ key ~ "=" ~ formatPrice ~ ","; 35 } 36 unittest 37 { 38 PkiBuilder pki = new PkiBuilder(); 39 pki.appendPrice("price3", "100"); 40 pki.appendPrice("price4", "100.10"); 41 pki.appendPrice("price5", "100.123"); 42 pki.appendPrice("price6", "100.10100"); 43 pki.appendPrice("price7", "100.120"); 44 pki.appendPrice("price8", "100.00"); 45 46 assert(pki.getPkiString == "[price3=100.0,price4=100.1,price5=100.123,price6=100.101,price7=100.12,price8=100.0]", 47 pki.getPkiString); 48 } 49 50 public void appendArray(string key, string value) 51 { 52 _pkiString = _pkiString ~ key ~ "=" ~ value ~ ","; 53 } 54 55 private void removeTrailingComma() 56 { 57 _pkiString = _pkiString[0..$-1]; 58 } 59 60 public string getPkiString() @property 61 { 62 removeTrailingComma(); 63 return "[" ~ _pkiString ~ "]"; 64 } 65 }