Skip to content

Commit 23c2d58

Browse files
authored
Merge pull request #2 from PapiHack/add-serializer/currencies
Add serializer/currencies
2 parents c599090 + fe1d628 commit 23c2d58

9 files changed

Lines changed: 302 additions & 39 deletions

File tree

src/Config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,13 @@ public static function setCurrency($currency)
110110

111111
}
112112

113+
// @codeCoverageIgnoreStart
113114
private static function setLiveMode($liveMode)
114115
{
115116
self::$liveMode = $liveMode;
116117
self::$testMode = !$liveMode;
117118
}
119+
// @codeCoverageIgnoreEnd
118120

119121
private static function setTestMode($testMode)
120122
{
@@ -155,6 +157,7 @@ public static function setEnv($env)
155157
}
156158
}
157159

160+
// @codeCoverageIgnoreStart
158161
private static function setApproriateLiveAndTestMode()
159162
{
160163
switch (self::getEnv())
@@ -167,5 +170,6 @@ private static function setApproriateLiveAndTestMode()
167170
break;
168171
}
169172
}
173+
// @codeCoverageIgnoreEnd
170174

171175
}

src/CustomField.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PayTech;
44

55
use Exception;
6+
use PayTech\Utils\Serializer;
67

78
/**
89
*
@@ -38,6 +39,6 @@ public static function find($name)
3839

3940
public static function retrieve()
4041
{
41-
return self::$data;
42+
return new Serializer(self::$data);
4243
}
4344
}

src/Enums/Currency.php

Lines changed: 171 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,175 @@
1212
*/
1313
abstract class Currency
1414
{
15-
const XOF = 'XOF';
16-
const USD = 'USD';
17-
const CAD = 'CAD';
18-
const GBP = 'GBP';
19-
const MAD = 'MAD';
15+
const AED = "AED";
16+
const AFN = "AFN";
17+
const ALL = "ALL";
18+
const AMD = "AMD";
19+
const ANG = "ANG";
20+
const AOA = "AOA";
21+
const ARS = "ARS";
22+
const AUD = "AUD";
23+
const AWG = "AWG";
24+
const AZN = "AZN";
25+
const BAM = "BAM";
26+
const BBD = "BBD";
27+
const BDT = "BDT";
28+
const BGN = "BGN";
29+
const BHD = "BHD";
30+
const BIF = "BIF";
31+
const BMD = "BMD";
32+
const BND = "BND";
33+
const BOB = "BOB";
34+
const BRL = "BRL";
35+
const BSD = "BSD";
36+
const BTC = "BTC";
37+
const BTN = "BTN";
38+
const BWP = "BWP";
39+
const BYN = "BYN";
40+
const BZD = "BZD";
41+
const CAD = "CAD";
42+
const CDF = "CDF";
43+
const CHF = "CHF";
44+
const CLF = "CLF";
45+
const CLP = "CLP";
46+
const CNH = "CNH";
47+
const CNY = "CNY";
48+
const COP = "COP";
49+
const CRC = "CRC";
50+
const CUC = "CUC";
51+
const CUP = "CUP";
52+
const CVE = "CVE";
53+
const CZK = "CZK";
54+
const DJF = "DJF";
55+
const DKK = "DKK";
56+
const DOP = "DOP";
57+
const DZD = "DZD";
58+
const EGP = "EGP";
59+
const ERN = "ERN";
60+
const ETB = "ETB";
61+
const EUR = "EUR";
62+
const FJD = "FJD";
63+
const FKP = "FKP";
64+
const GBP = "GBP";
65+
const GEL = "GEL";
66+
const GGP = "GGP";
67+
const GHS = "GHS";
68+
const GIP = "GIP";
69+
const GMD = "GMD";
70+
const GNF = "GNF";
71+
const GTQ = "GTQ";
72+
const GYD = "GYD";
73+
const HKD = "HKD";
74+
const HNL = "HNL";
75+
const HRK = "HRK";
76+
const HTG = "HTG";
77+
const HUF = "HUF";
78+
const IDR = "IDR";
79+
const ILS = "ILS";
80+
const IMP = "IMP";
81+
const INR = "INR";
82+
const IQD = "IQD";
83+
const IRR = "IRR";
84+
const ISK = "ISK";
85+
const JEP = "JEP";
86+
const JMD = "JMD";
87+
const JOD = "JOD";
88+
const JPY = "JPY";
89+
const KES = "KES";
90+
const KGS = "KGS";
91+
const KHR = "KHR";
92+
const KMF = "KMF";
93+
const KPW = "KPW";
94+
const KRW = "KRW";
95+
const KWD = "KWD";
96+
const KYD = "KYD";
97+
const KZT = "KZT";
98+
const LAK = "LAK";
99+
const LBP = "LBP";
100+
const LKR = "LKR";
101+
const LRD = "LRD";
102+
const LSL = "LSL";
103+
const LYD = "LYD";
104+
const MAD = "MAD";
105+
const MDL = "MDL";
106+
const MGA = "MGA";
107+
const MKD = "MKD";
108+
const MMK = "MMK";
109+
const MNT = "MNT";
110+
const MOP = "MOP";
111+
const MRO = "MRO";
112+
const MRU = "MRU";
113+
const MUR = "MUR";
114+
const MVR = "MVR";
115+
const MWK = "MWK";
116+
const MXN = "MXN";
117+
const MYR = "MYR";
118+
const MZN = "MZN";
119+
const NAD = "NAD";
120+
const NGN = "NGN";
121+
const NIO = "NIO";
122+
const NOK = "NOK";
123+
const NPR = "NPR";
124+
const NZD = "NZD";
125+
const OMR = "OMR";
126+
const PAB = "PAB";
127+
const PEN = "PEN";
128+
const PGK = "PGK";
129+
const PHP = "PHP";
130+
const PKR = "PKR";
131+
const PLN = "PLN";
132+
const PYG = "PYG";
133+
const QAR = "QAR";
134+
const RON = "RON";
135+
const RSD = "RSD";
136+
const RUB = "RUB";
137+
const RWF = "RWF";
138+
const SAR = "SAR";
139+
const SBD = "SBD";
140+
const SCR = "SCR";
141+
const SDG = "SDG";
142+
const SEK = "SEK";
143+
const SGD = "SGD";
144+
const SHP = "SHP";
145+
const SLL = "SLL";
146+
const SOS = "SOS";
147+
const SRD = "SRD";
148+
const SSP = "SSP";
149+
const STD = "STD";
150+
const STN = "STN";
151+
const SVC = "SVC";
152+
const SYP = "SYP";
153+
const SZL = "SZL";
154+
const THB = "THB";
155+
const TJS = "TJS";
156+
const TMT = "TMT";
157+
const TND = "TND";
158+
const TOP = "TOP";
159+
const TRY = "TRY";
160+
const TTD = "TTD";
161+
const TWD = "TWD";
162+
const TZS = "TZS";
163+
const UAH = "UAH";
164+
const UGX = "UGX";
165+
const USD = "USD";
166+
const UYU = "UYU";
167+
const UZS = "UZS";
168+
const VEF = "VEF";
169+
const VES = "VES";
170+
const VND = "VND";
171+
const VUV = "VUV";
172+
const WST = "WST";
173+
const XAF = "XAF";
174+
const XAG = "XAG";
175+
const XAU = "XAU";
176+
const XCD = "XCD";
177+
const XDR = "XDR";
178+
const XOF = "XOF";
179+
const XPD = "XPD";
180+
const XPF = "XPF";
181+
const XPT = "XPT";
182+
const YER = "YER";
183+
const ZAR = "ZAR";
184+
const ZMW = "ZMW";
185+
const ZWL = "ZWL";
20186
}

src/PayTech.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function send(InvoiceItem $invoiceItem)
2828
'ipn_url' => Config::getIpnUrl(),
2929
'success_url' => Config::getIsMobile() ? Config::MOBILE_SUCCESS_URL : Config::getSuccessUrl(),
3030
'cancel_url' => Config::getIsMobile() ? Config::MOBILE_CANCEL_URL : Config::getCancelUrl(),
31-
'custom_field' => json_encode(CustomField::retrieve())
31+
'custom_field' => CustomField::retrieve()->toJSONString()
3232
], []);
3333

3434
// @codeCoverageIgnoreStart

src/Utils/Check.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,24 @@
22

33
namespace PayTech\Utils;
44

5+
use PayTech\Enums\Currency;
6+
use PayTech\Enums\Environment;
7+
58
/**
69
*
710
* @author PapiHack
811
* @since 07/2020
912
*
1013
*/
11-
abstract class Check
12-
{
13-
14-
const authorizedCurrency = [
15-
'XOF',
16-
'USD',
17-
'CAD',
18-
'GBP',
19-
'MAD'
20-
];
21-
22-
const autorizedEnv = [
23-
'TEST',
24-
'PROD'
25-
];
26-
14+
abstract class Check
15+
{
2716
public static function isCurrencyAllowed($currency)
2817
{
29-
return array_search(strtoupper($currency), self::authorizedCurrency);
18+
return defined(Currency::class .'::'. strtoupper($currency));
3019
}
3120

32-
public static function isEnvAllowed($env)
21+
public static function isEnvAllowed($env)
3322
{
34-
return in_array(strtoupper($env), self::autorizedEnv);
23+
return defined(Environment::class .'::'. strtoupper($env));
3524
}
3625
}

src/Utils/Serializer.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace PayTech\Utils;
4+
5+
/**
6+
*
7+
* @author PapiHack
8+
* @since 07/2020
9+
*
10+
*/
11+
Class Serializer
12+
{
13+
private $data = [];
14+
15+
public function __construct(Array $data)
16+
{
17+
$this->setData($data);
18+
}
19+
20+
public function getData()
21+
{
22+
return $this->data;
23+
}
24+
25+
public function setData(Array $data)
26+
{
27+
$this->data = $data;
28+
}
29+
30+
public function toJSONString()
31+
{
32+
return json_encode($this->data);
33+
}
34+
35+
public function toXMLString()
36+
{
37+
return $this->arrayToXml($this->data, '<custom_fields/>');
38+
}
39+
40+
public function toQueryString()
41+
{
42+
return http_build_query($this->data);
43+
}
44+
45+
// @codeCoverageIgnoreStart
46+
private function arrayToXml($array, $rootElement = null, $xml = null) {
47+
$xmlDocument = $xml;
48+
49+
if ($xmlDocument === null)
50+
{
51+
$xmlDocument = new \SimpleXMLElement($rootElement !== null ? $rootElement : '<root/>');
52+
}
53+
54+
foreach ($array as $key => $value)
55+
{
56+
is_array($value) ? $this->arrayToXml($value, $key, $xmlDocument->addChild($key)) : $xmlDocument->addChild($key, $value);
57+
}
58+
59+
return $xmlDocument->asXML();
60+
}
61+
// @codeCoverageIgnoreEnd
62+
}

tests/Unit/PayTech/CustomFieldTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,32 @@
1616
});
1717

1818
test('The data property should be an empty Array', function () {
19-
$this->assertIsArray(CustomField::retrieve());
20-
$this->assertEmpty(CustomField::retrieve());
19+
$this->assertIsArray(CustomField::retrieve()->getData());
20+
$this->assertEmpty(CustomField::retrieve()->getData());
2121
});
2222

2323
test('data property should be an empty Array', function () {
2424
CustomField::push(['test' => 'this is a test']);
25-
$this->assertNotEmpty(CustomField::retrieve());
25+
$this->assertNotEmpty(CustomField::retrieve()->getData());
2626
});
2727

2828
it('Shoud have an entry named test', function () {
2929
CustomField::push(['test' => 'this is a test']);
30-
$this->assertTrue(array_key_exists('test', CustomField::retrieve()));
30+
$this->assertTrue(array_key_exists('test', CustomField::retrieve()->getData()));
3131
});
3232

3333
it('Shoud have an entry named test & price', function () {
3434
CustomField::push(['test' => 'this is a test']);
3535
CustomField::set('price', 500);
36-
$this->assertTrue(array_key_exists('test', CustomField::retrieve()));
37-
$this->assertTrue(array_key_exists('price', CustomField::retrieve()));
38-
$this->assertEquals(2, count(CustomField::retrieve()));
36+
$this->assertTrue(array_key_exists('test', CustomField::retrieve()->getData()));
37+
$this->assertTrue(array_key_exists('price', CustomField::retrieve()->getData()));
38+
$this->assertEquals(2, count(CustomField::retrieve()->getData()));
3939
});
4040

4141
it('Shoud set an dummy entry', function () {
4242
CustomField::set('dummy', 'test');
43-
$this->assertTrue(array_key_exists('dummy', CustomField::retrieve()));
44-
$this->assertEquals(3, count(CustomField::retrieve()));
43+
$this->assertTrue(array_key_exists('dummy', CustomField::retrieve()->getData()));
44+
$this->assertEquals(3, count(CustomField::retrieve()->getData()));
4545
});
4646

4747
it('Shoud get dummy value', function () {

0 commit comments

Comments
 (0)