-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyandex
128 lines (94 loc) · 2.83 KB
/
yandex
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
<?php
/*
* Prepare data for feed
*/
class DataSource {
public function items()
{
$data = array();
$data[0]['internal-id'] = 1;
$data[0]['type']='type';
$data[0]['property-type'] = 'type';
$data[0]['category']='cat';
$data[0]['url'] = 'url';
$data[0]['creation-date'] = '2010-11-13T12:32:45+04:00';
$data[0]['country'] = 'cou';
$data[0]['locality-name'] = 'loc';
$data[0]['address'] = 'addr';
$data[0]['phone'] = '123';
return $data;
}
}
/*
* XML formating for Yandex Feed
* Author: Michael Makarenkov
*/
interface FeedFactory {
public function create();
}
class YandexFactory implements FeedFactory {
public function create() {
return new Yandex();
}
}
abstract class Feed {
var $fileDescriptor;
public function writeHeader(){}
public function writeFooter(){}
public function writeItem(){}
}
class Yandex extends Feed {
public function __construct() {
$this->fileDescriptor = fopen("ya_feed.xml", "w");
$this->writeHeader();
$dataSource = new DataSource();
/*
* $data should be array data
*/
foreach($dataSource->items() as $data) {
$this->writeItem($data);
}
$this->writeFooter();
}
public function writeHeader() {
$str = '<?xml version="1.0" encoding="UTF-8"?>
<realty-feed xmlns="http://webmaster.yandex.ru/schemas/feed/realty/2010-06">
<generation-date>'.date('Y-m-d',time()).'T'.date('H:i:s',time()).'+04:00</generation-date>';
fwrite($this->fileDescriptor, $str);
}
public function writeFooter() {
$str = '</realty-feed>';
fwrite($this->fileDescriptor, $str);
fclose($this->fileDescriptor);
}
public function writeItem($data) {
$str = '<offer internal-id="'.$data['internal-id'].'">
<type>'.$data['type'].'</type>
<property-type>'.$data['property-type'].'</property-type>
<category>'.$data['category'].'</category>
<url>'.$data['url'].'</url>
<creation-date>'.$data['creation-date'].'</creation-date>
<location>
<country>'.$data['country'].'</country>
<locality-name>'.$data['locality-name'].'</locality-name>
<address>'.$data['address'].'</address>
</location>
<sales-agent>
<phone>'.$data['phone'].'</phone>
</sales-agent>
</offer>';
fwrite($this->fileDescriptor, $str);
}
}
class Application {
public function __construct(FeedFactory $ef) {
$feedObj = $ef->create();
}
}
class ApplicationRunner {
public static function run() {
$feed = new YandexFactory();
new Application($feed);
}
}
ApplicationRunner::run();