1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: ignatenkov |
5
|
|
|
* Date: 23.08.15 |
6
|
|
|
* Time: 4:51 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace YaWeather; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use YaWeather\Models\City; |
15
|
|
|
use YaWeather\Models\Day; |
16
|
|
|
use YaWeather\Models\Detail; |
17
|
|
|
|
18
|
|
|
class ParseXml { |
19
|
|
|
|
20
|
|
|
private $xml; |
21
|
|
|
private $city; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param $fileName |
26
|
|
|
*/ |
27
|
|
|
public function __construct($fileName) |
28
|
|
|
{ |
29
|
|
|
$this->xml = @simplexml_load_file($fileName); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Parse weather from XML and return Model City |
34
|
|
|
* @return bool|City |
35
|
|
|
*/ |
36
|
|
|
public function parse() { |
37
|
|
|
|
38
|
|
|
// check xml load |
39
|
|
|
if(!$this->xml) |
40
|
|
|
return false; |
41
|
|
|
|
42
|
|
|
$city = new City(); |
43
|
|
View Code Duplication |
foreach($this->xml->attributes() as $key => $value) { |
|
|
|
|
44
|
|
|
if(isset(Mapping::mapCity()[$key])) |
45
|
|
|
$city->{Mapping::mapCity()[$key]} = (string) $value; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$fact = new Detail(); |
49
|
|
View Code Duplication |
foreach ($this->xml->fact[0] as $key => $value) { |
|
|
|
|
50
|
|
|
if(isset(Mapping::mapDetail()[$key])) { |
51
|
|
|
$fact->{Mapping::mapDetail()[$key]} = (string) $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
$city->fact = $fact; |
56
|
|
|
|
57
|
|
|
$fact = new Detail(); |
58
|
|
View Code Duplication |
foreach ($this->xml->yesterday[0] as $key => $value) { |
|
|
|
|
59
|
|
|
if(isset(Mapping::mapDetail()[$key])) { |
60
|
|
|
$fact->{Mapping::mapDetail()[$key]} = (string) $value; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
$city->yesterday = $fact; |
65
|
|
|
|
66
|
|
|
$days = array(); |
67
|
|
|
foreach($this->xml->day as $value) { |
68
|
|
|
$day = new Day(); |
69
|
|
|
foreach ($value as $key => $value1) { |
70
|
|
|
if(isset(Mapping::mapDay()[$key])) { |
71
|
|
|
$day->{Mapping::mapDay()[$key]} = (string) $value1; |
72
|
|
|
} |
73
|
|
|
//var_dump($value1->attributes()["typeid"]); |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
foreach ($value->day_part as $key2 => $value2) { |
78
|
|
|
$detail = new Detail(); |
79
|
|
|
foreach ($value2 as $key3 => $value3) { |
80
|
|
|
if(isset(Mapping::mapDetail()[$key3])) { |
81
|
|
|
$detail->{Mapping::mapDetail()[$key3]} = (string) $value3; |
82
|
|
|
} |
83
|
|
|
if(isset(Mapping::mapDetail()["inner"][$key3])) { |
84
|
|
|
$detail->{Mapping::mapDetail()["inner"][$key3]} = (string) $value3->avg; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
$day->{$value2->attributes()["type"]} = $detail; |
88
|
|
|
} |
89
|
|
|
$days[(string)$value->attributes()["date"]] = $day; |
90
|
|
|
} |
91
|
|
|
$city->days = $days; |
92
|
|
|
|
93
|
|
|
return $city; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* static function encode string utf8 to cp1251 |
98
|
|
|
* @param $value |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public static function encoding($value) { |
102
|
|
|
return iconv( "UTF-8", "windows-1251//TRANSLIT", $value); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
|
107
|
|
|
|
108
|
|
|
|
109
|
|
|
|
110
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.