-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlParser.php
167 lines (148 loc) · 3.95 KB
/
xmlParser.php
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Convert an xml string into a simple text format
*
* @param $text
* @return bool|string
*/
function xmlToCSV($text)
{
if(! isValidXml($text)) {
// we can throw an exception here
return false;
}
$xml = simplexml_load_string($text);
$output = "Title|Code|Duration|Inclusions|MinPrice\n";
foreach($xml->TOUR as $item) {
$output .= implode('|', getRowData($item)) . "\n";
}
return $output;
}
/**
* Verify if xml string sent is valid
*
* @param $text
* @return bool
*/
function isValidXml($text)
{
$text = trim($text);
if (empty($text)) {
return false;
}
libxml_use_internal_errors(true);
simplexml_load_string($text);
$errors = libxml_get_errors();
libxml_clear_errors();
return empty($errors);
}
/**
* Get required values for each tour in the xml string
*
* @param $item
* @return array
*/
function getRowData($item)
{
$output = array();
$output[] = getTourTitle($item->Title);
$output[] = getTourCode($item->Code);
$output[] = getTourDuration($item->Duration);
$output[] = getTourInclusions($item->Inclusions);
$output[] = getTourMinPrice($item);
return $output;
}
/**
* get tour title
*
* @param $title
* @return string
*/
function getTourTitle($title)
{
$title = (string) $title;
return html_entity_decode($title, ENT_QUOTES, 'UTF-8');
}
/**
* get tour code
*
* @param $code
* @return string
*/
function getTourCode($code)
{
return (string) $code;
}
/**
* Get tour duration
*
* @param $duration
* @return int
*/
function getTourDuration($duration)
{
return (int) $duration;
}
/**
* Get a simplified verion of inclusions
*
* @param $inclusions
* @return mixed|string
*/
function getTourInclusions($inclusions)
{
$inclusions = str_replace(' ', ' ', strip_tags((string) $inclusions));
$inclusions = html_entity_decode($inclusions, ENT_QUOTES, 'UTF-8');
$inclusions = trim(preg_replace('!\s+!', ' ', $inclusions));
return $inclusions;
}
/**
* Get tour minimum price
*
* @param $item
* @return mixed
*/
function getTourMinPrice($item)
{
$itemsToCompare = array();
foreach($item->DEP as $departure)
{
$departurePrice = (int) $departure['EUR'];
$departureDiscount = (string) $departure['DISCOUNT'];
$itemsToCompare[] = getDeparturePrice($departurePrice, $departureDiscount);
}
return min($itemsToCompare);
}
/**
* Get final departure price based on discount
*
* @param $price
* @param null $discount
* @return string
*/
function getDeparturePrice($price, $discount = null)
{
if(!empty($discount)) {
$price = $price - ($price * ($discount / 100));
}
return number_format((float)$price, 2, '.', '');
}
$text = <<<XML
<?xml version="1.0"?>
<TOURS>
<TOUR>
<Title><![CDATA[Anzac & Egypt Combo Tour]]></Title>
<Code>AE-19</Code>
<Duration>18</Duration>
<Start>Istanbul</Start>
<End>Cairo</End>
<Inclusions>
<![CDATA[<div style="margin: 1px 0px; padding: 1px 0px; border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; text-align: justify; line-height: 19px; color: rgb(6, 119, 179);">The tour price cover the following services: <b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: transparent;">Accommodation</b>; 5, 4 and 3 star hotels </div>]]>
</Inclusions>
<DEP DepartureCode="AN-17" Starts="04/19/2015" GBP="1458" EUR="1724" USD="2350" DISCOUNT="15%" />
<DEP DepartureCode="AN-18" Starts="04/22/2015" GBP="1558" EUR="1784" USD="2550" DISCOUNT="20%" />
<DEP DepartureCode="AN-19" Starts="04/25/2015" GBP="1558" EUR="1784" USD="2550" />
</TOUR>
</TOURS>
XML;
var_dump(xmlToCSV($text));