Skip to content

Commit f35e6d7

Browse files
committed
add Serializer class & edit retrieve method in CustomField class
1 parent c599090 commit f35e6d7

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

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/Utils/Serializer.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
private function arrayToXml($array, $rootElement = null, $xml = null) {
46+
$xmlDocument = $xml;
47+
48+
if ($xmlDocument === null)
49+
{
50+
$xmlDocument = new \SimpleXMLElement($rootElement !== null ? $rootElement : '<root/>');
51+
}
52+
53+
foreach ($array as $key => $value)
54+
{
55+
is_array($value) ? $this->arrayToXml($value, $key, $xmlDocument->addChild($key)) : $xmlDocument->addChild($key, $value);
56+
}
57+
58+
return $xmlDocument->asXML();
59+
}
60+
}

0 commit comments

Comments
 (0)